Merge pull request #1441 from aarangara/add_html_table_layout

Adding table layout to the generated HTML
This commit is contained in:
troosan 2018-11-29 17:39:31 +01:00 committed by GitHub
commit 9dd5e0c27d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 1 deletions

View File

@ -10,6 +10,7 @@ v0.16.0 (xx xxx 2018)
### Fixed ### Fixed
- Fix regex in `cloneBlock` function @nicoder #1269 - Fix regex in `cloneBlock` function @nicoder #1269
- HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436 - 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 - Fix loading of Sharepoint document @Garrcomm #1498
- RTF writer: Round getPageSizeW and getPageSizeH to avoid decimals @Patrick64 #1493 - RTF writer: Round getPageSizeW and getPageSizeH to avoid decimals @Patrick64 #1493
- Fix parsing of Office 365 documents @Timanx #1485 - Fix parsing of Office 365 documents @Timanx #1485

View File

@ -39,7 +39,8 @@ class Table extends AbstractElement
$rows = $this->element->getRows(); $rows = $this->element->getRows();
$rowCount = count($rows); $rowCount = count($rows);
if ($rowCount > 0) { if ($rowCount > 0) {
$content .= '<table>' . PHP_EOL; $content .= '<table' . self::getTableStyle($this->element->getStyle()) . '>' . PHP_EOL;
for ($i = 0; $i < $rowCount; $i++) { for ($i = 0; $i < $rowCount; $i++) {
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */ /** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
$rowStyle = $rows[$i]->getStyle(); $rowStyle = $rows[$i]->getStyle();
@ -102,4 +103,25 @@ class Table extends AbstractElement
return $content; 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 . '"';
}
} }

View File

@ -157,4 +157,28 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$this->assertTrue(strpos($content, $expected) !== false); $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);
}
} }