Skip to content

Commit 0175f03

Browse files
committed
fix some buglets in surrogate pair decoding
First, the correct constant to use is 0x10000, aka Math.pow(2,20). Second, arrange to skip the low surrogate. Finally, fix the math in stringFromCode as well, and remove an extraneous semicolon.
1 parent 433a225 commit 0175f03

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

parse-css.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ 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, 21) + lead * Math.pow(2, 10) + trail;
50+
code = Math.pow(2, 20) + lead * Math.pow(2, 10) + trail;
51+
i++;
5152
}
5253
codepoints.push(code);
5354
}
@@ -57,9 +58,9 @@ function preprocess(str) {
5758
function stringFromCode(code) {
5859
if(code <= 0xffff) return String.fromCharCode(code);
5960
// Otherwise, encode astral char as surrogate pair.
60-
code -= Math.pow(2, 21);
61+
code -= Math.pow(2, 20);
6162
var lead = Math.floor(code/Math.pow(2, 10)) + 0xd800;
62-
var trail = code % Math.pow(2, 10); + 0xdc00;
63+
var trail = code % Math.pow(2, 10) + 0xdc00;
6364
return String.fromCharCode(lead) + String.fromCharCode(trail);
6465
}
6566

0 commit comments

Comments
 (0)