Skip to content

Commit

Permalink
Remove deprecated usage of function (#310)
Browse files Browse the repository at this point in the history
* remove deprecated usage of function

* remove iconv extension, add comments and tests

* Remove php 8.1  (#308)

* Fix tests

* Drop composer dev flag

* Support css-selector 6

* Drop php 8.1

* Update changelog

* refactor tests

* remove deprecated usage of function

* remove iconv extension, add comments and tests

* refactor tests

* fix code style

---------

Co-authored-by: Ivan Shcherbak <[email protected]>
  • Loading branch information
SergeiMV and funivan authored Dec 19, 2024
1 parent d7342ef commit bfdb9e6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/ElementFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,19 @@ private function setData(string $data): self

if (static::DOCUMENT_HTML === $this->type) {
$data = StringHelper::safeEncodeStr($data);
$data = mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8');

//Analogue of mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8')
//Usage of mb_convert_encoding with encoding to HTML_ENTITIES is deprecated since php version 8.2
//When passing data to ElementFinder in an encoding other than UTF-8, any unrecognized characters will be ignored
$data = mb_encode_numericentity(
htmlspecialchars_decode(
htmlentities($data, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8', false),
ENT_NOQUOTES
),
[0x80, 0x10FFFF, 0, ~0],
'UTF-8'
);

$this->dom->loadHTML($data, LIBXML_NOCDATA & LIBXML_NOERROR);
} elseif (static::DOCUMENT_XML === $this->type) {
$this->dom->loadXML($data, LIBXML_NOCDATA & LIBXML_NOERROR);
Expand Down
27 changes: 27 additions & 0 deletions tests/ElementFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Test\Xparse\ElementFinder;

use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Test\Xparse\ElementFinder\Dummy\ItemsByClassExpressionTranslator;
Expand Down Expand Up @@ -519,4 +520,30 @@ private function getValidXml(): string
</breakfast_menu>
';
}

/**
* @return string[][]
*/
public static function getDifferentEncodingsSupportDataProvider(): array
{
return [
[
'<body>Текст текст text</body>',
'Текст текст text',
],
[
'<body>&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; text</body>',
' text',
],
];
}

#[DataProvider('getDifferentEncodingsSupportDataProvider')]
public function testDifferentEncodingsSupport(string $html, string $bodyText): void
{
$page = new ElementFinder($html);
$pageBodyText = $page->content('//body')->first();
self::assertInstanceOf(ElementFinder::class, $page);
self::assertEquals($bodyText, $pageBodyText);
}
}

0 comments on commit bfdb9e6

Please sign in to comment.