Files
oleibman 5de82981d8 Html Reader Not Handling non-ASCII Data Correctly (#2943)
* Html Reader Not Handling non-ASCII Data Correctly

Fix #2942. Code was changed by #2894 because PHP8.2 will deprecate how it was being done. See linked issue for more details. Dom loadhtml assumes ISO-8859-1 in the absence of a charset attribute or equivalent, and there is no way to override that assumption. Sigh. The suggested replacements are unsuitable in one way or another. I think this will work with minimal disruption (replace ampersand, less than, and greater than with entities representing illegal characters, then use htmlentities, then restore ampersand, less than, and greater than).

* Better Implementation

Use regexp to escape non-ASCII. Less kludgey, less reliant on the vagaries of the PHP maintainers.

* Additional Tests

Test non-ASCII outside of cell contents: sheet title, image alt attribute.

* Apply Same Change in Second Location

Forgot to change loadFromString.

* Additional Test

Confirm escaped ampersand is handled correctly.
2022-07-16 22:08:44 -07:00

37 lines
1.5 KiB
PHP

<?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());
}
}