bug #2427 Fixed JS escaping for unicode characters with higher code points (mikealmond)

This PR was submitted for the 2.x branch but it was merged into the 1.x branch instead (closes #2427).

Discussion
----------

Fixed JS escaping for unicode characters with higher code points

Unicode characters with higher code points were being escaped incorrectly. When these characters are escaped, they should maintain their surrogate halves. Previously, Twig was dropping the first surrogate half.

https://mathiasbynens.be/notes/javascript-escapes#unicode-code-point
> The tetragram for centre symbol (𝌆) has code point U+1D306, so you could write it as \u{1D306}. For comparison, if you were to use simple Unicode escapes to represent this symbol, you’d have to write out the surrogate halves separately: '\uD834\uDF06'.

Commits
-------

7f913495 Fixed JS escaping for unicode characters with higher code points
This commit is contained in:
Fabien Potencier
2017-03-17 10:19:15 -07:00
3 changed files with 15 additions and 2 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
* 1.32.1 (2017-XX-XX)
* n/a
* fixed JS escaping for unicode characters with higher code points
* 1.32.0 (2017-02-26)
+6 -1
View File
@@ -1168,8 +1168,13 @@ function _twig_escape_js_callback($matches)
// \uHHHH
$char = twig_convert_encoding($char, 'UTF-16BE', 'UTF-8');
$char = strtoupper(bin2hex($char));
return '\\u'.strtoupper(substr('0000'.bin2hex($char), -4));
if (4 >= strlen($char)) {
return sprintf('\u%04s', $char);
}
return sprintf('\u%04s\u%04s', substr($char, 0, -4), substr($char, -4));
}
function _twig_escape_css_callback($matches)
@@ -0,0 +1,8 @@
--TEST--
"escape" filter
--TEMPLATE--
{{ "é ♜ 𝌆"|e('js') }}
--DATA--
return array()
--EXPECT--
\u00E9\x20\u265C\x20\uD834\uDF06