Skip to content

Commit

Permalink
Merge pull request #29 from stof/fix_entity_conversion
Browse files Browse the repository at this point in the history
Fix the handling of Unicode characters
  • Loading branch information
lorenzo committed Jan 12, 2023
2 parents ffa44ca + 1eedec4 commit f890472
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/pinky.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function loadTemplateString($html)
{
$document = new DOMDocument('1.0', 'UTF-8');
$internalErrors = libxml_use_internal_errors(true);
$document->loadHTML(htmlspecialchars_decode(htmlentities($html)));
$document->loadHTML(mb_encode_numericentity($html, [0x80, 0x10FFFF, 0, 0x1FFFFF], 'UTF-8'));
libxml_use_internal_errors($internalErrors);
$document->formatOutput = true;
return $document;
Expand Down
34 changes: 34 additions & 0 deletions tests/EncodingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
use PHPUnit\Framework\TestCase;

use function Pinky\transformString;

class EncodingTest extends TestCase
{
/**
* @param string $expected
* @param string $input
*
* @dataProvider provideStrings
*/
public function testMultiByteContent($expected, $input)
{
$this->assertEquals($expected, trim(transformString($input)->saveHTML()));
}

public static function provideStrings()
{
yield [
'<html><body><p>ASCII only</p></body></html>',
'ASCII only',
];
yield [
'<html><body><p>Twoje zam&oacute;wienie oczekuje na wp&#322;at&#281; zadatku &#127475;&#127473;</p></body></html>',
'Twoje zamówienie oczekuje na wpłatę zadatku 🇳🇱',
];
yield [
'<html><body><p>&#1055;&#1088;&#1080;&#1074;&#1077;&#1090; &#1084;&#1080;&#1088;!</p></body></html>',
'Привет мир!',
];
}
}

0 comments on commit f890472

Please sign in to comment.