Replace implicit dependence on ext/iconv in JS escaper

polyfill-iconv does not support the UTF-16BE conversion
This commit is contained in:
JoshyPHP
2020-09-13 19:23:45 +02:00
parent b48bd18dc6
commit b77c2c40b4
+10 -7
View File
@@ -282,15 +282,18 @@ function twig_escape_filter(Environment $env, $string, $strategy = 'html', $char
return $shortMap[$char]; return $shortMap[$char];
} }
// \uHHHH $codepoint = mb_ord($char);
$char = twig_convert_encoding($char, 'UTF-16BE', 'UTF-8'); if (0x10000 > $codepoint) {
$char = strtoupper(bin2hex($char)); return sprintf('\u%04X', $codepoint);
if (4 >= \strlen($char)) {
return sprintf('\u%04s', $char);
} }
return sprintf('\u%04s\u%04s', substr($char, 0, -4), substr($char, -4)); // Split characters outside the BMP into surrogate pairs
// https://tools.ietf.org/html/rfc2781.html#section-2.1
$u = $codepoint - 0x10000;
$high = 0xD800 | ($u >> 10);
$low = 0xDC00 | ($u & 0x3FF);
return sprintf('\u%04X\u%04X', $high, $low);
}, $string); }, $string);
if ('UTF-8' !== $charset) { if ('UTF-8' !== $charset) {