diff --git a/.gitignore b/.gitignore index dd858ceac6..f5f6f7eb81 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ phpword.ini /.project /nbproject /.php_cs.cache +/.phpunit.result.cache diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 02534736b0..ad5aec1181 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -107,10 +107,6 @@ protected static function parseInlineStyle($node, $styles = []) foreach ($attributes as $attribute) { $val = $attribute->value; switch (strtolower($attribute->name)) { - case 'style': - $styles = self::parseStyle($attribute, $styles); - - break; case 'align': $styles['alignment'] = self::mapAlign(trim($val)); @@ -152,6 +148,11 @@ protected static function parseInlineStyle($node, $styles = []) break; } } + + $attributeStyle = $attributes->getNamedItem('style'); + if ($attributeStyle) { + $styles = self::parseStyle($attributeStyle, $styles); + } } return $styles; diff --git a/tests/PhpWordTests/Shared/HtmlTest.php b/tests/PhpWordTests/Shared/HtmlTest.php index 69e34ed927..cd2338e18e 100644 --- a/tests/PhpWordTests/Shared/HtmlTest.php +++ b/tests/PhpWordTests/Shared/HtmlTest.php @@ -19,6 +19,7 @@ use Exception; use PhpOffice\PhpWord\Element\Section; +use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\Html; use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\SimpleType\LineSpacingRule; @@ -41,7 +42,7 @@ public function testAddHtml(): void $content = ''; // Default - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); self::assertCount(0, $section->getElements()); @@ -97,7 +98,7 @@ public function testParseFullHtml(): void public function testParseHtmlEntities(): void { \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true); - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, 'text with entities <my text>'); @@ -112,7 +113,7 @@ public function testParseHtmlEntities(): void public function testParseUnderline(): void { $html = 'test'; - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, $html); @@ -127,7 +128,7 @@ public function testParseUnderline(): void public function testParseTextDecoration(): void { $html = 'test'; - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, $html); @@ -142,7 +143,7 @@ public function testParseTextDecoration(): void public function testParseFont(): void { $html = 'test'; - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, $html); @@ -156,7 +157,7 @@ public function testParseFont(): void */ public function testParseLineHeight(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, '

test

'); Html::addHtml($section, '

test

'); @@ -191,7 +192,7 @@ public function testParseLineHeight(): void */ public function testParseTextIndent(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, '

test

'); @@ -205,7 +206,7 @@ public function testParseTextIndent(): void */ public function testParseTextAlign(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, '

test

'); Html::addHtml($section, '

test

'); @@ -225,7 +226,7 @@ public function testParseTextAlign(): void */ public function testParseFontSize(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, 'test'); Html::addHtml($section, 'test'); @@ -241,7 +242,7 @@ public function testParseFontSize(): void */ public function testParseTextDirection(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, 'test'); @@ -254,7 +255,7 @@ public function testParseTextDirection(): void */ public function testParseLang(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, 'test'); @@ -268,7 +269,7 @@ public function testParseLang(): void */ public function testParseFontFamily(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, 'test'); Html::addHtml($section, 'test'); @@ -284,7 +285,7 @@ public function testParseFontFamily(): void */ public function testParseParagraphAndSpanStyle(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, '

test

'); @@ -300,7 +301,7 @@ public function testParseParagraphAndSpanStyle(): void */ public function testParseParagraphWithPageBreak(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); Html::addHtml($section, '

'); @@ -314,7 +315,7 @@ public function testParseParagraphWithPageBreak(): void */ public function testParseTable(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); $html = ' @@ -355,12 +356,81 @@ public function testParseTable(): void self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:p/w:pPr/w:pBdr')); } + /** + * Parse widths in tables and cells, which also allows for controlling column width. + */ + public function testParseTableAndCellWidth(): void + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection([ + 'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE, + ]); + + // borders & backgrounds are here just for better visual comparison + $html = << + + + + +
25% + + + + + + + + + + + + + +
400px
T2.R2.C150ptT2.R2.C3
300pxT2.R3.C3
+
+HTML; + + Html::addHtml($section, $html); + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + + // outer table grid + $xpath = '/w:document/w:body/w:tbl/w:tblGrid/w:gridCol'; + self::assertTrue($doc->elementExists($xpath)); + self::assertEquals(25 * 50, $doc->getElement($xpath)->getAttribute('w:w')); + self::assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type')); + + // elementExists($xpath)); + self::assertEquals(6000, $doc->getElement($xpath)->getAttribute('w:w')); + self::assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type')); + + // elementExists($xpath)); + self::assertEquals(4500, $doc->getElement($xpath)->getAttribute('w:w')); + self::assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type')); + } + /** * Test parsing table (attribute border). */ public function testParseTableAttributeBorder(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); $html = ' @@ -384,12 +454,82 @@ public function testParseTableAttributeBorder(): void self::assertEquals(150, $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tblPr/w:tblBorders/w:top', 'w:sz')); } + /** + * Test parsing background color for table rows and table cellspacing. + */ + public function testParseTableCellspacingRowBgColor(): void + { + $phpWord = new 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'); + + $xpath = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellSpacing'; + self::assertTrue($doc->elementExists($xpath)); + self::assertEquals(3 * 15, $doc->getElement($xpath)->getAttribute('w:w')); + self::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'; + self::assertTrue($doc->elementExists($xpath)); + self::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'; + self::assertTrue($doc->elementExists($xpath)); + self::assertEquals('FF0000', $doc->getElement($xpath)->getAttribute('w:fill')); + } + + /** + * Test parsing background color for table rows and table cellspacing. + */ + public function testParseTableStyleAttributeInlineStyle(): void + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + + $html = << + + A + + + HTML; + + Html::addHtml($section, $html); + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + + $xpath = '/w:document/w:body/w:tbl/w:tblPr/w:tblW'; + self::assertTrue($doc->elementExists($xpath)); + self::assertEquals(100 * 50, $doc->getElement($xpath)->getAttribute('w:w')); + self::assertEquals('pct', $doc->getElement($xpath)->getAttribute('w:type')); + + $xpath = '/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:tcPr/w:shd'; + self::assertTrue($doc->elementExists($xpath)); + self::assertEquals('red', $doc->getElement($xpath)->getAttribute('w:fill')); + } + /** * Tests parsing of ul/li. */ public function testParseList(): void { - $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord = new PhpWord(); $section = $phpWord->addSection(); $html = '