Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Html Reader Not Handling non-ASCII Data Correctly #2943

Merged
merged 6 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private static function containsTags(string $data): bool
/**
* Loads Spreadsheet from file.
*/
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
public function loadSpreadsheetFromFile(string $filename): Spreadsheet
{
// Create new Spreadsheet
$spreadsheet = new Spreadsheet();
Expand Down Expand Up @@ -651,7 +651,13 @@ public function loadIntoExisting($filename, Spreadsheet $spreadsheet)
// Reload the HTML file into the DOM object
try {
$convert = $this->securityScanner->scanFile($filename);
$loaded = $dom->loadHTML($convert);
$lowend = "\u{80}";
$highend = "\u{10ffff}";
$regexp = "/[$lowend-$highend]/u";
/** @var callable */
$callback = [self::class, 'replaceNonAscii'];
$convert = preg_replace_callback($regexp, $callback, $convert);
$loaded = ($convert === null) ? false : $dom->loadHTML($convert);
} catch (Throwable $e) {
$loaded = false;
}
Expand All @@ -662,6 +668,11 @@ public function loadIntoExisting($filename, Spreadsheet $spreadsheet)
return $this->loadDocument($dom, $spreadsheet);
}

private static function replaceNonAscii(array $matches): string
{
return '&#' . mb_ord($matches[0], 'UTF-8') . ';';
}

/**
* Spreadsheet from content.
*
Expand All @@ -674,7 +685,13 @@ public function loadFromString($content, ?Spreadsheet $spreadsheet = null): Spre
// Reload the HTML file into the DOM object
try {
$convert = $this->securityScanner->scan($content);
$loaded = $dom->loadHTML($convert);
$lowend = "\u{80}";
$highend = "\u{10ffff}";
$regexp = "/[$lowend-$highend]/u";
/** @var callable */
$callback = [self::class, 'replaceNonAscii'];
$convert = preg_replace_callback($regexp, $callback, $convert);
$loaded = ($convert === null) ? false : $dom->loadHTML($convert);
} catch (Throwable $e) {
$loaded = false;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PhpSpreadsheetTests/Reader/Html/HtmlImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testCanInsertImage(): void

$html = '<table>
<tr>
<td><img src="' . $imagePath . '" alt="test image"></td>
<td><img src="' . $imagePath . '" alt="test image voilà"></td>
</tr>
</table>';
$filename = HtmlHelper::createHtml($html);
Expand All @@ -24,7 +24,7 @@ public function testCanInsertImage(): void
$drawing = $firstSheet->getDrawingCollection()[0];
self::assertEquals($imagePath, $drawing->getPath());
self::assertEquals('A1', $drawing->getCoordinates());
self::assertEquals('test image', $drawing->getName());
self::assertEquals('test image voilà', $drawing->getName());
self::assertEquals('100', $drawing->getWidth());
self::assertEquals('100', $drawing->getHeight());
}
Expand Down
36 changes: 36 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Html/Issue2942Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;

use PhpOffice\PhpSpreadsheet\Reader\Html;
use PHPUnit\Framework\TestCase;

class Issue2942Test extends TestCase
{
public function testLoadFromString(): void
{
$content = '<table><tbody><tr><td>éàâèî</td></tr></tbody></table>';
$reader = new Html();
$spreadsheet = $reader->loadFromString($content);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('éàâèî', $sheet->getCell('A1')->getValue());
}

public function testLoadFromFile(): void
{
$file = 'tests/data/Reader/HTML/utf8chars.html';
$reader = new Html();
$spreadsheet = $reader->loadSpreadsheetFromFile($file);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('Test Utf-8 characters voilà', $sheet->getTitle());
self::assertSame('éàâèî', $sheet->getCell('A1')->getValue());
self::assertSame('αβγδε', $sheet->getCell('B1')->getValue());
self::assertSame('𐐁𐐂𐐃 & だけち', $sheet->getCell('A2')->getValue());
self::assertSame('אבגדה', $sheet->getCell('B2')->getValue());
self::assertSame('𪔀𪔁𪔂', $sheet->getCell('C2')->getValue());
self::assertSame('᠐᠑᠒', $sheet->getCell('A3')->getValue());
self::assertSame('അആ', $sheet->getCell('B3')->getValue());
self::assertSame('กขฃ', $sheet->getCell('C3')->getValue());
self::assertSame('✀✐✠', $sheet->getCell('D3')->getValue());
}
}
28 changes: 28 additions & 0 deletions tests/data/Reader/HTML/utf8chars.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<!-- deliberately do not identify charset for this test -->
<title>Test Utf-8 characters voilà</title>
</head>
<body>
<table>
<tbody>
<tr>
<td>éàâèî</td><!-- Latin1 -->
<td>αβγδε</td><!-- Greek -->
</tr>
<tr>
<td>𐐁𐐂𐐃 &amp; だけち</td><!-- Osmanya (not in BMP) and Hiragana -->
<td>אבגדה</td><!-- Hebrew -->
<td>𪔀𪔁𪔂</td><!-- CJK Unified Ideographs Extension B (not in BMP) -->
</tr>
<tr>
<td>᠐᠑᠒</td><!-- Mongolian -->
<td>അആ</td><!-- Malayalam -->
<td>กขฃ</td><!-- Thai -->
<td>✀✐✠</td><!-- Dingbats -->
</tr>
</tbody>
</table>
</body>
</html>