Skip to content

Commit

Permalink
Expand unesc handling to correctly handle spec edgease for lone
Browse files Browse the repository at this point in the history
surrogates and out of bound codepoint values.
  • Loading branch information
samccone committed Apr 11, 2021
1 parent 3d03b95 commit ea4f37a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/__tests__/util/unesc.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ test('class selector with escaping', '.\\1D306', (t, tree) => {
test('class selector with escaping with more chars', '.\\1D306k', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, '𝌆k');
});

test('handles 0 value hex', '\\0', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, String.fromCodePoint(0xFFFD));
});

test('handles lone surrogate value hex', '\\DBFF', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, String.fromCodePoint(0xFFFD));
});

test('handles out of bound values', '\\110000', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, String.fromCodePoint(0xFFFD));
});
11 changes: 10 additions & 1 deletion src/util/unesc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ function gobbleHex (str) {
if (hex.length === 0) {
return undefined;
}
const codePoint = parseInt(hex, 16);

const isSurrogate = codePoint >= 0xD800 && codePoint <= 0xDFFF;
// Add special case for
// "If this number is zero, or is for a surrogate, or is greater than the maximum allowed code point"
// https://drafts.csswg.org/css-syntax/#maximum-allowed-code-point
if (isSurrogate || codePoint === 0x0000 || codePoint > 0x10FFFF) {
return ['\uFFFD', hex.length + (spaceTerminated ? 1 : 0)];
}

return [
String.fromCodePoint(parseInt(hex, 16)),
String.fromCodePoint(codePoint),
hex.length + (spaceTerminated ? 1 : 0),
];
}
Expand Down

0 comments on commit ea4f37a

Please sign in to comment.