Skip to content

Commit 5c33782

Browse files
committed
Add Named Table Styles to Head
Reaction to Coveralls report.
1 parent 6ed0ac1 commit 5c33782

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function write()
5151
$rowCellCount = count($rowCells);
5252
for ($j = 0; $j < $rowCellCount; $j++) {
5353
$cellStyle = $rowCells[$j]->getStyle();
54-
$cellStyleCss = $this->getTableStyle($cellStyle);
54+
$cellStyleCss = self::getTableStyle($cellStyle);
5555
$cellBgColor = $cellStyle->getBgColor();
5656
$cellFgColor = null;
5757
if ($cellBgColor) {
@@ -121,7 +121,7 @@ public function write()
121121
* @param string|\PhpOffice\PhpWord\Style\Table|\PhpOffice\PhpWord\Style\Cell|null $tableStyle
122122
* @return string
123123
*/
124-
private function getTableStyle($tableStyle = null)
124+
private static function getTableStyle($tableStyle = null)
125125
{
126126
if ($tableStyle == null) {
127127
return '';
@@ -131,6 +131,23 @@ private function getTableStyle($tableStyle = null)
131131

132132
return $style . '"';
133133
}
134+
135+
$style = self::getTableStyleString($tableStyle);
136+
if ($style === '') {
137+
return '';
138+
}
139+
140+
return ' style="' . $style . '"';
141+
}
142+
143+
/**
144+
* Translates Table style in CSS equivalent
145+
*
146+
* @param string|\PhpOffice\PhpWord\Style\Table|\PhpOffice\PhpWord\Style\Cell $tableStyle
147+
* @return string
148+
*/
149+
public static function getTableStyleString($tableStyle)
150+
{
134151
$style = '';
135152
if (method_exists($tableStyle, 'getLayout')) {
136153
if ($tableStyle->getLayout() == \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED) {
@@ -167,10 +184,6 @@ private function getTableStyle($tableStyle = null)
167184
}
168185
}
169186

170-
if ($style === '') {
171-
return '';
172-
}
173-
174-
return ' style="' . $style . '"';
187+
return $style;
175188
}
176189
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,12 @@ protected function writeClosing()
151151

152152
if (!$this->withoutP) {
153153
if (Settings::isOutputEscapingEnabled()) {
154-
$content .= $this->escaper->escapeHtml($this->closingText);
154+
// Scrutinizer notes that escapeHTML does not exist on AbstractEscaper.
155+
// Nevertheless, it does exist for HTML writer.
156+
$escaper = $this->escaper;
157+
/** @scrutinizer ignore-call */
158+
$contenx = $escaper->escapeHtml($this->closingText);
159+
$content .= $contenx;
155160
} else {
156161
$content .= $this->closingText;
157162
}

src/PhpWord/Writer/HTML/Part/Head.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use PhpOffice\PhpWord\Style;
2222
use PhpOffice\PhpWord\Style\Font;
2323
use PhpOffice\PhpWord\Style\Paragraph;
24+
use PhpOffice\PhpWord\Style\Table;
25+
use PhpOffice\PhpWord\Writer\HTML\Element\Table as TableStyleWriter;
2426
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
2527
use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter;
2628
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
@@ -143,6 +145,9 @@ private function writeStyles()
143145
}
144146
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
145147
}
148+
if ($style instanceof Table) {
149+
$css .= ".{$name} {" . TableStyleWriter::getTableStyleString($style) . '}' . PHP_EOL;
150+
}
146151
}
147152
}
148153
$secno = 0;

tests/PhpWord/Writer/HTML/StyleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ public function testWriteTableBorders()
109109
$row2->addCell(null, $bstyle)->addText('Row 2 Cell 1');
110110
$row2->addCell(null, $bstyle)->addText('Row 2 Cell 2');
111111

112+
$phpWord->addTableStyle('tstyle', array('borderStyle' => 'solid', 'borderSize' => 5));
113+
$table1 = $section->addTable('tstyle');
114+
$row1 = $table1->addRow();
115+
$row1->addCell(null, 'tstyle')->addText('Row 1 Cell 1');
116+
$row1->addCell(null, 'tstyle')->addText('Row 1 Cell 2');
117+
$row2 = $table1->addRow();
118+
$row2->addCell(null, 'tstyle')->addText('Row 2 Cell 1');
119+
$row2->addCell(null, 'tstyle')->addText('Row 2 Cell 2');
120+
112121
$dom = $this->getAsHTML($phpWord);
113122
$xpath = new \DOMXPath($dom);
114123

@@ -169,5 +178,11 @@ public function testWriteTableBorders()
169178
$this->assertEquals($cssnone, $xpath->query('/html/body/div/table[5]/tr[1]/td[2]')->item(0)->attributes->getNamedItem('style')->textContent);
170179
$this->assertEquals($cssnone, $xpath->query('/html/body/div/table[5]/tr[2]/td[1]')->item(0)->attributes->getNamedItem('style')->textContent);
171180
$this->assertEquals($cssnone, $xpath->query('/html/body/div/table[5]/tr[2]/td[2]')->item(0)->attributes->getNamedItem('style')->textContent);
181+
182+
$this->assertNull($xpath->query('/html/body/div/table[6]')->item(0)->attributes->getNamedItem('style'));
183+
$this->assertEquals('tstyle', $xpath->query('/html/body/div/table[6]')->item(0)->attributes->getNamedItem('class')->textContent);
184+
$style = $xpath->query('/html/head/style')->item(0)->textContent;
185+
self::assertNotFalse(preg_match('/^[.]tstyle[^\\r\\n]*/m', $style, $matches));
186+
self::assertEquals(".tstyle {table-layout: auto;$cssnone}", $matches[0]);
172187
}
173188
}

tests/PhpWord/Writer/PDF/MPDFTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function testConstruct()
3939
$section = $phpWord->addSection();
4040
$section->addText('Test 1');
4141
$section->addPageBreak();
42+
$section->addText('Test 2');
43+
$oSettings = new \PhpOffice\PhpWord\Style\Section();
44+
$oSettings->setSettingValue('orientation', 'landscape');
45+
$section = $phpWord->addSection($oSettings);
46+
$section->addText('Section 2 - landscape');
4247

4348
$rendererName = Settings::PDF_RENDERER_MPDF;
4449
$rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/mpdf/mpdf');

0 commit comments

Comments
 (0)