Skip to content

Commit 070bc68

Browse files
authored
Html Reader Converting Cell Containing 0 to Null String (#2813)
Fix #2810. Repairing some Phpstan diagnostics, used `?:` rather than `??` in a few places. 2 different Html modules are affected. Also, Ods Reader, but its problem is with sheet title rather than cell contents. And, as it turns out, Ods Reader was already not handling sheets with a title of `0` correctly - it made a truthy test before setting sheet title. That is now changed to truthy or numeric. Other readers are not susceptible to this problem. Tests are added.
1 parent b0bfdde commit 070bc68

File tree

8 files changed

+66
-5
lines changed

8 files changed

+66
-5
lines changed

src/PhpSpreadsheet/Calculation/ArrayEnabled.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private static function initialiseHelper($arguments): void
2020
if (self::$arrayArgumentHelper === null) {
2121
self::$arrayArgumentHelper = new ArrayArgumentHelper();
2222
}
23-
self::$arrayArgumentHelper->initialise($arguments ?: []);
23+
self::$arrayArgumentHelper->initialise(($arguments === false) ? [] : $arguments);
2424
}
2525

2626
/**

src/PhpSpreadsheet/Helper/Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ protected function parseTextNode(DOMText $textNode): void
795795
$domText = preg_replace(
796796
'/\s+/u',
797797
' ',
798-
str_replace(["\r", "\n"], ' ', $textNode->nodeValue ?: '')
798+
str_replace(["\r", "\n"], ' ', $textNode->nodeValue ?? '')
799799
);
800800
$this->stringData .= $domText;
801801
$this->buildTextRun();

src/PhpSpreadsheet/Reader/Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ protected function processDomElement(DOMNode $element, Worksheet $sheet, int &$r
619619
{
620620
foreach ($element->childNodes as $child) {
621621
if ($child instanceof DOMText) {
622-
$domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue ?: ''));
622+
$domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue ?? ''));
623623
if (is_string($cellContent)) {
624624
// simply append the text if the cell content is a plain text string
625625
$cellContent .= $domText;

src/PhpSpreadsheet/Reader/Ods.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ public function loadIntoExisting($filename, Spreadsheet $spreadsheet)
325325
}
326326
$spreadsheet->setActiveSheetIndex($worksheetID);
327327

328-
if ($worksheetName) {
328+
if ($worksheetName || is_numeric($worksheetName)) {
329329
// Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in
330330
// formula cells... during the load, all formulae should be correct, and we're simply
331331
// bringing the worksheet name in line with the formula, not the reverse
@@ -628,7 +628,7 @@ private function lookForActiveSheet(DOMElement $settings, Spreadsheet $spreadshe
628628
foreach ($settings->getElementsByTagNameNS($configNs, 'config-item') as $t) {
629629
if ($t->getAttributeNs($configNs, 'name') === 'ActiveTable') {
630630
try {
631-
$spreadsheet->setActiveSheetIndexByName($t->nodeValue ?: '');
631+
$spreadsheet->setActiveSheetIndexByName($t->nodeValue ?? '');
632632
} catch (Throwable $e) {
633633
// do nothing
634634
}

tests/PhpSpreadsheetTests/Helper/HtmlTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function providerUtf8EncodingSupport(): array
2828
['können', 'können'],
2929
['русский', 'русский'],
3030
["foo\nbar", '<p>foo</p><p>bar</p>'],
31+
'issue2810' => ['0', '0'],
3132
];
3233
}
3334
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Html;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class Issue2810Test extends TestCase
9+
{
10+
// Reader has been converting falsey values to null
11+
public function testIssue2810(): void
12+
{
13+
$content = <<<'EOF'
14+
<!DOCTYPE html>
15+
<html>
16+
<head>
17+
<meta charset='utf-8'>
18+
<title>Declaracion en Linea</title>
19+
</head>
20+
<body>
21+
<table>
22+
<tr>
23+
<td>1</td>
24+
<td>0</td>
25+
<td>2</td>
26+
</tr>
27+
</table>
28+
</body>
29+
</html>
30+
31+
EOF;
32+
$reader = new Html();
33+
$spreadsheet = $reader->loadFromString($content);
34+
$sheet = $spreadsheet->getActiveSheet();
35+
self::assertSame(1, $sheet->getCell('A1')->getValue());
36+
self::assertSame(0, $sheet->getCell('B1')->getValue());
37+
self::assertSame(2, $sheet->getCell('C1')->getValue());
38+
$spreadsheet->disconnectWorksheets();
39+
}
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Ods;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class Issue2810Test extends TestCase
9+
{
10+
public function testIssue2810(): void
11+
{
12+
// Active sheet with title of '0' wasn't found
13+
$filename = 'tests/data/Reader/Ods/issue.2810.ods';
14+
$reader = new Ods();
15+
$spreadsheet = $reader->load($filename);
16+
$sheet = $spreadsheet->getActiveSheet();
17+
self::assertSame('Active', $sheet->getCell('A1')->getValue());
18+
$spreadsheet->disconnectWorksheets();
19+
}
20+
}

tests/data/Reader/Ods/issue.2810.ods

8.19 KB
Binary file not shown.

0 commit comments

Comments
 (0)