Skip to content

Commit 785705b

Browse files
vipmaaPowerKiKi
authored andcommitted
Best effort to support invalid colspan values in HTML reader
Closes #878
1 parent d6b3514 commit 785705b

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2323
- COUPNUM should not return zero when settlement is in the last period - [Issue #1020](https://github.com/PHPOffice/PhpSpreadsheet/issues/1020) and [PR #1021](https://github.com/PHPOffice/PhpSpreadsheet/pull/1021)
2424
- Fix handling of named ranges referencing sheets with spaces or "!" in their title
2525
- Cover `getSheetByName()` with tests for name with quote and spaces - [#739](https://github.com/PHPOffice/PhpSpreadsheet/issues/739)
26+
- Best effort to support invalid colspan values in HTML reader - [878](https://github.com/PHPOffice/PhpSpreadsheet/pull/878)
2627

2728
## [1.8.2] - 2019-07-08
2829

src/PhpSpreadsheet/Reader/Html.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -502,26 +502,26 @@ protected function processDomElement(DOMNode $element, Worksheet $sheet, &$row,
502502
if (isset($attributeArray['rowspan'], $attributeArray['colspan'])) {
503503
//create merging rowspan and colspan
504504
$columnTo = $column;
505-
for ($i = 0; $i < $attributeArray['colspan'] - 1; ++$i) {
505+
for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) {
506506
++$columnTo;
507507
}
508-
$range = $column . $row . ':' . $columnTo . ($row + $attributeArray['rowspan'] - 1);
508+
$range = $column . $row . ':' . $columnTo . ($row + (int) $attributeArray['rowspan'] - 1);
509509
foreach (Coordinate::extractAllCellReferencesInRange($range) as $value) {
510510
$this->rowspan[$value] = true;
511511
}
512512
$sheet->mergeCells($range);
513513
$column = $columnTo;
514514
} elseif (isset($attributeArray['rowspan'])) {
515515
//create merging rowspan
516-
$range = $column . $row . ':' . $column . ($row + $attributeArray['rowspan'] - 1);
516+
$range = $column . $row . ':' . $column . ($row + (int) $attributeArray['rowspan'] - 1);
517517
foreach (Coordinate::extractAllCellReferencesInRange($range) as $value) {
518518
$this->rowspan[$value] = true;
519519
}
520520
$sheet->mergeCells($range);
521521
} elseif (isset($attributeArray['colspan'])) {
522522
//create merging colspan
523523
$columnTo = $column;
524-
for ($i = 0; $i < $attributeArray['colspan'] - 1; ++$i) {
524+
for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) {
525525
++$columnTo;
526526
}
527527
$sheet->mergeCells($column . $row . ':' . $columnTo . $row);

tests/PhpSpreadsheetTests/Reader/HtmlTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function providerCanReadVerySmallFile()
3333
/**
3434
* @dataProvider providerCanReadVerySmallFile
3535
*
36-
* @param bool $expected
36+
* @param bool $expected
3737
* @param string $content
3838
*/
3939
public function testCanReadVerySmallFile($expected, $content)
@@ -321,4 +321,14 @@ private function loadHtmlIntoSpreadsheet($filename)
321321
{
322322
return (new Html())->load($filename);
323323
}
324+
325+
public function testRowspanInRendering()
326+
{
327+
$filename = './data/Reader/HTML/rowspan.html';
328+
$reader = new Html();
329+
$spreadsheet = $reader->load($filename);
330+
331+
$actual = $spreadsheet->getActiveSheet()->getMergeCells();
332+
self::assertSame(['A2:C2' => 'A2:C2'], $actual);
333+
}
324334
}

tests/data/Reader/HTML/rowspan.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<table>
2+
<tbody>
3+
<tr>
4+
<td>A1</td>
5+
<td>B1</td>
6+
<td>C1</td>
7+
<td>D1</td>
8+
</tr>
9+
<tr>
10+
<td colspan='3"'>A2 with invalid colspan</td>
11+
<td>D2<td>
12+
</tr>
13+
</tbody>
14+
</table>

0 commit comments

Comments
 (0)