Skip to content

Commit b50de97

Browse files
committed
support auto table layout too
1 parent 32fb85f commit b50de97

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ v0.16.0 (xx xxx 2018)
1010
### Fixed
1111
- Fix regex in `cloneBlock` function @nicoder #1269
1212
- HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436
13+
- Adding table layout to the generated HTML @aarangara #1441
1314

1415
v0.15.0 (14 Jul 2018)
1516
----------------------

src/PhpWord/Writer/HTML/Element/Table.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ public function write()
3939
$rows = $this->element->getRows();
4040
$rowCount = count($rows);
4141
if ($rowCount > 0) {
42-
$tableStyle = $this->element->getStyle();
43-
$tableLayout = $tableStyle === null ? '' : $tableStyle->getLayout();
44-
$content .= '<table' . (empty($tableLayout) ? '' : ' style="table-layout: ' . $tableLayout . '"') . '>' . PHP_EOL;
42+
$content .= '<table' . self::getTableStyle($this->element->getStyle()) . '>' . PHP_EOL;
43+
4544
for ($i = 0; $i < $rowCount; $i++) {
4645
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
4746
$rowStyle = $rows[$i]->getStyle();
@@ -104,4 +103,25 @@ public function write()
104103

105104
return $content;
106105
}
106+
107+
/**
108+
* Translates Table style in CSS equivalent
109+
*
110+
* @param \PhpOffice\PhpWord\Style\Table|null $tableStyle
111+
* @return string
112+
*/
113+
private function getTableStyle(\PhpOffice\PhpWord\Style\Table $tableStyle = null)
114+
{
115+
if ($tableStyle == null) {
116+
return '';
117+
}
118+
$style = ' style="';
119+
if ($tableStyle->getLayout() == \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED) {
120+
$style .= 'table-layout: fixed;';
121+
} elseif ($tableStyle->getLayout() == \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO) {
122+
$style .= 'table-layout: auto;';
123+
}
124+
125+
return $style . '"';
126+
}
107127
}

tests/PhpWord/Writer/HTML/ElementTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,19 @@ public function testWriteTableLayout()
166166
$phpWord = new PhpWord();
167167
$section = $phpWord->addSection();
168168
$section->addTable();
169-
$table = $section->addTable(array('layout' => 'fixed'));
170169

171-
$row1 = $table->addRow();
170+
$table1 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED));
171+
$row1 = $table1->addRow();
172172
$row1->addCell()->addText('fixed layout table');
173173

174+
$table2 = $section->addTable(array('layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_AUTO));
175+
$row2 = $table2->addRow();
176+
$row2->addCell()->addText('auto layout table');
177+
174178
$dom = $this->getAsHTML($phpWord);
175179
$xpath = new \DOMXPath($dom);
176180

177-
$this->assertEquals('table-layout: fixed', $xpath->query('/html/body/table')->item(0)->attributes->getNamedItem('style')->textContent);
181+
$this->assertEquals('table-layout: fixed;', $xpath->query('/html/body/table[1]')->item(0)->attributes->getNamedItem('style')->textContent);
182+
$this->assertEquals('table-layout: auto;', $xpath->query('/html/body/table[2]')->item(0)->attributes->getNamedItem('style')->textContent);
178183
}
179184
}

0 commit comments

Comments
 (0)