Skip to content

Commit bfdb9e6

Browse files
SergeiMVfunivan
andauthored
Remove deprecated usage of function (#310)
* 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]>
1 parent d7342ef commit bfdb9e6

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/ElementFinder.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,19 @@ private function setData(string $data): self
214214

215215
if (static::DOCUMENT_HTML === $this->type) {
216216
$data = StringHelper::safeEncodeStr($data);
217-
$data = mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8');
217+
218+
//Analogue of mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8')
219+
//Usage of mb_convert_encoding with encoding to HTML_ENTITIES is deprecated since php version 8.2
220+
//When passing data to ElementFinder in an encoding other than UTF-8, any unrecognized characters will be ignored
221+
$data = mb_encode_numericentity(
222+
htmlspecialchars_decode(
223+
htmlentities($data, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8', false),
224+
ENT_NOQUOTES
225+
),
226+
[0x80, 0x10FFFF, 0, ~0],
227+
'UTF-8'
228+
);
229+
218230
$this->dom->loadHTML($data, LIBXML_NOCDATA & LIBXML_NOERROR);
219231
} elseif (static::DOCUMENT_XML === $this->type) {
220232
$this->dom->loadXML($data, LIBXML_NOCDATA & LIBXML_NOERROR);

tests/ElementFinderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Test\Xparse\ElementFinder;
66

77
use InvalidArgumentException;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use PHPUnit\Framework\TestCase;
910
use RuntimeException;
1011
use Test\Xparse\ElementFinder\Dummy\ItemsByClassExpressionTranslator;
@@ -519,4 +520,30 @@ private function getValidXml(): string
519520
</breakfast_menu>
520521
';
521522
}
523+
524+
/**
525+
* @return string[][]
526+
*/
527+
public static function getDifferentEncodingsSupportDataProvider(): array
528+
{
529+
return [
530+
[
531+
'<body>Текст текст text</body>',
532+
'Текст текст text',
533+
],
534+
[
535+
'<body>&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; text</body>',
536+
' text',
537+
],
538+
];
539+
}
540+
541+
#[DataProvider('getDifferentEncodingsSupportDataProvider')]
542+
public function testDifferentEncodingsSupport(string $html, string $bodyText): void
543+
{
544+
$page = new ElementFinder($html);
545+
$pageBodyText = $page->content('//body')->first();
546+
self::assertInstanceOf(ElementFinder::class, $page);
547+
self::assertEquals($bodyText, $pageBodyText);
548+
}
522549
}

0 commit comments

Comments
 (0)