Skip to content

Commit 59de019

Browse files
authored
Fix listitem parsing (#1290)
* Word 2007 Reader: Added support for ListItemRun * Add tests + changelog
1 parent def0237 commit 59de019

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ v0.15.0 (?? ??? 2018)
1212
- Add support for Track changes @Cip @troosan #354 #1262
1313
- Add support for fixed Table Layout @aoloe @ekopach @troosan #841 #1276
1414
- Add support for Cell Spacing @dox07 @troosan #1040
15+
- Add parsing of formatting inside lists @atomicalnet @troosan #594
1516

1617
### Fixed
1718
- Fix reading of docx default style - @troosan #1238
@@ -22,6 +23,8 @@ v0.15.0 (?? ??? 2018)
2223
- Bookmark are not writton as internal link in html writer @troosan #1263
2324
- It should be possible to add a Footnote in a ListItemRun @troosan #1287 #1287
2425

26+
### Changed
27+
- Remove zend-stdlib dependency @Trainmaster #1284
2528

2629

2730
v0.14.0 (29 Dec 2017)
68 Bytes
Binary file not shown.

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,15 @@ protected function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, $pa
138138
$parent->addPreserveText($textContent, $fontStyle, $paragraphStyle);
139139
} elseif ($xmlReader->elementExists('w:pPr/w:numPr', $domNode)) {
140140
// List item
141-
$textContent = '';
142141
$numId = $xmlReader->getAttribute('w:val', $domNode, 'w:pPr/w:numPr/w:numId');
143142
$levelId = $xmlReader->getAttribute('w:val', $domNode, 'w:pPr/w:numPr/w:ilvl');
144-
$nodes = $xmlReader->getElements('w:r', $domNode);
143+
$nodes = $xmlReader->getElements('*', $domNode);
144+
145+
$listItemRun = $parent->addListItemRun($levelId, "PHPWordList{$numId}", $paragraphStyle);
146+
145147
foreach ($nodes as $node) {
146-
$textContent .= $xmlReader->getValue('w:t', $node);
148+
$this->readRun($xmlReader, $node, $listItemRun, $docPart, $paragraphStyle);
147149
}
148-
$parent->addListItem($textContent, $levelId, null, "PHPWordList{$numId}", $paragraphStyle);
149150
} elseif (!empty($headingMatches)) {
150151
// Heading
151152
$textContent = '';

tests/PhpWord/Reader/Word2007/ElementTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,44 @@ public function testReadTextBreak()
4343
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $elements[1]);
4444
$this->assertEquals('test string', $elements[1]->getText());
4545
}
46+
47+
/**
48+
* Test reading of textbreak
49+
*/
50+
public function testReadListItemRunWithFormatting()
51+
{
52+
$documentXml = '<w:p>
53+
<w:pPr>
54+
<w:numPr>
55+
<w:ilvl w:val="0"/>
56+
<w:numId w:val="11"/>
57+
</w:numPr>
58+
</w:pPr>
59+
<w:r>
60+
<w:t>Two</w:t>
61+
</w:r>
62+
<w:r>
63+
<w:t xml:space="preserve"> with </w:t>
64+
</w:r>
65+
<w:r>
66+
<w:rPr>
67+
<w:b/>
68+
</w:rPr>
69+
<w:t>bold</w:t>
70+
</w:r>
71+
</w:p>';
72+
73+
$phpWord = $this->getDocumentFromString($documentXml);
74+
75+
$elements = $this->get($phpWord->getSections(), 0)->getElements();
76+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\ListItemRun', $elements[0]);
77+
$this->assertEquals(0, $elements[0]->getDepth());
78+
79+
$listElements = $this->get($elements, 0)->getElements();
80+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Text', $listElements[0]);
81+
$this->assertEquals('Two', $listElements[0]->getText());
82+
$this->assertEquals(' with ', $listElements[1]->getText());
83+
$this->assertEquals('bold', $listElements[2]->getText());
84+
$this->assertTrue($listElements[2]->getFontStyle()->getBold());
85+
}
4686
}

0 commit comments

Comments
 (0)