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.
This commit is contained in:
oleibman 2022-05-10 07:33:45 -07:00 committed by GitHub
parent b0bfdde164
commit 070bc68514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 66 additions and 5 deletions

View File

@ -20,7 +20,7 @@ trait ArrayEnabled
if (self::$arrayArgumentHelper === null) {
self::$arrayArgumentHelper = new ArrayArgumentHelper();
}
self::$arrayArgumentHelper->initialise($arguments ?: []);
self::$arrayArgumentHelper->initialise(($arguments === false) ? [] : $arguments);
}
/**

View File

@ -795,7 +795,7 @@ class Html
$domText = preg_replace(
'/\s+/u',
' ',
str_replace(["\r", "\n"], ' ', $textNode->nodeValue ?: '')
str_replace(["\r", "\n"], ' ', $textNode->nodeValue ?? '')
);
$this->stringData .= $domText;
$this->buildTextRun();

View File

@ -619,7 +619,7 @@ class Html extends BaseReader
{
foreach ($element->childNodes as $child) {
if ($child instanceof DOMText) {
$domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue ?: ''));
$domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue ?? ''));
if (is_string($cellContent)) {
// simply append the text if the cell content is a plain text string
$cellContent .= $domText;

View File

@ -325,7 +325,7 @@ class Ods extends BaseReader
}
$spreadsheet->setActiveSheetIndex($worksheetID);
if ($worksheetName) {
if ($worksheetName || is_numeric($worksheetName)) {
// Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in
// formula cells... during the load, all formulae should be correct, and we're simply
// bringing the worksheet name in line with the formula, not the reverse
@ -628,7 +628,7 @@ class Ods extends BaseReader
foreach ($settings->getElementsByTagNameNS($configNs, 'config-item') as $t) {
if ($t->getAttributeNs($configNs, 'name') === 'ActiveTable') {
try {
$spreadsheet->setActiveSheetIndexByName($t->nodeValue ?: '');
$spreadsheet->setActiveSheetIndexByName($t->nodeValue ?? '');
} catch (Throwable $e) {
// do nothing
}

View File

@ -28,6 +28,7 @@ class HtmlTest extends TestCase
['können', 'können'],
['русский', 'русский'],
["foo\nbar", '<p>foo</p><p>bar</p>'],
'issue2810' => ['0', '0'],
];
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;
use PhpOffice\PhpSpreadsheet\Reader\Html;
use PHPUnit\Framework\TestCase;
class Issue2810Test extends TestCase
{
// Reader has been converting falsey values to null
public function testIssue2810(): void
{
$content = <<<'EOF'
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Declaracion en Linea</title>
</head>
<body>
<table>
<tr>
<td>1</td>
<td>0</td>
<td>2</td>
</tr>
</table>
</body>
</html>
EOF;
$reader = new Html();
$spreadsheet = $reader->loadFromString($content);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame(1, $sheet->getCell('A1')->getValue());
self::assertSame(0, $sheet->getCell('B1')->getValue());
self::assertSame(2, $sheet->getCell('C1')->getValue());
$spreadsheet->disconnectWorksheets();
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Reader\Ods;
use PHPUnit\Framework\TestCase;
class Issue2810Test extends TestCase
{
public function testIssue2810(): void
{
// Active sheet with title of '0' wasn't found
$filename = 'tests/data/Reader/Ods/issue.2810.ods';
$reader = new Ods();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('Active', $sheet->getCell('A1')->getValue());
$spreadsheet->disconnectWorksheets();
}
}

Binary file not shown.