Skip to content

Commit a88ceea

Browse files
authored
Merge pull request #6672 from kenjis/fix-DOMParser-withString-php82
refactor: DOMParser::withString() for PHP 8.2
2 parents e68b67f + a83d410 commit a88ceea

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

system/Test/DOMParser.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ public function getBody(): string
6161
*/
6262
public function withString(string $content)
6363
{
64-
// converts all special characters to utf-8
65-
$content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8');
64+
// DOMDocument::loadHTML() will treat your string as being in ISO-8859-1
65+
// (the HTTP/1.1 default character set) unless you tell it otherwise.
66+
// https://stackoverflow.com/a/8218649
67+
// So encode characters to HTML numeric string references.
68+
$content = mb_encode_numericentity($content, [0x80, 0x10FFFF, 0, 0x1FFFFF], 'UTF-8');
6669

6770
// turning off some errors
6871
libxml_use_internal_errors(true);
@@ -86,7 +89,7 @@ public function withString(string $content)
8689
* Loads the contents of a file as a string
8790
* so that we can work with it.
8891
*
89-
* @return DOMParser
92+
* @return $this
9093
*/
9194
public function withFile(string $path)
9295
{
@@ -181,7 +184,7 @@ public function seeCheckboxIsChecked(string $element): bool
181184
/**
182185
* Search the DOM using an XPath expression.
183186
*
184-
* @return DOMNodeList
187+
* @return DOMNodeList|false
185188
*/
186189
protected function doXPath(?string $search, string $element, array $paths = [])
187190
{

tests/system/Test/DOMParserTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ public function testParseSelectorWithAttribute()
8080
public function provideText()
8181
{
8282
return [
83-
['Hello World'],
84-
['Hellö Wörld'],
83+
'en' => ['Hello World'],
84+
'sv' => ['Hej, världen'],
85+
'ja' => ['こんにちは、世界'],
8586
];
8687
}
8788

0 commit comments

Comments
 (0)