Skip to content

refactor: DOMParser::withString() for PHP 8.2 #6672

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

Merged
merged 5 commits into from
Oct 25, 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
11 changes: 7 additions & 4 deletions system/Test/DOMParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ public function getBody(): string
*/
public function withString(string $content)
{
// converts all special characters to utf-8
$content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8');
// DOMDocument::loadHTML() will treat your string as being in ISO-8859-1
// (the HTTP/1.1 default character set) unless you tell it otherwise.
// https://stackoverflow.com/a/8218649
// So encode characters to HTML numeric string references.
$content = mb_encode_numericentity($content, [0x80, 0x10FFFF, 0, 0x1FFFFF], 'UTF-8');

// turning off some errors
libxml_use_internal_errors(true);
Expand All @@ -86,7 +89,7 @@ public function withString(string $content)
* Loads the contents of a file as a string
* so that we can work with it.
*
* @return DOMParser
* @return $this
*/
public function withFile(string $path)
{
Expand Down Expand Up @@ -181,7 +184,7 @@ public function seeCheckboxIsChecked(string $element): bool
/**
* Search the DOM using an XPath expression.
*
* @return DOMNodeList
* @return DOMNodeList|false
*/
protected function doXPath(?string $search, string $element, array $paths = [])
{
Expand Down
5 changes: 3 additions & 2 deletions tests/system/Test/DOMParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public function testParseSelectorWithAttribute()
public function provideText()
{
return [
['Hello World'],
['Hellö Wörld'],
'en' => ['Hello World'],
'sv' => ['Hej, världen'],
'ja' => ['こんにちは、世界'],
];
}

Expand Down