Skip to content

Commit dfea4e1

Browse files
authored
Merge pull request #1766 from stefan-91/develop
Add support for ListItemRun in HTML writer
2 parents 5940d18 + 1451fad commit dfea4e1

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2018 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\HTML\Element;
19+
20+
/**
21+
* ListItem element HTML writer
22+
*
23+
* @since 0.10.0
24+
*/
25+
class ListItemRun extends TextRun
26+
{
27+
/**
28+
* Write list item
29+
*
30+
* @return string
31+
*/
32+
public function write()
33+
{
34+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\ListItemRun) {
35+
return '';
36+
}
37+
38+
$writer = new Container($this->parentWriter, $this->element);
39+
$content = $writer->write() . PHP_EOL;
40+
41+
return $content;
42+
}
43+
}

tests/PhpWord/Writer/HTML/ElementTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ElementTest extends \PHPUnit\Framework\TestCase
3434
*/
3535
public function testUnmatchedElements()
3636
{
37-
$elements = array('Container', 'Footnote', 'Image', 'Link', 'ListItem', 'Table', 'Title', 'Bookmark');
37+
$elements = array('Container', 'Footnote', 'Image', 'Link', 'ListItem', 'ListItemRun', 'Table', 'Title', 'Bookmark');
3838
foreach ($elements as $element) {
3939
$objectClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $element;
4040
$parentWriter = new HTML();
@@ -163,6 +163,31 @@ public function testWriteTitleTextRun()
163163
$this->assertContains($expected, $content);
164164
}
165165

166+
/**
167+
* Test write element ListItemRun
168+
*/
169+
public function testListItemRun()
170+
{
171+
$expected1 = 'List item run 1';
172+
$expected2 = 'List item run 1 in bold';
173+
174+
$phpWord = new PhpWord();
175+
$section = $phpWord->addSection();
176+
177+
$listItemRun = $section->addListItemRun(0, null, 'MyParagraphStyle');
178+
$listItemRun->addText($expected1);
179+
$listItemRun->addText($expected2, array('bold' => true));
180+
181+
$htmlWriter = new HTML($phpWord);
182+
$content = $htmlWriter->getContent();
183+
184+
$dom = new \DOMDocument();
185+
$dom->loadHTML($content);
186+
187+
$this->assertEquals($expected1, $dom->getElementsByTagName('p')->item(0)->textContent);
188+
$this->assertEquals($expected2, $dom->getElementsByTagName('p')->item(1)->textContent);
189+
}
190+
166191
/**
167192
* Tests writing table with layout
168193
*/

0 commit comments

Comments
 (0)