Skip to content

Commit a878df1

Browse files
committed
Merge pull request #33 from tromey/fix-surrogate-pairs
fix some buglets in surrogate pair decoding
2 parents 433a225 + 0175f03 commit a878df1

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

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)