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

PHPOffice / PHPWord / 18455246156

13 Oct 2025 04:33AM UTC coverage: 96.74% (-0.02%) from 96.757%
18455246156

Pull #2843

github

web-flow
Merge 1bacc1dee into 0ab0b4940
Pull Request #2843: Writer HTML: ListItemRun render as unordered lists

27 of 30 new or added lines in 2 files covered. (90.0%)

12524 of 12946 relevant lines covered (96.74%)

35.37 hits per line

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

93.94
/src/PhpWord/Writer/HTML/Element/Container.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\Writer\HTML\Element;
20

21
use PhpOffice\PhpWord\Element\AbstractContainer as ContainerElement;
22

23
/**
24
 * Container element HTML writer.
25
 *
26
 * @since 0.11.0
27
 */
28
class Container extends AbstractElement
29
{
30
    /**
31
     * Namespace; Can't use __NAMESPACE__ in inherited class (RTF).
32
     *
33
     * @var string
34
     */
35
    protected $namespace = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element';
36

37
    /**
38
     * Write container.
39
     *
40
     * @return string
41
     */
42
    public function write()
43
    {
44
        $container = $this->element;
35✔
45
        if (!$container instanceof ContainerElement) {
35✔
46
            return '';
1✔
47
        }
48
        $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
34✔
49
        $withoutP = in_array($containerClass, ['TextRun', 'Footnote', 'Endnote']) ? true : false;
34✔
50
        $content = '';
34✔
51

52
        $elements = $container->getElements();
34✔
53
        foreach ($elements as $index => $element) {
34✔
54
            if ($element instanceof \PhpOffice\PhpWord\Element\ListItemRun) {
34✔
55
                $prevElement = $elements[$index - 1] ?? null;
1✔
56

57
                if ($prevElement === null) {
1✔
58
                    $content .= '<ul>';
1✔
59
                } elseif (!$prevElement instanceof \PhpOffice\PhpWord\Element\ListItemRun) {
1✔
NEW
60
                    $content .= '<ul>';
×
61
                } elseif ($prevElement->getDepth() < $element->getDepth()) {
1✔
62
                    if (str_ends_with($content, '</li>')) {
1✔
63
                        $content = substr($content, 0, -5);
1✔
64
                    }
65
                    $content .= '<ul>';
1✔
66
                }
67
            }
68

69
            $elementClass = get_class($element);
34✔
70
            $writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass);
34✔
71
            if (class_exists($writerClass)) {
34✔
72
                /** @var AbstractElement $writer Type hint */
73
                $writer = new $writerClass($this->parentWriter, $element, $withoutP);
34✔
74
                $content .= $writer->write();
34✔
75
            }
76

77
            if ($element instanceof \PhpOffice\PhpWord\Element\ListItemRun) {
34✔
78
                $nextElement = $elements[$index + 1] ?? null;
1✔
79

80
                if ($nextElement === null) {
1✔
81
                    $content .= '</ul>';
1✔
82
                } elseif (!$nextElement instanceof \PhpOffice\PhpWord\Element\ListItemRun) {
1✔
NEW
83
                    $content .= '</ul>';
×
84
                } elseif ($nextElement->getDepth() < $element->getDepth()) {
1✔
85
                    for ($i = $element->getDepth() - $nextElement->getDepth(); $i !== 0; --$i) {
1✔
86
                        $content .= '</ul></li>';
1✔
87
                    }
88
                }
89
            }
90
        }
91

92
        return $content;
34✔
93
    }
94
}
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