Skip to content

Commit

Permalink
Preprocess a standalone surrogate to \uFFFD
Browse files Browse the repository at this point in the history
- Also optimize surrogate pair handling using native String.codePointAt().
- ref: https://www.w3.org/TR/css-syntax-3/#input-preprocessing
  • Loading branch information
danny0838 committed Mar 31, 2024
1 parent 28c723b commit c6a916e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
17 changes: 7 additions & 10 deletions parse-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,14 @@ function preprocess(str) {
// following the preprocessing cleanup rules.
var codepoints = [];
for(var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i);
if(code == 0xd && str.charCodeAt(i+1) == 0xa) {
var code = str.codePointAt(i);
if (code == 0xd && str.charCodeAt(i+1) == 0xa) {
code = 0xa; i++;
}
if(code == 0xd || code == 0xc) code = 0xa;
if(code == 0x0) code = 0xfffd;
if(between(code, 0xd800, 0xdbff) && between(str.charCodeAt(i+1), 0xdc00, 0xdfff)) {
// Decode a surrogate pair into an astral codepoint.
var lead = code - 0xd800;
var trail = str.charCodeAt(i+1) - 0xdc00;
code = Math.pow(2, 16) + lead * Math.pow(2, 10) + trail;
} else if (code == 0xd || code == 0xc) {
code = 0xa;
} else if (code == 0x0 || between(code, 0xd800, 0xdfff)) {
code = 0xfffd;
} else if (code > 0xffff) {
i++;
}
codepoints.push(code);
Expand Down
30 changes: 30 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,36 @@ var TESTS = [
}
]
}
}, {
parser: '',
css: `\u{20000},\u{0},\uD800,\uDFFF`,
expected: [
{
"type": "IDENT",
"value": "\u{20000}"
},
{
"type": "COMMA",
},
{
"type": "IDENT",
"value": "\uFFFD"
},
{
"type": "COMMA",
},
{
"type": "IDENT",
"value": "\uFFFD"
},
{
"type": "COMMA",
},
{
"type": "IDENT",
"value": "\uFFFD"
},
]
}
];

Expand Down

0 comments on commit c6a916e

Please sign in to comment.