diff --git a/CHANGELOG.md b/CHANGELOG.md index 8753ae7d..d1db7a5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ v0.16.0 (xx xxx 2018) ### Fixed - Fix regex in `cloneBlock` function @nicoder #1269 - HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436 +- Adding table layout to the generated HTML @aarangara #1441 - Fix loading of Sharepoint document @Garrcomm #1498 - RTF writer: Round getPageSizeW and getPageSizeH to avoid decimals @Patrick64 #1493 - Fix parsing of Office 365 documents @Timanx #1485 diff --git a/src/PhpWord/Writer/HTML/Element/Table.php b/src/PhpWord/Writer/HTML/Element/Table.php index 844066f4..a5143d2b 100644 --- a/src/PhpWord/Writer/HTML/Element/Table.php +++ b/src/PhpWord/Writer/HTML/Element/Table.php @@ -39,7 +39,8 @@ class Table extends AbstractElement $rows = $this->element->getRows(); $rowCount = count($rows); if ($rowCount > 0) { - $content .= '' . PHP_EOL; + $content .= 'element->getStyle()) . '>' . PHP_EOL; + for ($i = 0; $i < $rowCount; $i++) { /** @var $row \PhpOffice\PhpWord\Element\Row Type hint */ $rowStyle = $rows[$i]->getStyle(); @@ -102,4 +103,25 @@ class Table extends AbstractElement return $content; } + + /** + * Translates Table style in CSS equivalent + * + * @param \PhpOffice\PhpWord\Style\Table|null $tableStyle + * @return string + */ + private function getTableStyle(\PhpOffice\PhpWord\Style\Table $tableStyle = null) + { + if ($tableStyle == null) { + return ''; + } + $style = ' style="'; + if ($tableStyle->getLayout() == \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED) { + $style .= 'table-layout: fixed;'; + } elseif ($tableStyle->getLayout() == \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO) { + $style .= 'table-layout: auto;'; + } + + return $style . '"'; + } } diff --git a/tests/PhpWord/Writer/HTML/ElementTest.php b/tests/PhpWord/Writer/HTML/ElementTest.php index 7a6397ef..73c6ede9 100644 --- a/tests/PhpWord/Writer/HTML/ElementTest.php +++ b/tests/PhpWord/Writer/HTML/ElementTest.php @@ -157,4 +157,28 @@ class ElementTest extends \PHPUnit\Framework\TestCase $this->assertTrue(strpos($content, $expected) !== false); } + + /** + * Tests writing table with layout + */ + public function testWriteTableLayout() + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + $section->addTable(); + + $table1 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED)); + $row1 = $table1->addRow(); + $row1->addCell()->addText('fixed layout table'); + + $table2 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO)); + $row2 = $table2->addRow(); + $row2->addCell()->addText('auto layout table'); + + $dom = $this->getAsHTML($phpWord); + $xpath = new \DOMXPath($dom); + + $this->assertEquals('table-layout: fixed;', $xpath->query('/html/body/table[1]')->item(0)->attributes->getNamedItem('style')->textContent); + $this->assertEquals('table-layout: auto;', $xpath->query('/html/body/table[2]')->item(0)->attributes->getNamedItem('style')->textContent); + } }