From e180cfe456ee81c210fa0034c740655f25c91144 Mon Sep 17 00:00:00 2001 From: lubosdz Date: Sat, 11 Jul 2020 00:24:08 +0200 Subject: [PATCH] Html parser (addHtml) - support cellspacing, bgColor --- src/PhpWord/Shared/Html.php | 14 +++++++++-- tests/PhpWord/Shared/HtmlTest.php | 42 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index cb09af62..08004b7a 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -96,7 +96,7 @@ class Html $attributes = $node->attributes; // get all the attributes(eg: id, class) foreach ($attributes as $attribute) { - switch ($attribute->name) { + switch (strtolower($attribute->name)) { case 'style': $styles = self::parseStyle($attribute, $styles); break; @@ -119,6 +119,15 @@ class Html $styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP; } break; + case 'cellspacing': + // tables e.g. , where "2" = 2px (always pixels) + $val = intval($attribute->value).'px'; + $styles['cellSpacing'] = Converter::cssToTwip($val); + break; + case 'bgcolor': + // tables, rows, cells e.g. + $styles['bgColor'] = trim($attribute->value, '# '); + break; } } } @@ -519,7 +528,8 @@ class Html foreach ($properties as $property) { list($cKey, $cValue) = array_pad(explode(':', $property, 2), 2, null); $cValue = trim($cValue); - switch (trim($cKey)) { + $cKey = strtolower(trim($cKey)); + switch ($cKey) { case 'text-decoration': switch ($cValue) { case 'underline': diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index 5474eb0d..61ebd535 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -702,4 +702,46 @@ HTML; $this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type')); } + public function testParseCellspacingRowBgColor() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection([ + 'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE, + ]); + + // borders & backgrounds are here just for better visual comparison + $html = << + + + + + + + + +
AB
CD
+HTML; + + Html::addHtml($section, $html); + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + + // uncomment to see results + file_put_contents('./table_src.html', $html); + file_put_contents('./table_result_'.time().'.docx', file_get_contents( TestHelperDOCX::getFile() ) ); + + $xpath = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellSpacing'; + $this->assertTrue($doc->elementExists($xpath)); + $this->assertEquals(3 * 15, $doc->getElement($xpath)->getAttribute('w:w')); + $this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type')); + + $xpath = '/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:tcPr/w:shd'; + $this->assertTrue($doc->elementExists($xpath)); + $this->assertEquals('lightgreen', $doc->getElement($xpath)->getAttribute('w:fill')); + + $xpath = '/w:document/w:body/w:tbl/w:tr[2]/w:tc[1]/w:tcPr/w:shd'; + $this->assertTrue($doc->elementExists($xpath)); + $this->assertEquals('FF0000', $doc->getElement($xpath)->getAttribute('w:fill')); + } + }