Skip to content

Commit 2a7f1e9

Browse files
authored
Fix calculation of Unicode surrogate pairs
See section 3.7 of Unicode 3.0 (later versions are not as clear regarding how to do the surrogate pairs calculation): https://www.unicode.org/versions/Unicode3.0.0/ch03.pdf N = (H - D800_16) * 400_16 + (L - DC00_16) + 10000_16. 10000_16 is 2^16, not 2^20.
1 parent b3e508f commit 2a7f1e9

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

parse-css.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function preprocess(str) {
4747
// Decode a surrogate pair into an astral codepoint.
4848
var lead = code - 0xd800;
4949
var trail = str.charCodeAt(i+1) - 0xdc00;
50-
code = Math.pow(2, 20) + lead * Math.pow(2, 10) + trail;
50+
code = Math.pow(2, 16) + lead * Math.pow(2, 10) + trail;
5151
i++;
5252
}
5353
codepoints.push(code);
@@ -58,7 +58,7 @@ function preprocess(str) {
5858
function stringFromCode(code) {
5959
if(code <= 0xffff) return String.fromCharCode(code);
6060
// Otherwise, encode astral char as surrogate pair.
61-
code -= Math.pow(2, 20);
61+
code -= Math.pow(2, 16);
6262
var lead = Math.floor(code/Math.pow(2, 10)) + 0xd800;
6363
var trail = code % Math.pow(2, 10) + 0xdc00;
6464
return String.fromCharCode(lead) + String.fromCharCode(trail);

0 commit comments

Comments
 (0)