Skip to content

Commit f3c73f3

Browse files
laloptroosan
authored andcommitted
Fix HTML parsing when style attribute is empty (#1295)
1 parent 59de019 commit f3c73f3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,9 @@ private static function parseListItem($node, $element, &$styles, $data)
486486
private static function parseStyle($attribute, $styles)
487487
{
488488
$properties = explode(';', trim($attribute->value, " \t\n\r\0\x0B;"));
489+
489490
foreach ($properties as $property) {
490-
list($cKey, $cValue) = explode(':', $property, 2);
491+
list($cKey, $cValue) = array_pad(explode(':', $property, 2), 2, null);
491492
$cValue = trim($cValue);
492493
switch (trim($cKey)) {
493494
case 'text-decoration':

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,14 @@ public function testParseLink()
418418
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
419419
$this->assertEquals('link text', $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t')->nodeValue);
420420
}
421+
422+
public function testParseMalformedStyleIsIgnored()
423+
{
424+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
425+
$section = $phpWord->addSection();
426+
$html = '<p style="">text</p>';
427+
Html::addHtml($section, $html);
428+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
429+
$this->assertFalse($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr/w:jc'));
430+
}
421431
}

0 commit comments

Comments
 (0)