File tree Expand file tree Collapse file tree 3 files changed +32
-6
lines changed
src/PhpWord/Writer/HTML/Element
tests/PhpWord/Writer/HTML Expand file tree Collapse file tree 3 files changed +32
-6
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ v0.16.0 (xx xxx 2018)
10
10
### Fixed
11
11
- Fix regex in ` cloneBlock ` function @nicoder #1269
12
12
- 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
13
14
14
15
v0.15.0 (14 Jul 2018)
15
16
----------------------
Original file line number Diff line number Diff line change @@ -39,9 +39,8 @@ public function write()
39
39
$ rows = $ this ->element ->getRows ();
40
40
$ rowCount = count ($ rows );
41
41
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
+
45
44
for ($ i = 0 ; $ i < $ rowCount ; $ i ++) {
46
45
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
47
46
$ rowStyle = $ rows [$ i ]->getStyle ();
@@ -104,4 +103,25 @@ public function write()
104
103
105
104
return $ content ;
106
105
}
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
+ }
107
127
}
Original file line number Diff line number Diff line change @@ -166,14 +166,19 @@ public function testWriteTableLayout()
166
166
$ phpWord = new PhpWord ();
167
167
$ section = $ phpWord ->addSection ();
168
168
$ section ->addTable ();
169
- $ table = $ section ->addTable (array ('layout ' => 'fixed ' ));
170
169
171
- $ row1 = $ table ->addRow ();
170
+ $ table1 = $ section ->addTable (array ('layout ' => \PhpOffice \PhpWord \Style \Table::LAYOUT_FIXED ));
171
+ $ row1 = $ table1 ->addRow ();
172
172
$ row1 ->addCell ()->addText ('fixed layout table ' );
173
173
174
+ $ table2 = $ section ->addTable (array ('layout ' => \PhpOffice \PhpWord \Style \Table::LAYOUT_AUTO ));
175
+ $ row2 = $ table2 ->addRow ();
176
+ $ row2 ->addCell ()->addText ('auto layout table ' );
177
+
174
178
$ dom = $ this ->getAsHTML ($ phpWord );
175
179
$ xpath = new \DOMXPath ($ dom );
176
180
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 );
178
183
}
179
184
}
You can’t perform that action at this time.
0 commit comments