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

PHPOffice / PHPWord / 18454464320

13 Oct 2025 03:42AM UTC coverage: 96.74% (-0.02%) from 96.757%
18454464320

Pull #2843

github

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

26 of 29 new or added lines in 2 files covered. (89.66%)

12523 of 12945 relevant lines covered (96.74%)

35.37 hits per line

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

93.75
/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 = isset($elements[$index - 1]) ? $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
                    $content = mb_rtrim($content, '</li>');
1✔
63
                    $content .= '<ul>';
1✔
64
                }
65
            }
66

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

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

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

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