• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PHPWord / 15406691115

03 Jun 2025 01:51AM UTC coverage: 96.752% (-0.008%) from 96.76%
15406691115

Pull #2785

github

web-flow
Merge 7fedd8556 into 1f28d36d4
Pull Request #2785: Update phpmath from 0.2.0 to 0.3.0

12483 of 12902 relevant lines covered (96.75%)

35.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.39
/src/PhpWord/Shared/Html.php
1
<?php
2

3
/**
4
 * This file is part of PHPWord - A pure PHP library for reading and writing
5
 * word processing documents.
6
 *
7
 * PHPWord is free software distributed under the terms of the GNU Lesser
8
 * General Public License version 3 as published by the Free Software Foundation.
9
 *
10
 * For the full copyright and license information, please read the LICENSE
11
 * file that was distributed with this source code. For the full list of
12
 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13
 *
14
 * @see         https://github.com/PHPOffice/PHPWord
15
 *
16
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17
 */
18

19
namespace PhpOffice\PhpWord\Shared;
20

21
use DOMAttr;
22
use DOMDocument;
23
use DOMNode;
24
use DOMXPath;
25
use Exception;
26
use PhpOffice\PhpWord\ComplexType\RubyProperties;
27
use PhpOffice\PhpWord\Element\AbstractContainer;
28
use PhpOffice\PhpWord\Element\Row;
29
use PhpOffice\PhpWord\Element\Table;
30
use PhpOffice\PhpWord\Element\TextRun;
31
use PhpOffice\PhpWord\Settings;
32
use PhpOffice\PhpWord\SimpleType\Jc;
33
use PhpOffice\PhpWord\SimpleType\NumberFormat;
34
use PhpOffice\PhpWord\Style\Paragraph;
35

36
/**
37
 * Common Html functions.
38
 *
39
 * @SuppressWarnings(PHPMD.UnusedPrivateMethod) For readWPNode
40
 */
41
class Html
42
{
43
    private const RGB_REGEXP = '/^\s*rgb\s*[(]\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*[)]\s*$/';
44

45
    protected static $listIndex = 0;
46

47
    protected static $xpath;
48

49
    protected static $options;
50

51
    /**
52
     * @var Css
53
     */
54
    protected static $css;
55

56
    /**
57
     * Add HTML parts.
58
     *
59
     * Note: $stylesheet parameter is removed to avoid PHPMD error for unused parameter
60
     * Warning: Do not pass user-generated HTML here, as that would allow an attacker to read arbitrary
61
     * files or perform server-side request forgery by passing local file paths or URLs in <img>.
62
     *
63
     * @param AbstractContainer $element Where the parts need to be added
64
     * @param string $html The code to parse
65
     * @param bool $fullHTML If it's a full HTML, no need to add 'body' tag
66
     * @param bool $preserveWhiteSpace If false, the whitespaces between nodes will be removed
67
     */
68
    public static function addHtml($element, $html, $fullHTML = false, $preserveWhiteSpace = true, $options = null): void
69
    {
70
        /*
71
         * @todo parse $stylesheet for default styles.  Should result in an array based on id, class and element,
72
         * which could be applied when such an element occurs in the parseNode function.
73
         */
74
        static::$options = $options;
63✔
75

76
        // Preprocess: remove all line ends, decode HTML entity,
77
        // fix ampersand and angle brackets and add body tag for HTML fragments
78
        $html = str_replace(["\n", "\r"], '', $html);
63✔
79
        $html = str_replace(['&lt;', '&gt;', '&amp;', '&quot;'], ['_lt_', '_gt_', '_amp_', '_quot_'], $html);
63✔
80
        $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
63✔
81
        $html = str_replace('&', '&amp;', $html);
63✔
82
        $html = str_replace(['_lt_', '_gt_', '_amp_', '_quot_'], ['&lt;', '&gt;', '&amp;', '&quot;'], $html);
63✔
83

84
        if (false === $fullHTML) {
63✔
85
            $html = '<body>' . $html . '</body>';
61✔
86
        }
87

88
        // Load DOM
89
        if (\PHP_VERSION_ID < 80000) {
63✔
90
            $orignalLibEntityLoader = libxml_disable_entity_loader(true);
63✔
91
        }
92
        $dom = new DOMDocument();
63✔
93
        $dom->preserveWhiteSpace = $preserveWhiteSpace;
63✔
94
        $dom->loadXML($html);
63✔
95
        static::$xpath = new DOMXPath($dom);
63✔
96
        $node = $dom->getElementsByTagName('body');
63✔
97

98
        static::parseNode($node->item(0), $element);
63✔
99
        if (\PHP_VERSION_ID < 80000) {
62✔
100
            libxml_disable_entity_loader($orignalLibEntityLoader);
62✔
101
        }
102
    }
103

104
    /**
105
     * parse Inline style of a node.
106
     *
107
     * @param DOMNode $node Node to check on attributes and to compile a style array
108
     * @param array<string, mixed> $styles is supplied, the inline style attributes are added to the already existing style
109
     *
110
     * @return array
111
     */
112
    protected static function parseInlineStyle($node, $styles = [])
113
    {
114
        if (XML_ELEMENT_NODE == $node->nodeType) {
61✔
115
            $attributes = $node->attributes; // get all the attributes(eg: id, class)
61✔
116

117
            $attributeDir = $attributes->getNamedItem('dir');
61✔
118
            $attributeDirValue = $attributeDir ? $attributeDir->nodeValue : '';
61✔
119
            $bidi = $attributeDirValue === 'rtl';
61✔
120
            foreach ($attributes as $attribute) {
61✔
121
                $val = $attribute->value;
43✔
122
                switch (strtolower($attribute->name)) {
43✔
123
                    case 'align':
43✔
124
                        $styles['alignment'] = self::mapAlign(trim($val), $bidi);
2✔
125

126
                        break;
2✔
127
                    case 'lang':
43✔
128
                        $styles['lang'] = $val;
1✔
129

130
                        break;
1✔
131
                    case 'width':
42✔
132
                        // tables, cells
133
                        $val = $val === 'auto' ? '100%' : $val;
9✔
134
                        if (false !== strpos($val, '%')) {
9✔
135
                            // e.g. <table width="100%"> or <td width="50%">
136
                            $styles['width'] = (int) $val * 50;
5✔
137
                            $styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT;
5✔
138
                        } else {
139
                            // e.g. <table width="250> where "250" = 250px (always pixels)
140
                            $styles['width'] = Converter::pixelToTwip(self::convertHtmlSize($val));
4✔
141
                            $styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP;
4✔
142
                        }
143

144
                        break;
9✔
145
                    case 'cellspacing':
37✔
146
                        // tables e.g. <table cellspacing="2">,  where "2" = 2px (always pixels)
147
                        $styles['cellSpacing'] = Converter::pixelToTwip(self::convertHtmlSize($val));
1✔
148

149
                        break;
1✔
150
                    case 'bgcolor':
37✔
151
                        // tables, rows, cells e.g. <tr bgColor="#FF0000">
152
                        $styles['bgColor'] = self::convertRgb($val);
3✔
153

154
                        break;
3✔
155
                    case 'valign':
36✔
156
                        // cells e.g. <td valign="middle">
157
                        if (preg_match('#(?:top|bottom|middle|baseline)#i', $val, $matches)) {
1✔
158
                            $styles['valign'] = self::mapAlignVertical($matches[0]);
1✔
159
                        }
160

161
                        break;
1✔
162
                }
163
            }
164

165
            $attributeIdentifier = $attributes->getNamedItem('id');
61✔
166
            if ($attributeIdentifier && self::$css) {
61✔
167
                $styles = self::parseStyleDeclarations(self::$css->getStyle('#' . $attributeIdentifier->nodeValue), $styles);
×
168
            }
169

170
            $attributeClass = $attributes->getNamedItem('class');
61✔
171
            if ($attributeClass) {
61✔
172
                if (self::$css) {
2✔
173
                    $styles = self::parseStyleDeclarations(self::$css->getStyle('.' . $attributeClass->nodeValue), $styles);
2✔
174
                }
175
                $styles['className'] = $attributeClass->nodeValue;
2✔
176
            }
177

178
            $attributeStyle = $attributes->getNamedItem('style');
61✔
179
            if ($attributeStyle) {
61✔
180
                $styles = self::parseStyle($attributeStyle, $styles);
31✔
181
            }
182
        }
183

184
        return $styles;
61✔
185
    }
186

187
    /**
188
     * Parse a node and add a corresponding element to the parent element.
189
     *
190
     * @param DOMNode $node node to parse
191
     * @param AbstractContainer $element object to add an element corresponding with the node
192
     * @param array $styles Array with all styles
193
     * @param array $data Array to transport data to a next level in the DOM tree, for example level of listitems
194
     */
195
    protected static function parseNode($node, $element, $styles = [], $data = []): void
196
    {
197
        if ($node->nodeName == 'style') {
63✔
198
            self::$css = new Css($node->textContent);
2✔
199
            self::$css->process();
2✔
200

201
            return;
2✔
202
        }
203

204
        // Populate styles array
205
        $styleTypes = ['font', 'paragraph', 'list', 'table', 'row', 'cell'];
63✔
206
        foreach ($styleTypes as $styleType) {
63✔
207
            if (!isset($styles[$styleType])) {
63✔
208
                $styles[$styleType] = [];
63✔
209
            }
210
        }
211

212
        // Node mapping table
213
        $nodes = [
63✔
214
            // $method               $node   $element    $styles     $data   $argument1      $argument2
215
            'p' => ['Paragraph',     $node,  $element,   $styles,    null,   null,           null],
63✔
216
            'h1' => ['Heading',      $node,  $element,   $styles,    null,   'Heading1',     null],
63✔
217
            'h2' => ['Heading',      $node,  $element,   $styles,    null,   'Heading2',     null],
63✔
218
            'h3' => ['Heading',      $node,  $element,   $styles,    null,   'Heading3',     null],
63✔
219
            'h4' => ['Heading',      $node,  $element,   $styles,    null,   'Heading4',     null],
63✔
220
            'h5' => ['Heading',      $node,  $element,   $styles,    null,   'Heading5',     null],
63✔
221
            'h6' => ['Heading',      $node,  $element,   $styles,    null,   'Heading6',     null],
63✔
222
            '#text' => ['Text',      $node,  $element,   $styles,    null,   null,           null],
63✔
223
            'strong' => ['Property', null,   null,       $styles,    null,   'bold',         true],
63✔
224
            'b' => ['Property',    null,   null,       $styles,    null,   'bold',         true],
63✔
225
            'em' => ['Property',    null,   null,       $styles,    null,   'italic',       true],
63✔
226
            'i' => ['Property',    null,   null,       $styles,    null,   'italic',       true],
63✔
227
            'u' => ['Property',    null,   null,       $styles,    null,   'underline',    'single'],
63✔
228
            'sup' => ['Property',    null,   null,       $styles,    null,   'superScript',  true],
63✔
229
            'sub' => ['Property',    null,   null,       $styles,    null,   'subScript',    true],
63✔
230
            'span' => ['Span',        $node,  null,       $styles,    null,   null,           null],
63✔
231
            'font' => ['Span',        $node,  null,       $styles,    null,   null,           null],
63✔
232
            'table' => ['Table',       $node,  $element,   $styles,    null,   null,           null],
63✔
233
            'tr' => ['Row',         $node,  $element,   $styles,    null,   null,           null],
63✔
234
            'td' => ['Cell',        $node,  $element,   $styles,    null,   null,           null],
63✔
235
            'th' => ['Cell',        $node,  $element,   $styles,    null,   null,           null],
63✔
236
            'ul' => ['List',        $node,  $element,   $styles,    $data,  null,           null],
63✔
237
            'ol' => ['List',        $node,  $element,   $styles,    $data,  null,           null],
63✔
238
            'li' => ['ListItem',    $node,  $element,   $styles,    $data,  null,           null],
63✔
239
            'img' => ['Image',       $node,  $element,   $styles,    null,   null,           null],
63✔
240
            'br' => ['LineBreak',   null,   $element,   $styles,    null,   null,           null],
63✔
241
            'a' => ['Link',        $node,  $element,   $styles,    null,   null,           null],
63✔
242
            'input' => ['Input',       $node,  $element,   $styles,    null,   null,           null],
63✔
243
            'hr' => ['HorizRule',   $node,  $element,   $styles,    null,   null,           null],
63✔
244
            'ruby' => ['Ruby',   $node,  $element,   $styles,    null,   null,           null],
63✔
245
        ];
63✔
246

247
        $newElement = null;
63✔
248
        $keys = ['node', 'element', 'styles', 'data', 'argument1', 'argument2'];
63✔
249

250
        if (isset($nodes[$node->nodeName])) {
63✔
251
            // Execute method based on node mapping table and return $newElement or null
252
            // Arguments are passed by reference
253
            $arguments = [];
63✔
254
            $args = [];
63✔
255
            [$method, $args[0], $args[1], $args[2], $args[3], $args[4], $args[5]] = $nodes[$node->nodeName];
63✔
256
            for ($i = 0; $i <= 5; ++$i) {
63✔
257
                if ($args[$i] !== null) {
63✔
258
                    $arguments[$keys[$i]] = &$args[$i];
63✔
259
                }
260
            }
261
            $method = "parse{$method}";
63✔
262
            $newElement = call_user_func_array(['PhpOffice\PhpWord\Shared\Html', $method], array_values($arguments));
63✔
263

264
            // Retrieve back variables from arguments
265
            foreach ($keys as $key) {
63✔
266
                if (array_key_exists($key, $arguments)) {
63✔
267
                    $$key = $arguments[$key];
63✔
268
                }
269
            }
270
        }
271

272
        if ($newElement === null) {
63✔
273
            $newElement = $element;
63✔
274
        }
275

276
        static::parseChildNodes($node, $newElement, $styles, $data);
63✔
277
    }
278

279
    /**
280
     * Parse child nodes.
281
     *
282
     * @param DOMNode $node
283
     * @param AbstractContainer|Row|Table $element
284
     * @param array $styles
285
     * @param array $data
286
     */
287
    protected static function parseChildNodes($node, $element, $styles, $data): void
288
    {
289
        if ('li' != $node->nodeName) {
63✔
290
            $cNodes = $node->childNodes;
63✔
291
            if (!empty($cNodes)) {
63✔
292
                foreach ($cNodes as $cNode) {
63✔
293
                    if ($element instanceof AbstractContainer || $element instanceof Table || $element instanceof Row) {
63✔
294
                        self::parseNode($cNode, $element, $styles, $data);
63✔
295
                    }
296
                }
297
            }
298
        }
299
    }
300

301
    /**
302
     * Parse paragraph node.
303
     *
304
     * @param DOMNode $node
305
     * @param AbstractContainer $element
306
     * @param array &$styles
307
     *
308
     * @return \PhpOffice\PhpWord\Element\PageBreak|TextRun
309
     */
310
    protected static function parseParagraph($node, $element, &$styles)
311
    {
312
        $styles['paragraph'] = self::recursiveParseStylesInHierarchy($node, $styles['paragraph']);
32✔
313
        if (isset($styles['paragraph']['isPageBreak']) && $styles['paragraph']['isPageBreak']) {
32✔
314
            return $element->addPageBreak();
1✔
315
        }
316

317
        return $element->addTextRun($styles['paragraph']);
31✔
318
    }
319

320
    /**
321
     * Parse input node.
322
     *
323
     * @param DOMNode $node
324
     * @param AbstractContainer $element
325
     * @param array &$styles
326
     */
327
    protected static function parseInput($node, $element, &$styles): void
328
    {
329
        $attributes = $node->attributes;
1✔
330
        if (null === $attributes->getNamedItem('type')) {
1✔
331
            return;
×
332
        }
333

334
        $inputType = $attributes->getNamedItem('type')->nodeValue;
1✔
335
        switch ($inputType) {
336
            case 'checkbox':
1✔
337
                $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->nodeValue === 'true' ? true : false;
1✔
338
                $textrun = $element->addTextRun($styles['paragraph']);
1✔
339
                $textrun->addFormField('checkbox')->setValue($checked);
1✔
340

341
                break;
1✔
342
        }
343
    }
344

345
    /**
346
     * Parse heading node.
347
     *
348
     * @param string $argument1 Name of heading style
349
     *
350
     * @todo Think of a clever way of defining header styles, now it is only based on the assumption, that
351
     * Heading1 - Heading6 are already defined somewhere
352
     */
353
    protected static function parseHeading(DOMNode $node, AbstractContainer $element, array &$styles, string $argument1): TextRun
354
    {
355
        $style = new Paragraph();
4✔
356
        $style->setStyleName($argument1);
4✔
357
        $style->setStyleByArray(self::parseInlineStyle($node, $styles['paragraph']));
4✔
358

359
        return $element->addTextRun($style);
4✔
360
    }
361

362
    /**
363
     * Parse text node.
364
     *
365
     * @param DOMNode $node
366
     * @param AbstractContainer $element
367
     * @param array &$styles
368
     */
369
    protected static function parseText($node, $element, &$styles): void
370
    {
371
        $styles['font'] = self::recursiveParseStylesInHierarchy($node, $styles['font']);
47✔
372

373
        //alignment applies on paragraph, not on font. Let's copy it there
374
        if (isset($styles['font']['alignment']) && is_array($styles['paragraph'])) {
47✔
375
            $styles['paragraph']['alignment'] = $styles['font']['alignment'];
9✔
376
        }
377

378
        if (is_callable([$element, 'addText'])) {
47✔
379
            $element->addText($node->nodeValue, $styles['font'], $styles['paragraph']);
47✔
380
        }
381
    }
382

383
    /**
384
     * Parse property node.
385
     *
386
     * @param array &$styles
387
     * @param string $argument1 Style name
388
     * @param string $argument2 Style value
389
     */
390
    protected static function parseProperty(&$styles, $argument1, $argument2): void
391
    {
392
        $styles['font'][$argument1] = $argument2;
6✔
393
    }
394

395
    /**
396
     * Parse span node.
397
     *
398
     * @param DOMNode $node
399
     * @param array &$styles
400
     */
401
    protected static function parseSpan($node, &$styles): void
402
    {
403
        self::parseInlineStyle($node, $styles['font']);
12✔
404
    }
405

406
    /**
407
     * Parse table node.
408
     *
409
     * @param DOMNode $node
410
     * @param AbstractContainer $element
411
     * @param array &$styles
412
     *
413
     * @return Table $element
414
     *
415
     * @todo As soon as TableItem, RowItem and CellItem support relative width and height
416
     */
417
    protected static function parseTable($node, $element, &$styles)
418
    {
419
        $elementStyles = self::parseInlineStyle($node, $styles['table']);
16✔
420

421
        $newElement = $element->addTable($elementStyles);
16✔
422

423
        // Add style name from CSS Class
424
        if (isset($elementStyles['className'])) {
16✔
425
            $newElement->getStyle()->setStyleName($elementStyles['className']);
1✔
426
        }
427

428
        $attributes = $node->attributes;
16✔
429
        if ($attributes->getNamedItem('border')) {
16✔
430
            $border = (int) $attributes->getNamedItem('border')->nodeValue;
1✔
431
            $newElement->getStyle()->setBorderSize(Converter::pixelToTwip($border));
1✔
432
        }
433

434
        return $newElement;
16✔
435
    }
436

437
    /**
438
     * Parse a table row.
439
     *
440
     * @param DOMNode $node
441
     * @param Table $element
442
     * @param array &$styles
443
     *
444
     * @return Row $element
445
     */
446
    protected static function parseRow($node, $element, &$styles)
447
    {
448
        $rowStyles = self::parseInlineStyle($node, $styles['row']);
16✔
449
        if ($node->parentNode->nodeName == 'thead') {
16✔
450
            $rowStyles['tblHeader'] = true;
2✔
451
        }
452

453
        // set cell height to control row heights
454
        $height = $rowStyles['height'] ?? null;
16✔
455
        unset($rowStyles['height']); // would not apply
16✔
456

457
        return $element->addRow($height, $rowStyles);
16✔
458
    }
459

460
    /**
461
     * Parse table cell.
462
     *
463
     * @param DOMNode $node
464
     * @param Table $element
465
     * @param array &$styles
466
     *
467
     * @return \PhpOffice\PhpWord\Element\Cell|TextRun $element
468
     */
469
    protected static function parseCell($node, $element, &$styles)
470
    {
471
        $cellStyles = self::recursiveParseStylesInHierarchy($node, $styles['cell']);
16✔
472

473
        $colspan = $node->getAttribute('colspan');
16✔
474
        if (!empty($colspan)) {
16✔
475
            $cellStyles['gridSpan'] = $colspan - 0;
2✔
476
        }
477

478
        // set cell width to control column widths
479
        $width = $cellStyles['width'] ?? null;
16✔
480
        unset($cellStyles['width']); // would not apply
16✔
481
        $cell = $element->addCell($width, $cellStyles);
16✔
482

483
        if (self::shouldAddTextRun($node)) {
16✔
484
            return $cell->addTextRun(self::filterOutNonInheritedStyles(self::parseInlineStyle($node, $styles['paragraph'])));
16✔
485
        }
486

487
        return $cell;
3✔
488
    }
489

490
    /**
491
     * Checks if $node contains an HTML element that cannot be added to TextRun.
492
     *
493
     * @return bool Returns true if the node contains an HTML element that cannot be added to TextRun
494
     */
495
    protected static function shouldAddTextRun(DOMNode $node)
496
    {
497
        $containsBlockElement = self::$xpath->query('.//table|./p|./ul|./ol|./h1|./h2|./h3|./h4|./h5|./h6', $node)->length > 0;
16✔
498
        if ($containsBlockElement) {
16✔
499
            return false;
3✔
500
        }
501

502
        return true;
16✔
503
    }
504

505
    /**
506
     * Recursively parses styles on parent nodes
507
     * TODO if too slow, add caching of parent nodes, !! everything is static here so watch out for concurrency !!
508
     */
509
    protected static function recursiveParseStylesInHierarchy(DOMNode $node, array $style)
510
    {
511
        $parentStyle = [];
61✔
512
        if ($node->parentNode != null && XML_ELEMENT_NODE == $node->parentNode->nodeType) {
61✔
513
            $parentStyle = self::recursiveParseStylesInHierarchy($node->parentNode, []);
61✔
514
        }
515
        if ($node->nodeName === '#text') {
61✔
516
            $parentStyle = array_merge($parentStyle, $style);
47✔
517
        } else {
518
            $parentStyle = self::filterOutNonInheritedStyles($parentStyle);
61✔
519
        }
520
        $style = self::parseInlineStyle($node, $parentStyle);
61✔
521

522
        return $style;
61✔
523
    }
524

525
    /**
526
     * Removes non-inherited styles from array.
527
     */
528
    protected static function filterOutNonInheritedStyles(array $styles)
529
    {
530
        $nonInheritedStyles = [
61✔
531
            'borderSize',
61✔
532
            'borderTopSize',
61✔
533
            'borderRightSize',
61✔
534
            'borderBottomSize',
61✔
535
            'borderLeftSize',
61✔
536
            'borderColor',
61✔
537
            'borderTopColor',
61✔
538
            'borderRightColor',
61✔
539
            'borderBottomColor',
61✔
540
            'borderLeftColor',
61✔
541
            'borderStyle',
61✔
542
            'spaceAfter',
61✔
543
            'spaceBefore',
61✔
544
            'underline',
61✔
545
            'strikethrough',
61✔
546
            'hidden',
61✔
547
        ];
61✔
548

549
        $styles = array_diff_key($styles, array_flip($nonInheritedStyles));
61✔
550

551
        return $styles;
61✔
552
    }
553

554
    /**
555
     * Parse list node.
556
     *
557
     * @param DOMNode $node
558
     * @param AbstractContainer $element
559
     * @param array &$styles
560
     * @param array &$data
561
     */
562
    protected static function parseList($node, $element, &$styles, &$data)
563
    {
564
        $isOrderedList = $node->nodeName === 'ol';
7✔
565
        if (isset($data['listdepth'])) {
7✔
566
            ++$data['listdepth'];
3✔
567
        } else {
568
            $data['listdepth'] = 0;
7✔
569
            $styles['list'] = 'listStyle_' . self::$listIndex++;
7✔
570
            $style = $element->getPhpWord()->addNumberingStyle($styles['list'], self::getListStyle($isOrderedList));
7✔
571

572
            // extract attributes start & type e.g. <ol type="A" start="3">
573
            $start = 0;
7✔
574
            $type = '';
7✔
575
            foreach ($node->attributes as $attribute) {
7✔
576
                switch ($attribute->name) {
1✔
577
                    case 'start':
1✔
578
                        $start = (int) $attribute->value;
1✔
579

580
                        break;
1✔
581
                    case 'type':
1✔
582
                        $type = $attribute->value;
1✔
583

584
                        break;
1✔
585
                }
586
            }
587

588
            $levels = $style->getLevels();
7✔
589
            /** @var \PhpOffice\PhpWord\Style\NumberingLevel */
590
            $level = $levels[0];
7✔
591
            if ($start > 0) {
7✔
592
                $level->setStart($start);
1✔
593
            }
594
            $type = $type ? self::mapListType($type) : null;
7✔
595
            if ($type) {
7✔
596
                $level->setFormat($type);
1✔
597
            }
598
        }
599
        if ($node->parentNode->nodeName === 'li') {
7✔
600
            return $element->getParent();
1✔
601
        }
602
    }
603

604
    /**
605
     * @param bool $isOrderedList
606
     *
607
     * @return array
608
     */
609
    protected static function getListStyle($isOrderedList)
610
    {
611
        if ($isOrderedList) {
7✔
612
            return [
5✔
613
                'type' => 'multilevel',
5✔
614
                'levels' => [
5✔
615
                    ['format' => NumberFormat::DECIMAL,      'text' => '%1.', 'alignment' => 'left',  'tabPos' => 720,  'left' => 720,  'hanging' => 360],
5✔
616
                    ['format' => NumberFormat::LOWER_LETTER, 'text' => '%2.', 'alignment' => 'left',  'tabPos' => 1440, 'left' => 1440, 'hanging' => 360],
5✔
617
                    ['format' => NumberFormat::LOWER_ROMAN,  'text' => '%3.', 'alignment' => 'right', 'tabPos' => 2160, 'left' => 2160, 'hanging' => 180],
5✔
618
                    ['format' => NumberFormat::DECIMAL,      'text' => '%4.', 'alignment' => 'left',  'tabPos' => 2880, 'left' => 2880, 'hanging' => 360],
5✔
619
                    ['format' => NumberFormat::LOWER_LETTER, 'text' => '%5.', 'alignment' => 'left',  'tabPos' => 3600, 'left' => 3600, 'hanging' => 360],
5✔
620
                    ['format' => NumberFormat::LOWER_ROMAN,  'text' => '%6.', 'alignment' => 'right', 'tabPos' => 4320, 'left' => 4320, 'hanging' => 180],
5✔
621
                    ['format' => NumberFormat::DECIMAL,      'text' => '%7.', 'alignment' => 'left',  'tabPos' => 5040, 'left' => 5040, 'hanging' => 360],
5✔
622
                    ['format' => NumberFormat::LOWER_LETTER, 'text' => '%8.', 'alignment' => 'left',  'tabPos' => 5760, 'left' => 5760, 'hanging' => 360],
5✔
623
                    ['format' => NumberFormat::LOWER_ROMAN,  'text' => '%9.', 'alignment' => 'right', 'tabPos' => 6480, 'left' => 6480, 'hanging' => 180],
5✔
624
                ],
5✔
625
            ];
5✔
626
        }
627

628
        return [
4✔
629
            'type' => 'hybridMultilevel',
4✔
630
            'levels' => [
4✔
631
                ['format' => NumberFormat::BULLET, 'text' => '•', 'alignment' => 'left', 'tabPos' => 720,  'left' => 720,  'hanging' => 360, 'font' => 'Symbol',      'hint' => 'default'],
4✔
632
                ['format' => NumberFormat::BULLET, 'text' => 'â—¦',  'alignment' => 'left', 'tabPos' => 1440, 'left' => 1440, 'hanging' => 360, 'font' => 'Courier New', 'hint' => 'default'],
4✔
633
                ['format' => NumberFormat::BULLET, 'text' => '•', 'alignment' => 'left', 'tabPos' => 2160, 'left' => 2160, 'hanging' => 360, 'font' => 'Wingdings',   'hint' => 'default'],
4✔
634
                ['format' => NumberFormat::BULLET, 'text' => '•', 'alignment' => 'left', 'tabPos' => 2880, 'left' => 2880, 'hanging' => 360, 'font' => 'Symbol',      'hint' => 'default'],
4✔
635
                ['format' => NumberFormat::BULLET, 'text' => 'â—¦',  'alignment' => 'left', 'tabPos' => 3600, 'left' => 3600, 'hanging' => 360, 'font' => 'Courier New', 'hint' => 'default'],
4✔
636
                ['format' => NumberFormat::BULLET, 'text' => '•', 'alignment' => 'left', 'tabPos' => 4320, 'left' => 4320, 'hanging' => 360, 'font' => 'Wingdings',   'hint' => 'default'],
4✔
637
                ['format' => NumberFormat::BULLET, 'text' => '•', 'alignment' => 'left', 'tabPos' => 5040, 'left' => 5040, 'hanging' => 360, 'font' => 'Symbol',      'hint' => 'default'],
4✔
638
                ['format' => NumberFormat::BULLET, 'text' => 'â—¦',  'alignment' => 'left', 'tabPos' => 5760, 'left' => 5760, 'hanging' => 360, 'font' => 'Courier New', 'hint' => 'default'],
4✔
639
                ['format' => NumberFormat::BULLET, 'text' => '•', 'alignment' => 'left', 'tabPos' => 6480, 'left' => 6480, 'hanging' => 360, 'font' => 'Wingdings',   'hint' => 'default'],
4✔
640
            ],
4✔
641
        ];
4✔
642
    }
643

644
    /**
645
     * Parse list item node.
646
     *
647
     * @param DOMNode $node
648
     * @param AbstractContainer $element
649
     * @param array &$styles
650
     * @param array $data
651
     *
652
     * @todo This function is almost the same like `parseChildNodes`. Merged?
653
     * @todo As soon as ListItem inherits from AbstractContainer or TextRun delete parsing part of childNodes
654
     */
655
    protected static function parseListItem($node, $element, &$styles, $data): void
656
    {
657
        $cNodes = $node->childNodes;
7✔
658
        if (!empty($cNodes)) {
7✔
659
            $listRun = $element->addListItemRun($data['listdepth'], $styles['list'], $styles['paragraph']);
7✔
660
            foreach ($cNodes as $cNode) {
7✔
661
                self::parseNode($cNode, $listRun, $styles, $data);
7✔
662
            }
663
        }
664
    }
665

666
    /**
667
     * Parse style.
668
     *
669
     * @param DOMAttr $attribute
670
     * @param array $styles
671
     *
672
     * @return array
673
     */
674
    protected static function parseStyle($attribute, $styles)
675
    {
676
        $properties = explode(';', trim($attribute->value, " \t\n\r\0\x0B;"));
32✔
677

678
        $selectors = [];
32✔
679
        foreach ($properties as $property) {
32✔
680
            [$cKey, $cValue] = array_pad(explode(':', $property, 2), 2, null);
32✔
681
            $selectors[strtolower(trim($cKey))] = trim($cValue ?? '');
32✔
682
        }
683

684
        return self::parseStyleDeclarations($selectors, $styles);
32✔
685
    }
686

687
    protected static function parseStyleDeclarations(array $selectors, array $styles)
688
    {
689
        $bidi = ($selectors['direction'] ?? '') === 'rtl';
34✔
690
        foreach ($selectors as $property => $value) {
34✔
691
            switch ($property) {
692
                case 'text-decoration':
34✔
693
                    switch ($value) {
694
                        case 'underline':
4✔
695
                            $styles['underline'] = 'single';
3✔
696

697
                            break;
3✔
698
                        case 'line-through':
1✔
699
                            $styles['strikethrough'] = true;
1✔
700

701
                            break;
1✔
702
                    }
703

704
                    break;
4✔
705
                case 'text-align':
32✔
706
                    $styles['alignment'] = self::mapAlign($value, $bidi);
8✔
707

708
                    break;
8✔
709
                case 'ruby-align':
31✔
710
                    $styles['rubyAlignment'] = self::mapRubyAlign($value);
1✔
711

712
                    break;
1✔
713
                case 'display':
31✔
714
                    $styles['hidden'] = $value === 'none' || $value === 'hidden';
1✔
715

716
                    break;
1✔
717
                case 'direction':
30✔
718
                    $styles['rtl'] = $value === 'rtl';
3✔
719
                    $styles['bidi'] = $value === 'rtl';
3✔
720

721
                    break;
3✔
722
                case 'font-size':
27✔
723
                    $styles['size'] = Converter::cssToPoint($value);
6✔
724

725
                    break;
6✔
726
                case 'font-family':
24✔
727
                    $value = array_map('trim', explode(',', $value));
5✔
728
                    $styles['name'] = ucwords($value[0]);
5✔
729

730
                    break;
5✔
731
                case 'color':
20✔
732
                    $styles['color'] = self::convertRgb($value);
5✔
733

734
                    break;
5✔
735
                case 'background-color':
18✔
736
                    $styles['bgColor'] = self::convertRgb($value);
5✔
737

738
                    break;
5✔
739
                case 'line-height':
17✔
740
                    $matches = [];
2✔
741
                    if ($value === 'normal' || $value === 'inherit') {
2✔
742
                        $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO;
1✔
743
                        $spacing = 0;
1✔
744
                    } elseif (preg_match('/([0-9]+\.?[0-9]*[a-z]+)/', $value, $matches)) {
2✔
745
                        //matches number with a unit, e.g. 12px, 15pt, 20mm, ...
746
                        $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::EXACT;
2✔
747
                        $spacing = Converter::cssToTwip($matches[1]);
2✔
748
                    } elseif (preg_match('/([0-9]+)%/', $value, $matches)) {
1✔
749
                        //matches percentages
750
                        $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO;
1✔
751
                        //we are subtracting 1 line height because the Spacing writer is adding one line
752
                        $spacing = ((((int) $matches[1]) / 100) * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT;
1✔
753
                    } else {
754
                        //any other, wich is a multiplier. E.g. 1.2
755
                        $spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO;
1✔
756
                        //we are subtracting 1 line height because the Spacing writer is adding one line
757
                        $spacing = ($value * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT;
1✔
758
                    }
759
                    $styles['spacingLineRule'] = $spacingLineRule;
2✔
760
                    $styles['line-spacing'] = $spacing;
2✔
761

762
                    break;
2✔
763
                case 'letter-spacing':
15✔
764
                    $styles['letter-spacing'] = Converter::cssToTwip($value);
1✔
765

766
                    break;
1✔
767
                case 'text-indent':
14✔
768
                    $styles['indentation']['firstLine'] = Converter::cssToTwip($value);
1✔
769

770
                    break;
1✔
771
                case 'font-weight':
13✔
772
                    $tValue = false;
3✔
773
                    if (preg_match('#bold#', $value)) {
3✔
774
                        $tValue = true; // also match bolder
3✔
775
                    }
776
                    $styles['bold'] = $tValue;
3✔
777

778
                    break;
3✔
779
                case 'font-style':
12✔
780
                    $tValue = false;
1✔
781
                    if (preg_match('#(?:italic|oblique)#', $value)) {
1✔
782
                        $tValue = true;
1✔
783
                    }
784
                    $styles['italic'] = $tValue;
1✔
785

786
                    break;
1✔
787
                case 'font-variant':
11✔
788
                    $tValue = false;
1✔
789
                    if (preg_match('#small-caps#', $value)) {
1✔
790
                        $tValue = true;
1✔
791
                    }
792
                    $styles['smallCaps'] = $tValue;
1✔
793

794
                    break;
1✔
795
                case 'margin':
10✔
796
                    $value = Converter::cssToTwip($value);
×
797
                    $styles['spaceBefore'] = $value;
×
798
                    $styles['spaceAfter'] = $value;
×
799

800
                    break;
×
801
                case 'margin-top':
10✔
802
                    // BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($value)
803
                    $styles['spaceBefore'] = Converter::cssToTwip($value);
2✔
804

805
                    break;
2✔
806
                case 'margin-bottom':
10✔
807
                    // BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($value)
808
                    $styles['spaceAfter'] = Converter::cssToTwip($value);
2✔
809

810
                    break;
2✔
811

812
                case 'padding':
9✔
813
                    $valueTop = $valueRight = $valueBottom = $valueLeft = null;
1✔
814
                    $cValue = preg_replace('# +#', ' ', trim($value));
1✔
815
                    $paddingArr = explode(' ', $cValue);
1✔
816
                    $countParams = count($paddingArr);
1✔
817
                    if ($countParams == 1) {
1✔
818
                        $valueTop = $valueRight = $valueBottom = $valueLeft = $paddingArr[0];
×
819
                    } elseif ($countParams == 2) {
1✔
820
                        $valueTop = $valueBottom = $paddingArr[0];
×
821
                        $valueRight = $valueLeft = $paddingArr[1];
×
822
                    } elseif ($countParams == 3) {
1✔
823
                        $valueTop = $paddingArr[0];
×
824
                        $valueRight = $valueLeft = $paddingArr[1];
×
825
                        $valueBottom = $paddingArr[2];
×
826
                    } elseif ($countParams == 4) {
1✔
827
                        $valueTop = $paddingArr[0];
1✔
828
                        $valueRight = $paddingArr[1];
1✔
829
                        $valueBottom = $paddingArr[2];
1✔
830
                        $valueLeft = $paddingArr[3];
1✔
831
                    }
832
                    if ($valueTop !== null) {
1✔
833
                        $styles['paddingTop'] = Converter::cssToTwip($valueTop);
1✔
834
                    }
835
                    if ($valueRight !== null) {
1✔
836
                        $styles['paddingRight'] = Converter::cssToTwip($valueRight);
1✔
837
                    }
838
                    if ($valueBottom !== null) {
1✔
839
                        $styles['paddingBottom'] = Converter::cssToTwip($valueBottom);
1✔
840
                    }
841
                    if ($valueLeft !== null) {
1✔
842
                        $styles['paddingLeft'] = Converter::cssToTwip($valueLeft);
1✔
843
                    }
844

845
                    break;
1✔
846
                case 'padding-top':
9✔
847
                    $styles['paddingTop'] = Converter::cssToTwip($value);
1✔
848

849
                    break;
1✔
850
                case 'padding-right':
9✔
851
                    $styles['paddingRight'] = Converter::cssToTwip($value);
1✔
852

853
                    break;
1✔
854
                case 'padding-bottom':
9✔
855
                    $styles['paddingBottom'] = Converter::cssToTwip($value);
1✔
856

857
                    break;
1✔
858
                case 'padding-left':
9✔
859
                    $styles['paddingLeft'] = Converter::cssToTwip($value);
1✔
860

861
                    break;
1✔
862

863
                case 'border-color':
8✔
864
                    self::mapBorderColor($styles, $value);
1✔
865

866
                    break;
1✔
867
                case 'border-width':
8✔
868
                    $styles['borderSize'] = Converter::cssToPoint($value);
1✔
869

870
                    break;
1✔
871
                case 'border-style':
8✔
872
                    $styles['borderStyle'] = self::mapBorderStyle($value);
1✔
873

874
                    break;
1✔
875
                case 'width':
8✔
876
                    if (preg_match('/([0-9]+[a-z]+)/', $value, $matches)) {
3✔
877
                        $styles['width'] = Converter::cssToTwip($matches[1]);
2✔
878
                        $styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP;
2✔
879
                    } elseif (preg_match('/([0-9]+)%/', $value, $matches)) {
3✔
880
                        $styles['width'] = $matches[1] * 50;
3✔
881
                        $styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT;
3✔
882
                    } elseif (preg_match('/([0-9]+)/', $value, $matches)) {
1✔
883
                        $styles['width'] = $matches[1];
1✔
884
                        $styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::AUTO;
1✔
885
                    }
886

887
                    break;
3✔
888
                case 'height':
7✔
889
                    $styles['height'] = Converter::cssToTwip($value);
1✔
890
                    $styles['exactHeight'] = true;
1✔
891

892
                    break;
1✔
893
                case 'border':
6✔
894
                case 'border-top':
4✔
895
                case 'border-bottom':
4✔
896
                case 'border-right':
3✔
897
                case 'border-left':
3✔
898
                    // must have exact order [width color style], e.g. "1px #0011CC solid" or "2pt green solid"
899
                    // Word does not accept shortened hex colors e.g. #CCC, only full e.g. #CCCCCC
900
                    if (preg_match('/([0-9]+[^0-9]*)\s+(\#[a-fA-F0-9]+|[a-zA-Z]+)\s+([a-z]+)/', $value, $matches)) {
4✔
901
                        if (false !== strpos($property, '-')) {
4✔
902
                            $tmp = explode('-', $property);
1✔
903
                            $which = $tmp[1];
1✔
904
                            $which = ucfirst($which); // e.g. bottom -> Bottom
1✔
905
                        } else {
906
                            $which = '';
3✔
907
                        }
908
                        // Note - border width normalization:
909
                        // Width of border in Word is calculated differently than HTML borders, usually showing up too bold.
910
                        // Smallest 1px (or 1pt) appears in Word like 2-3px/pt in HTML once converted to twips.
911
                        // Therefore we need to normalize converted twip value to cca 1/2 of value.
912
                        // This may be adjusted, if better ratio or formula found.
913
                        // BC change: up to ver. 0.17.0 was $size converted to points - Converter::cssToPoint($size)
914
                        $size = Converter::cssToTwip($matches[1]);
4✔
915
                        $size = (int) ($size / 2);
4✔
916
                        // valid variants may be e.g. borderSize, borderTopSize, borderLeftColor, etc ..
917
                        $styles["border{$which}Size"] = $size; // twips
4✔
918
                        $styles["border{$which}Color"] = trim($matches[2], '#');
4✔
919
                        $styles["border{$which}Style"] = self::mapBorderStyle($matches[3]);
4✔
920
                    }
921

922
                    break;
4✔
923
                case 'vertical-align':
3✔
924
                    // https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align
925
                    if (preg_match('#(?:top|bottom|middle|sub|baseline)#i', $value, $matches)) {
1✔
926
                        $styles['valign'] = self::mapAlignVertical($matches[0]);
1✔
927
                    }
928

929
                    break;
1✔
930
                case 'page-break-after':
2✔
931
                    if ($value == 'always') {
1✔
932
                        $styles['isPageBreak'] = true;
1✔
933
                    }
934

935
                    break;
1✔
936
            }
937
        }
938

939
        return $styles;
34✔
940
    }
941

942
    /**
943
     * Parse image node.
944
     *
945
     * @param DOMNode $node
946
     * @param AbstractContainer $element
947
     *
948
     * @return \PhpOffice\PhpWord\Element\Image
949
     */
950
    protected static function parseImage($node, $element)
951
    {
952
        $style = [];
9✔
953
        $src = null;
9✔
954
        foreach ($node->attributes as $attribute) {
9✔
955
            switch ($attribute->name) {
9✔
956
                case 'src':
9✔
957
                    $src = $attribute->value;
9✔
958

959
                    break;
9✔
960
                case 'width':
9✔
961
                    $style['width'] = self::convertHtmlSize($attribute->value);
9✔
962
                    $style['unit'] = \PhpOffice\PhpWord\Style\Image::UNIT_PX;
9✔
963

964
                    break;
9✔
965
                case 'height':
9✔
966
                    $style['height'] = self::convertHtmlSize($attribute->value);
9✔
967
                    $style['unit'] = \PhpOffice\PhpWord\Style\Image::UNIT_PX;
9✔
968

969
                    break;
9✔
970
                case 'style':
6✔
971
                    $styleattr = explode(';', $attribute->value);
5✔
972
                    foreach ($styleattr as $attr) {
5✔
973
                        if (strpos($attr, ':')) {
5✔
974
                            [$k, $v] = explode(':', $attr);
5✔
975
                            switch ($k) {
976
                                case 'float':
5✔
977
                                    if (trim($v) == 'right') {
5✔
978
                                        $style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_RIGHT;
5✔
979
                                        $style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_MARGIN; // inner section area
5✔
980
                                        $style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
5✔
981
                                        $style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
5✔
982
                                        $style['overlap'] = true;
5✔
983
                                    }
984
                                    if (trim($v) == 'left') {
5✔
985
                                        $style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_LEFT;
3✔
986
                                        $style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_MARGIN; // inner section area
3✔
987
                                        $style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
3✔
988
                                        $style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
3✔
989
                                        $style['overlap'] = true;
3✔
990
                                    }
991

992
                                    break;
5✔
993
                            }
994
                        }
995
                    }
996

997
                    break;
5✔
998
            }
999
        }
1000
        $originSrc = $src;
9✔
1001
        if (strpos($src, 'data:image') !== false) {
9✔
1002
            $tmpDir = Settings::getTempDir() . '/';
1✔
1003

1004
            $match = [];
1✔
1005
            preg_match('/data:image\/(\w+);base64,(.+)/', $src, $match);
1✔
1006
            if (!empty($match)) {
1✔
1007
                $src = $imgFile = $tmpDir . uniqid() . '.' . $match[1];
1✔
1008

1009
                $ifp = fopen($imgFile, 'wb');
1✔
1010

1011
                if ($ifp !== false) {
1✔
1012
                    fwrite($ifp, base64_decode($match[2]));
1✔
1013
                    fclose($ifp);
1✔
1014
                }
1015
            }
1016
        }
1017
        $src = urldecode($src);
9✔
1018

1019
        if (!is_file($src)
9✔
1020
            && null !== self::$options
9✔
1021
            && isset(self::$options['IMG_SRC_SEARCH'], self::$options['IMG_SRC_REPLACE'])
9✔
1022
        ) {
1023
            $src = str_replace(self::$options['IMG_SRC_SEARCH'], self::$options['IMG_SRC_REPLACE'], $src);
1✔
1024
        }
1025

1026
        if (!is_file($src)) {
9✔
1027
            if ($imgBlob = @file_get_contents($src)) {
3✔
1028
                $tmpDir = Settings::getTempDir() . '/';
3✔
1029
                $match = [];
3✔
1030
                preg_match('/.+\.(\w+)$/', $src, $match);
3✔
1031
                $src = $tmpDir . uniqid();
3✔
1032
                if (isset($match[1])) {
3✔
1033
                    $src .= '.' . $match[1];
2✔
1034
                }
1035

1036
                $ifp = fopen($src, 'wb');
3✔
1037

1038
                if ($ifp !== false) {
3✔
1039
                    fwrite($ifp, $imgBlob);
3✔
1040
                    fclose($ifp);
3✔
1041
                }
1042
            }
1043
        }
1044

1045
        if (is_file($src)) {
9✔
1046
            $newElement = $element->addImage($src, $style);
9✔
1047
        } else {
1048
            throw new Exception("Could not load image $originSrc");
×
1049
        }
1050

1051
        return $newElement;
8✔
1052
    }
1053

1054
    /**
1055
     * Transforms a CSS border style into a word border style.
1056
     *
1057
     * @param string $cssBorderStyle
1058
     *
1059
     * @return null|string
1060
     */
1061
    protected static function mapBorderStyle($cssBorderStyle)
1062
    {
1063
        switch ($cssBorderStyle) {
1064
            case 'none':
4✔
1065
            case 'dashed':
4✔
1066
            case 'dotted':
4✔
1067
            case 'double':
4✔
1068
                return $cssBorderStyle;
2✔
1069
            default:
1070
                return 'single';
3✔
1071
        }
1072
    }
1073

1074
    protected static function mapBorderColor(&$styles, $cssBorderColor): void
1075
    {
1076
        $numColors = substr_count($cssBorderColor, '#');
1✔
1077
        if ($numColors === 1) {
1✔
1078
            $styles['borderColor'] = trim($cssBorderColor, '#');
1✔
1079
        } elseif ($numColors > 1) {
1✔
1080
            $colors = explode(' ', $cssBorderColor);
1✔
1081
            $borders = ['borderTopColor', 'borderRightColor', 'borderBottomColor', 'borderLeftColor'];
1✔
1082
            for ($i = 0; $i < min(4, $numColors, count($colors)); ++$i) {
1✔
1083
                $styles[$borders[$i]] = trim($colors[$i], '#');
1✔
1084
            }
1085
        }
1086
    }
1087

1088
    /**
1089
     * Transforms a HTML/CSS alignment into a \PhpOffice\PhpWord\SimpleType\Jc.
1090
     *
1091
     * @param string $cssAlignment
1092
     * @param bool $bidi
1093
     *
1094
     * @return null|string
1095
     */
1096
    protected static function mapAlign($cssAlignment, $bidi)
1097
    {
1098
        switch ($cssAlignment) {
1099
            case 'right':
9✔
1100
                return $bidi ? Jc::START : Jc::END;
1✔
1101
            case 'center':
9✔
1102
                return Jc::CENTER;
7✔
1103
            case 'justify':
4✔
1104
                return Jc::BOTH;
1✔
1105
            default:
1106
                return $bidi ? Jc::END : Jc::START;
4✔
1107
        }
1108
    }
1109

1110
    /**
1111
     * Transforms a HTML/CSS ruby alignment into a \PhpOffice\PhpWord\SimpleType\Jc.
1112
     */
1113
    protected static function mapRubyAlign(string $cssRubyAlignment): string
1114
    {
1115
        switch ($cssRubyAlignment) {
1116
            case 'center':
1✔
1117
                return RubyProperties::ALIGNMENT_CENTER;
1✔
1118
            case 'start':
×
1119
                return RubyProperties::ALIGNMENT_LEFT;
×
1120
            case 'space-between':
×
1121
                return RubyProperties::ALIGNMENT_DISTRIBUTE_SPACE;
×
1122
            default:
1123
                return '';
×
1124
        }
1125
    }
1126

1127
    /**
1128
     * Transforms a HTML/CSS vertical alignment.
1129
     *
1130
     * @param string $alignment
1131
     *
1132
     * @return null|string
1133
     */
1134
    protected static function mapAlignVertical($alignment)
1135
    {
1136
        $alignment = strtolower($alignment);
1✔
1137
        switch ($alignment) {
1138
            case 'top':
1✔
1139
            case 'baseline':
1✔
1140
            case 'bottom':
1✔
1141
                return $alignment;
1✔
1142
            case 'middle':
1✔
1143
                return 'center';
1✔
1144
            case 'sub':
×
1145
                return 'bottom';
×
1146
            case 'text-top':
×
1147
            case 'baseline':
×
1148
                return 'top';
×
1149
            default:
1150
                // @discuss - which one should apply:
1151
                // - Word uses default vert. alignment: top
1152
                // - all browsers use default vert. alignment: middle
1153
                // Returning empty string means attribute wont be set so use Word default (top).
1154
                return '';
×
1155
        }
1156
    }
1157

1158
    /**
1159
     * Map list style for ordered list.
1160
     *
1161
     * @param string $cssListType
1162
     */
1163
    protected static function mapListType($cssListType)
1164
    {
1165
        switch ($cssListType) {
1166
            case 'a':
1✔
1167
                return NumberFormat::LOWER_LETTER; // a, b, c, ..
×
1168
            case 'A':
1✔
1169
                return NumberFormat::UPPER_LETTER; // A, B, C, ..
1✔
1170
            case 'i':
1✔
1171
                return NumberFormat::LOWER_ROMAN; // i, ii, iii, iv, ..
1✔
1172
            case 'I':
×
1173
                return NumberFormat::UPPER_ROMAN; // I, II, III, IV, ..
×
1174
            case '1':
×
1175
            default:
1176
                return NumberFormat::DECIMAL; // 1, 2, 3, ..
×
1177
        }
1178
    }
1179

1180
    /**
1181
     * Parse line break.
1182
     *
1183
     * @param AbstractContainer $element
1184
     */
1185
    protected static function parseLineBreak($element): void
1186
    {
1187
        $element->addTextBreak();
2✔
1188
    }
1189

1190
    /**
1191
     * Parse link node.
1192
     *
1193
     * @param DOMNode $node
1194
     * @param AbstractContainer $element
1195
     * @param array $styles
1196
     */
1197
    protected static function parseLink($node, $element, &$styles)
1198
    {
1199
        $target = null;
3✔
1200
        foreach ($node->attributes as $attribute) {
3✔
1201
            switch ($attribute->name) {
3✔
1202
                case 'href':
3✔
1203
                    $target = $attribute->value;
3✔
1204

1205
                    break;
3✔
1206
            }
1207
        }
1208
        $styles['font'] = self::parseInlineStyle($node, $styles['font']);
3✔
1209

1210
        if (empty($target)) {
3✔
1211
            $target = '#';
1✔
1212
        }
1213

1214
        if (strpos($target, '#') === 0 && strlen($target) > 1) {
3✔
1215
            return $element->addLink(substr($target, 1), $node->textContent, $styles['font'], $styles['paragraph'], true);
1✔
1216
        }
1217

1218
        return $element->addLink($target, $node->textContent, $styles['font'], $styles['paragraph']);
2✔
1219
    }
1220

1221
    /**
1222
     * Render horizontal rule
1223
     * Note: Word rule is not the same as HTML's <hr> since it does not support width and thus neither alignment.
1224
     *
1225
     * @param DOMNode $node
1226
     * @param AbstractContainer $element
1227
     */
1228
    protected static function parseHorizRule($node, $element): void
1229
    {
1230
        $styles = self::parseInlineStyle($node);
1✔
1231

1232
        // <hr> is implemented as an empty paragraph - extending 100% inside the section
1233
        // Some properties may be controlled, e.g. <hr style="border-bottom: 3px #DDDDDD solid; margin-bottom: 0;">
1234

1235
        $fontStyle = $styles + ['size' => 3];
1✔
1236

1237
        $paragraphStyle = $styles + [
1✔
1238
            'lineHeight' => 0.25, // multiply default line height - e.g. 1, 1.5 etc
1✔
1239
            'spacing' => 0, // twip
1✔
1240
            'spaceBefore' => 120, // twip, 240/2 (default line height)
1✔
1241
            'spaceAfter' => 120, // twip
1✔
1242
            'borderBottomSize' => empty($styles['line-height']) ? 1 : $styles['line-height'],
1✔
1243
            'borderBottomColor' => empty($styles['color']) ? '000000' : $styles['color'],
1✔
1244
            'borderBottomStyle' => 'single', // same as "solid"
1✔
1245
        ];
1✔
1246

1247
        $element->addText('', $fontStyle, $paragraphStyle);
1✔
1248

1249
        // Notes: <hr/> cannot be:
1250
        // - table - throws error "cannot be inside textruns", e.g. lists
1251
        // - line - that is a shape, has different behaviour
1252
        // - repeated text, e.g. underline "_", because of unpredictable line wrapping
1253
    }
1254

1255
    /**
1256
     * Parse ruby node.
1257
     *
1258
     * @param DOMNode $node
1259
     * @param AbstractContainer $element
1260
     * @param array $styles
1261
     */
1262
    protected static function parseRuby($node, $element, &$styles)
1263
    {
1264
        $rubyProperties = new RubyProperties();
1✔
1265
        $baseTextRun = new TextRun($styles['paragraph']);
1✔
1266
        $rubyTextRun = new TextRun(null);
1✔
1267
        if ($node->hasAttributes()) {
1✔
1268
            $langAttr = $node->attributes->getNamedItem('lang');
1✔
1269
            if ($langAttr !== null) {
1✔
1270
                $rubyProperties->setLanguageId($langAttr->textContent);
1✔
1271
            }
1272
            $styleAttr = $node->attributes->getNamedItem('style');
1✔
1273
            if ($styleAttr !== null) {
1✔
1274
                $styles = self::parseStyle($styleAttr, $styles['paragraph']);
1✔
1275
                if (isset($styles['rubyAlignment']) && $styles['rubyAlignment'] !== '') {
1✔
1276
                    $rubyProperties->setAlignment($styles['rubyAlignment']);
1✔
1277
                }
1278
                if (isset($styles['size']) && $styles['size'] !== '') {
1✔
1279
                    $rubyProperties->setFontSizeForBaseText($styles['size']);
1✔
1280
                }
1281
                $baseTextRun->setParagraphStyle($styles);
1✔
1282
            }
1283
        }
1284
        foreach ($node->childNodes as $child) {
1✔
1285
            if ($child->nodeName === '#text') {
1✔
1286
                $content = trim($child->textContent);
1✔
1287
                if ($content !== '') {
1✔
1288
                    $baseTextRun->addText($content);
1✔
1289
                }
1290
            } elseif ($child->nodeName === 'rt') {
1✔
1291
                $rubyTextRun->addText(trim($child->textContent));
1✔
1292
                if ($child->hasAttributes()) {
1✔
1293
                    $styleAttr = $child->attributes->getNamedItem('style');
1✔
1294
                    if ($styleAttr !== null) {
1✔
1295
                        $styles = self::parseStyle($styleAttr, []);
1✔
1296
                        if (isset($styles['size']) && $styles['size'] !== '') {
1✔
1297
                            $rubyProperties->setFontFaceSize($styles['size']);
1✔
1298
                        }
1299
                        $rubyTextRun->setParagraphStyle($styles);
1✔
1300
                    }
1301
                }
1302
            }
1303
        }
1304

1305
        return $element->addRuby($baseTextRun, $rubyTextRun, $rubyProperties);
1✔
1306
    }
1307

1308
    private static function convertRgb(string $rgb): string
1309
    {
1310
        if (preg_match(self::RGB_REGEXP, $rgb, $matches) === 1) {
9✔
1311
            return sprintf('%02X%02X%02X', $matches[1], $matches[2], $matches[3]);
1✔
1312
        }
1313

1314
        return trim($rgb, '# ');
9✔
1315
    }
1316

1317
    /**
1318
     * Transform HTML sizes (pt, px) in pixels.
1319
     */
1320
    protected static function convertHtmlSize(string $size): float
1321
    {
1322
        // pt
1323
        if (false !== strpos($size, 'pt')) {
14✔
1324
            return Converter::pointToPixel((float) str_replace('pt', '', $size));
2✔
1325
        }
1326

1327
        // px
1328
        if (false !== strpos($size, 'px')) {
12✔
1329
            return (float) str_replace('px', '', $size);
2✔
1330
        }
1331

1332
        return (float) $size;
10✔
1333
    }
1334
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc