Skip to content
This repository has been archived by the owner on Jan 8, 2021. It is now read-only.

Commit

Permalink
Fix mbstring shim for html-entities
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jan 12, 2015
1 parent f26cc98 commit ffa0821
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.1.28 (2015-01-12)

- fix mbstring shim for html-entities

## v1.1.27 (2015-01-11)

- update to Unicode 7.0
Expand Down
22 changes: 21 additions & 1 deletion class/Patchwork/PHP/Shim/Mbstring.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,27 @@ protected static function getSubpart($pos, $part, $haystack, $encoding)

protected static function html_encoding_callback($m)
{
return htmlentities($m[0], ENT_COMPAT, 'UTF-8');
$i = 1;
$entities = '';
$m = unpack('C*', htmlentities($m[0], ENT_COMPAT, 'UTF-8'));

while (isset($m[$i])) {
if (0x80 > $m[$i]) {
$entities .= chr($m[$i++]);
continue;
}
if (0xF0 <= $m[$i]) {
$c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
} elseif (0xE0 <= $m[$i]) {
$c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
} else {
$c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80;
}

$entities .= '&#'.$c.';';
}

return $entities;
}

protected static function title_case_lower($s)
Expand Down
4 changes: 2 additions & 2 deletions tests/Patchwork/Tests/PHP/Shim/MbstringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ function testmb_convert_encoding()
{
$this->assertSame( utf8_decode('déjà'), p::mb_convert_encoding('déjà', 'Windows-1252') );
$this->assertSame( base64_encode('déjà'), p::mb_convert_encoding('déjà', 'Base64') );
$this->assertSame( 'd&eacute;j&agrave;', p::mb_convert_encoding('déjà', 'Html-entities') );
$this->assertSame( '&#23455;<&>d&eacute;j&agrave;', p::mb_convert_encoding('実<&>déjà', 'Html-entities') );
$this->assertSame( 'déjà', p::mb_convert_encoding(base64_encode('déjà'), 'Utf-8', 'Base64') );
$this->assertSame( 'déjà', p::mb_convert_encoding('d&eacute;j&agrave;', 'Utf-8', 'Html-entities') );
$this->assertSame( 'déjà', p::mb_convert_encoding('d&eacute;j&#224;', 'Utf-8', 'Html-entities') );
}

/**
Expand Down

0 comments on commit ffa0821

Please sign in to comment.