support `auto` table layout too

This commit is contained in:
troosan 2018-11-28 22:54:57 +01:00
parent 32fb85fc8e
commit b50de97a41
3 changed files with 32 additions and 6 deletions

View File

@ -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
v0.15.0 (14 Jul 2018)
----------------------

View File

@ -39,9 +39,8 @@ class Table extends AbstractElement
$rows = $this->element->getRows();
$rowCount = count($rows);
if ($rowCount > 0) {
$tableStyle = $this->element->getStyle();
$tableLayout = $tableStyle === null ? '' : $tableStyle->getLayout();
$content .= '<table' . (empty($tableLayout) ? '' : ' style="table-layout: ' . $tableLayout . '"') . '>' . PHP_EOL;
$content .= '<table' . self::getTableStyle($this->element->getStyle()) . '>' . PHP_EOL;
for ($i = 0; $i < $rowCount; $i++) {
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
$rowStyle = $rows[$i]->getStyle();
@ -104,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 . '"';
}
}

View File

@ -166,14 +166,19 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addTable();
$table = $section->addTable(array('layout' => 'fixed'));
$row1 = $table->addRow();
$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')->item(0)->attributes->getNamedItem('style')->textContent);
$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);
}
}