1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Html; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Border; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class HtmlTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testCsvWithAngleBracket(): void |
15
|
|
|
{ |
16
|
|
|
$filename = 'tests/data/Reader/HTML/csv_with_angle_bracket.csv'; |
17
|
|
|
$reader = new Html(); |
18
|
|
|
self::assertFalse($reader->canRead($filename)); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testBadHtml(): void |
22
|
|
|
{ |
23
|
|
|
$this->expectException(ReaderException::class); |
24
|
|
|
$filename = 'tests/data/Reader/HTML/badhtml.html'; |
25
|
|
|
$reader = new Html(); |
26
|
|
|
self::assertTrue($reader->canRead($filename)); |
27
|
|
|
$reader->load($filename); |
28
|
|
|
self::assertTrue(false); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testNonHtml(): void |
32
|
|
|
{ |
33
|
|
|
$this->expectException(ReaderException::class); |
34
|
|
|
$filename = __FILE__; |
35
|
|
|
$reader = new Html(); |
36
|
|
|
self::assertFalse($reader->canRead($filename)); |
37
|
|
|
$reader->load($filename); |
38
|
|
|
self::assertTrue(false); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testInvalidFilename(): void |
42
|
|
|
{ |
43
|
|
|
$reader = new Html(); |
44
|
|
|
self::assertEquals(0, $reader->getSheetIndex()); |
45
|
|
|
self::assertFalse($reader->canRead('')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function providerCanReadVerySmallFile() |
49
|
|
|
{ |
50
|
|
|
$padding = str_repeat('a', 2048); |
51
|
|
|
|
52
|
|
|
return [ |
53
|
|
|
[true, ' <html> ' . $padding . ' </html> '], |
54
|
|
|
[true, ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>' . $padding . '</html>'], |
55
|
|
|
[true, '<html></html>'], |
56
|
|
|
[false, ''], |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @dataProvider providerCanReadVerySmallFile |
62
|
|
|
* |
63
|
|
|
* @param bool $expected |
64
|
|
|
* @param string $content |
65
|
|
|
*/ |
66
|
|
|
public function testCanReadVerySmallFile($expected, $content): void |
67
|
|
|
{ |
68
|
|
|
$filename = HtmlHelper::createHtml($content); |
69
|
|
|
$reader = new Html(); |
70
|
|
|
$actual = $reader->canRead($filename); |
71
|
|
|
|
72
|
|
|
self::assertSame($expected, $actual); |
73
|
|
|
|
74
|
|
|
unlink($filename); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testBackgroundColorInRanding(): void |
78
|
|
|
{ |
79
|
|
|
$html = '<table> |
80
|
|
|
<tr> |
81
|
|
|
<td style="background-color: #0000FF;color: #FFFFFF">Blue background</td> |
82
|
|
|
<td style="background-color: unknown1;color: unknown2">Unknown fore/background</td> |
83
|
|
|
</tr> |
84
|
|
|
</table>'; |
85
|
|
|
$filename = HtmlHelper::createHtml($html); |
86
|
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true); |
87
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
88
|
|
|
$style = $firstSheet->getCell('A1')->getStyle(); |
89
|
|
|
self::assertEquals('FFFFFF', $style->getFont()->getColor()->getRGB()); |
90
|
|
|
self::assertEquals('0000FF', $style->getFill()->getStartColor()->getRGB()); |
91
|
|
|
self::assertEquals('0000FF', $style->getFill()->getEndColor()->getRGB()); |
92
|
|
|
$style = $firstSheet->getCell('B1')->getStyle(); |
93
|
|
|
self::assertEquals('000000', $style->getFont()->getColor()->getRGB()); |
94
|
|
|
self::assertEquals('000000', $style->getFill()->getEndColor()->getRGB()); |
95
|
|
|
self::assertEquals('FFFFFF', $style->getFill()->getstartColor()->getRGB()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testCanApplyInlineFontStyles(): void |
99
|
|
|
{ |
100
|
|
|
$html = '<table> |
101
|
|
|
<tr> |
102
|
|
|
<td style="font-size: 16px;">16px</td> |
103
|
|
|
<td style="font-family: \'Times New Roman\'">Times New Roman</td> |
104
|
|
|
<td style="font-weight: bold;">Bold</td> |
105
|
|
|
<td style="font-style: italic;">Italic</td> |
106
|
|
|
<td style="text-decoration: underline;">Underline</td> |
107
|
|
|
<td style="text-decoration: line-through;">Line through</td> |
108
|
|
|
</tr> |
109
|
|
|
</table>'; |
110
|
|
|
$filename = HtmlHelper::createHtml($html); |
111
|
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true); |
112
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
113
|
|
|
|
114
|
|
|
$style = $firstSheet->getCell('A1')->getStyle(); |
115
|
|
|
self::assertEquals(16, $style->getFont()->getSize()); |
116
|
|
|
|
117
|
|
|
$style = $firstSheet->getCell('B1')->getStyle(); |
118
|
|
|
self::assertEquals('Times New Roman', $style->getFont()->getName()); |
119
|
|
|
|
120
|
|
|
$style = $firstSheet->getCell('C1')->getStyle(); |
121
|
|
|
self::assertTrue($style->getFont()->getBold()); |
122
|
|
|
|
123
|
|
|
$style = $firstSheet->getCell('D1')->getStyle(); |
124
|
|
|
self::assertTrue($style->getFont()->getItalic()); |
125
|
|
|
|
126
|
|
|
$style = $firstSheet->getCell('E1')->getStyle(); |
127
|
|
|
self::assertEquals(Font::UNDERLINE_SINGLE, $style->getFont()->getUnderline()); |
128
|
|
|
|
129
|
|
|
$style = $firstSheet->getCell('F1')->getStyle(); |
130
|
|
|
self::assertTrue($style->getFont()->getStrikethrough()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testCanApplyInlineWidth(): void |
134
|
|
|
{ |
135
|
|
|
$html = '<table> |
136
|
|
|
<tr> |
137
|
|
|
<td width="50">50px</td> |
138
|
|
|
<td style="width: 100px;">100px</td> |
139
|
|
|
</tr> |
140
|
|
|
</table>'; |
141
|
|
|
$filename = HtmlHelper::createHtml($html); |
142
|
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true); |
143
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
144
|
|
|
|
145
|
|
|
$dimension = $firstSheet->getColumnDimension('A'); |
146
|
|
|
self::assertEquals(50, $dimension->getWidth()); |
147
|
|
|
|
148
|
|
|
$dimension = $firstSheet->getColumnDimension('B'); |
149
|
|
|
self::assertEquals(100, $dimension->getWidth()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testCanApplyInlineHeight(): void |
153
|
|
|
{ |
154
|
|
|
$html = '<table> |
155
|
|
|
<tr> |
156
|
|
|
<td height="50">1</td> |
157
|
|
|
</tr> |
158
|
|
|
<tr> |
159
|
|
|
<td style="height: 100px;">2</td> |
160
|
|
|
</tr> |
161
|
|
|
</table>'; |
162
|
|
|
$filename = HtmlHelper::createHtml($html); |
163
|
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true); |
164
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
165
|
|
|
|
166
|
|
|
$dimension = $firstSheet->getRowDimension(1); |
167
|
|
|
self::assertEquals(50, $dimension->getRowHeight()); |
168
|
|
|
|
169
|
|
|
$dimension = $firstSheet->getRowDimension(2); |
170
|
|
|
self::assertEquals(100, $dimension->getRowHeight()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testCanApplyAlignment(): void |
174
|
|
|
{ |
175
|
|
|
$html = '<table> |
176
|
|
|
<tr> |
177
|
|
|
<td align="center">Center align</td> |
178
|
|
|
<td valign="center">Center valign</td> |
179
|
|
|
<td style="text-align: center;">Center align</td> |
180
|
|
|
<td style="vertical-align: center;">Center valign</td> |
181
|
|
|
<td style="text-indent: 10px;">Text indent</td> |
182
|
|
|
<td style="word-wrap: break-word;">Wraptext</td> |
183
|
|
|
</tr> |
184
|
|
|
</table>'; |
185
|
|
|
$filename = HtmlHelper::createHtml($html); |
186
|
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true); |
187
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
188
|
|
|
|
189
|
|
|
$style = $firstSheet->getCell('A1')->getStyle(); |
190
|
|
|
self::assertEquals(Alignment::HORIZONTAL_CENTER, $style->getAlignment()->getHorizontal()); |
191
|
|
|
|
192
|
|
|
$style = $firstSheet->getCell('B1')->getStyle(); |
193
|
|
|
self::assertEquals(Alignment::VERTICAL_CENTER, $style->getAlignment()->getVertical()); |
194
|
|
|
|
195
|
|
|
$style = $firstSheet->getCell('C1')->getStyle(); |
196
|
|
|
self::assertEquals(Alignment::HORIZONTAL_CENTER, $style->getAlignment()->getHorizontal()); |
197
|
|
|
|
198
|
|
|
$style = $firstSheet->getCell('D1')->getStyle(); |
199
|
|
|
self::assertEquals(Alignment::VERTICAL_CENTER, $style->getAlignment()->getVertical()); |
200
|
|
|
|
201
|
|
|
$style = $firstSheet->getCell('E1')->getStyle(); |
202
|
|
|
self::assertEquals(10, $style->getAlignment()->getIndent()); |
203
|
|
|
|
204
|
|
|
$style = $firstSheet->getCell('F1')->getStyle(); |
205
|
|
|
self::assertTrue($style->getAlignment()->getWrapText()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testCanApplyInlineDataFormat(): void |
209
|
|
|
{ |
210
|
|
|
$html = '<table> |
211
|
|
|
<tr> |
212
|
|
|
<td data-format="mmm-yy">2019-02-02 12:34:00</td> |
213
|
|
|
</tr> |
214
|
|
|
</table>'; |
215
|
|
|
$filename = HtmlHelper::createHtml($html); |
216
|
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true); |
217
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
218
|
|
|
|
219
|
|
|
$style = $firstSheet->getCell('A1')->getStyle(); |
220
|
|
|
self::assertEquals('mmm-yy', $style->getNumberFormat()->getFormatCode()); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function testCanApplyCellWrapping(): void |
224
|
|
|
{ |
225
|
|
|
$html = '<table> |
226
|
|
|
<tr> |
227
|
|
|
<td>Hello World</td> |
228
|
|
|
</tr> |
229
|
|
|
<tr> |
230
|
|
|
<td>Hello<br />World</td> |
231
|
|
|
</tr> |
232
|
|
|
<tr> |
233
|
|
|
<td>Hello<br>World</td> |
234
|
|
|
</tr> |
235
|
|
|
</table>'; |
236
|
|
|
$filename = HtmlHelper::createHtml($html); |
237
|
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true); |
238
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
239
|
|
|
|
240
|
|
|
$cellStyle = $firstSheet->getStyle('A1'); |
241
|
|
|
self::assertFalse($cellStyle->getAlignment()->getWrapText()); |
242
|
|
|
|
243
|
|
|
$cellStyle = $firstSheet->getStyle('A2'); |
244
|
|
|
self::assertTrue($cellStyle->getAlignment()->getWrapText()); |
245
|
|
|
$cellValue = $firstSheet->getCell('A2')->getValue(); |
246
|
|
|
self::assertStringContainsString("\n", $cellValue); |
247
|
|
|
|
248
|
|
|
$cellStyle = $firstSheet->getStyle('A3'); |
249
|
|
|
self::assertTrue($cellStyle->getAlignment()->getWrapText()); |
250
|
|
|
$cellValue = $firstSheet->getCell('A3')->getValue(); |
251
|
|
|
self::assertStringContainsString("\n", $cellValue); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function testRowspanInRendering(): void |
255
|
|
|
{ |
256
|
|
|
$filename = 'tests/data/Reader/HTML/rowspan.html'; |
257
|
|
|
$reader = new Html(); |
258
|
|
|
$spreadsheet = $reader->load($filename); |
259
|
|
|
|
260
|
|
|
$actual = $spreadsheet->getActiveSheet()->getMergeCells(); |
261
|
|
|
self::assertSame(['A2:C2' => 'A2:C2'], $actual); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testTextIndentUseRowspan(): void |
265
|
|
|
{ |
266
|
|
|
$html = '<table> |
267
|
|
|
<tr> |
268
|
|
|
<td>1</td> |
269
|
|
|
<td rowspan="2" style="vertical-align: center;">Center Align</td> |
270
|
|
|
<td>Row</td> |
271
|
|
|
</tr> |
272
|
|
|
<tr> |
273
|
|
|
<td>2</td> |
274
|
|
|
<td style="text-indent:10px">Text Indent</td> |
275
|
|
|
</tr> |
276
|
|
|
</table>'; |
277
|
|
|
$filename = HtmlHelper::createHtml($html); |
278
|
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true); |
279
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
280
|
|
|
$style = $firstSheet->getCell('C2')->getStyle(); |
281
|
|
|
self::assertEquals(10, $style->getAlignment()->getIndent()); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public function testBorderWithRowspanAndColspan(): void |
285
|
|
|
{ |
286
|
|
|
$html = '<table> |
287
|
|
|
<tr> |
288
|
|
|
<td style="border: 1px solid black;">NOT SPANNED</td> |
289
|
|
|
<td rowspan="2" colspan="2" style="border: 1px solid black;">SPANNED</td> |
290
|
|
|
</tr> |
291
|
|
|
<tr> |
292
|
|
|
<td style="border: 1px solid black;">NOT SPANNED</td> |
293
|
|
|
</tr> |
294
|
|
|
</table>'; |
295
|
|
|
|
296
|
|
|
$reader = new Html(); |
297
|
|
|
$spreadsheet = $reader->loadFromString($html); |
298
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
299
|
|
|
$style = $firstSheet->getStyle('B1:C2'); |
300
|
|
|
|
301
|
|
|
$borders = $style->getBorders(); |
302
|
|
|
|
303
|
|
|
$totalBorders = [ |
304
|
|
|
$borders->getTop(), |
305
|
|
|
$borders->getLeft(), |
306
|
|
|
$borders->getBottom(), |
307
|
|
|
$borders->getRight(), |
308
|
|
|
]; |
309
|
|
|
|
310
|
|
|
foreach ($totalBorders as $border) { |
311
|
|
|
self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|