Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/1.x/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Bug fixes

- Set writeAttribute return type by [@radarhere](https://github.com/radarhere) fixing [#2204](https://github.com/PHPOffice/PHPWord/issues/2204) in [#2776](https://github.com/PHPOffice/PHPWord/pull/2776)
- Writer HTML: ListItemRun render as unordered lists by [@andomiell](https://github.com/andomiell) partially fixing [#1462](https://github.com/PHPOffice/PHPWord/issues/1462)

### Miscellaneous

Expand Down
31 changes: 30 additions & 1 deletion src/PhpWord/Writer/HTML/Element/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,43 @@ public function write()
$content = '';

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

if ($prevElement === null) {
$content .= '<ul>';
} elseif (!$prevElement instanceof \PhpOffice\PhpWord\Element\ListItemRun) {
$content .= '<ul>';
} elseif ($prevElement->getDepth() < $element->getDepth()) {
if (str_ends_with($content, '</li>')) {
$content = substr($content, 0, -5);
}
$content .= '<ul>';
}
}

$elementClass = get_class($element);
$writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass);
if (class_exists($writerClass)) {
/** @var AbstractElement $writer Type hint */
$writer = new $writerClass($this->parentWriter, $element, $withoutP);
$content .= $writer->write();
}

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

if ($nextElement === null) {
$content .= '</ul>';
} elseif (!$nextElement instanceof \PhpOffice\PhpWord\Element\ListItemRun) {
$content .= '</ul>';
} elseif ($nextElement->getDepth() < $element->getDepth()) {
for ($i = $element->getDepth() - $nextElement->getDepth(); $i !== 0; --$i) {
$content .= '</ul></li>';
}
}
}
}

return $content;
Expand Down
19 changes: 16 additions & 3 deletions src/PhpWord/Writer/HTML/Element/ListItemRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,22 @@ public function write()
return '';
}

$writer = new Container($this->parentWriter, $this->element);
$content = $writer->write() . PHP_EOL;
$content = '<li>';

return $content;
foreach ($this->element->getElements() as $element) {
$namespace = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element';
$elementClass = get_class($element);
$writerClass = str_replace('PhpOffice\\PhpWord\\Element', $namespace, $elementClass);

if (!class_exists($writerClass)) {
continue;
}

/** @var AbstractElement $writer */
$writer = new $writerClass($this->parentWriter, $element, true);
$content .= $writer->write();
}

return "$content</li>";
}
}
60 changes: 53 additions & 7 deletions tests/PhpWordTests/Writer/HTML/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

use DateTime;
use DOMDocument;
use DOMNode;
use DOMNodeList;
use DOMXPath;
use PhpOffice\PhpWord\Element\Text as TextElement;
use PhpOffice\PhpWord\Element\TextRun;
Expand Down Expand Up @@ -77,8 +79,8 @@
$dom = Helper::getAsHTML($phpWord);
$xpath = new DOMXPath($dom);

self::assertEquals(1, $xpath->query('/html/body/div/p[1]/ins')->length);

Check failure on line 82 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 82 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 82 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 82 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.4)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 82 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 82 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.
self::assertEquals(1, $xpath->query('/html/body/div/p[2]/del')->length);

Check failure on line 83 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 83 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 83 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 83 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.4)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 83 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 83 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.
}

/**
Expand All @@ -101,8 +103,8 @@
$dom = Helper::getAsHTML($phpWord);
$xpath = new DOMXPath($dom);

self::assertEquals(1, $xpath->query('/html/body/div/table/tr[1]/td')->length);

Check failure on line 106 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 106 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 106 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 106 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.4)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 106 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.

Check failure on line 106 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Cannot access property $length on DOMNodeList<DOMNameSpaceNode|DOMNode>|false.
self::assertEquals('2', $xpath->query('/html/body/div/table/tr/td[1]')->item(0)->attributes->getNamedItem('colspan')->textContent);

Check failure on line 107 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Access to an undefined property DOMNameSpaceNode|DOMNode::$attributes.

Check failure on line 107 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Access to an undefined property DOMNameSpaceNode|DOMNode::$attributes.

Check failure on line 107 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Access to an undefined property DOMNameSpaceNode|DOMNode::$attributes.

Check failure on line 107 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.4)

Access to an undefined property DOMNameSpaceNode|DOMNode::$attributes.

Check failure on line 107 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Access to an undefined property DOMNameSpaceNode|DOMNode::$attributes.

Check failure on line 107 in tests/PhpWordTests/Writer/HTML/ElementTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Access to an undefined property DOMNameSpaceNode|DOMNode::$attributes.
self::assertEquals(2, $xpath->query('/html/body/div/table/tr[2]/td')->length);

self::assertEquals('#6086B8', $xpath->query('/html/body/div/table/tr[1]/td')->item(0)->attributes->getNamedItem('bgcolor')->textContent);
Expand Down Expand Up @@ -200,24 +202,68 @@
*/
public function testListItemRun(): void
{
$expected1 = 'List item run 1';
$expected2 = 'List item run 1 in bold';
$expected11 = 'List item run 1.1';
$expected11InBold = 'List item run 1.1 in bold';
$expected12 = 'List item run 1.2';
$expected21 = 'List item run 2.1';
$expected22 = 'List item run 2.2';
$expected31 = 'List item run 3.1';
$expected13 = 'List item run 1.3';

$phpWord = new PhpWord();
$section = $phpWord->addSection();

$listItemRun = $section->addListItemRun(0, null, 'MyParagraphStyle');
$listItemRun->addText($expected1);
$listItemRun->addText($expected2, ['bold' => true]);
$listItemRun11 = $section->addListItemRun(0, null, 'MyParagraphStyle');
$listItemRun11->addText($expected11);
$listItemRun11->addText($expected11InBold, ['bold' => true]);

$listItemRun12 = $section->addListItemRun(0);
$listItemRun12->addText($expected12);

$listItemRun21 = $section->addListItemRun(1);
$listItemRun21->addText($expected21);

$listItemRun22 = $section->addListItemRun(1);
$listItemRun22->addText($expected22);

$listItemRun31 = $section->addListItemRun(2);
$listItemRun31->addText($expected31);

$listItemRun13 = $section->addListItemRun(0);
$listItemRun13->addText($expected13);

$htmlWriter = new HTML($phpWord);
$content = $htmlWriter->getContent();

$dom = new DOMDocument();
$dom->loadHTML($content);

self::assertEquals($expected1, $dom->getElementsByTagName('p')->item(0)->textContent);
self::assertEquals($expected2, $dom->getElementsByTagName('p')->item(1)->textContent);
$xpath = new DOMXPath($dom);

/** @var DOMNodeList<DOMNode> $list */
$list = $xpath->query('//body/div/ul/li');

$item11 = $list->item(0);
$item12 = $list->item(1);
$item13 = $list->item(2);

self::assertEquals($expected11, $item11->childNodes->item(0)->textContent);
self::assertEquals('span', $item11->childNodes->item(1)->nodeName);
self::assertEquals($expected11InBold, $item11->childNodes->item(1)->textContent);

self::assertEquals($expected12, $item12->childNodes->item(0)->textContent);
self::assertEquals('ul', $item12->childNodes->item(1)->nodeName);

self::assertEquals($expected21, $item12->childNodes->item(1)->childNodes->item(0)->textContent);

$item22 = $item12->childNodes->item(1)->childNodes->item(1);

self::assertEquals($expected22, $item22->childNodes->item(0)->textContent);
self::assertEquals('ul', $item22->childNodes->item(1)->nodeName);

self::assertEquals($expected31, $item22->childNodes->item(1)->childNodes->item(0)->textContent);

self::assertEquals($expected13, $item13->childNodes->item(0)->textContent);
}

/**
Expand Down
Loading