Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/__tests__/escapes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,29 @@ test("bare parens capture contents as a string", "(h1)", (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, "(h1)");
t.deepEqual(tree.nodes[0].nodes[0].type, "string");
});

for (const hex of ["61", "000061"]) {
for (const whitespace of [" ", "\t", "\n", "\r", "\f", "\r\n"]) {
test(
`hex escape ${hex} terminated by ${JSON.stringify(whitespace)}`,
`[x="\\${hex}${whitespace}b"]`,
(t, tree) => {
t.is(tree.first.first.value, "ab");
},
);
}
}

for (const [value, expected] of [
["\\000061 b", "a b"],
["\\000000 b", "\uFFFDb"],
["\\00D800 b", "\uFFFDb"],
["\\110000 b", "\uFFFDb"],
["\\000061b", "ab"],
["\\000061\u00a0b", "a\u00a0b"],
["\\000061\vb", "a\vb"],
]) {
test(`hex escape boundary ${JSON.stringify(value)}`, `[x="${value}"]`, (t, tree) => {
t.is(tree.first.first.value, expected);
});
}
14 changes: 9 additions & 5 deletions src/util/unesc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
function gobbleHex(str) {
const lower = str.toLowerCase();
let hex = "";
let spaceTerminated = false;
for (let i = 0; i < 6 && lower[i] !== undefined; i++) {
const code = lower.charCodeAt(i);
// check to see if we are dealing with a valid hex char [a-f|0-9]
const valid = (code >= 97 && code <= 102) || (code >= 48 && code <= 57);
// https://drafts.csswg.org/css-syntax/#consume-escaped-code-point
spaceTerminated = code === 32;
if (!valid) {
break;
}
Expand All @@ -25,17 +23,23 @@ function gobbleHex(str) {
if (hex.length === 0) {
return undefined;
}
let consumed = hex.length;
const next = str[consumed];
if (next === " " || next === "\t" || next === "\n" || next === "\r" || next === "\f") {
// CSS preprocessing treats CRLF as a single newline.
consumed += next === "\r" && str[consumed + 1] === "\n" ? 2 : 1;
}
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 ["\uFFFD", consumed];
}

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

const CONTAINS_ESCAPE = /\\/;
Expand All @@ -49,7 +53,7 @@ export default function unesc(str) {

for (let i = 0; i < str.length; i++) {
if (str[i] === "\\") {
const gobbled = gobbleHex(str.slice(i + 1, i + 7));
const gobbled = gobbleHex(str.slice(i + 1, i + 9));
if (gobbled !== undefined) {
ret += gobbled[0];
i += gobbled[1];
Expand Down