fix: parsing comments and escaped

This commit is contained in:
alexander.akait 2023-04-14 03:31:58 +03:00
parent 308e405806
commit 65b521659b
1 changed files with 30 additions and 19 deletions

View File

@ -36,7 +36,7 @@ const CC_FORM_FEED = "\f".charCodeAt(0);
const CC_TAB = "\t".charCodeAt(0); const CC_TAB = "\t".charCodeAt(0);
const CC_SPACE = " ".charCodeAt(0); const CC_SPACE = " ".charCodeAt(0);
const CC_SLASH = "/".charCodeAt(0); const CC_SOLIDUS = "/".charCodeAt(0);
const CC_REVERSE_SOLIDUS = "\\".charCodeAt(0); const CC_REVERSE_SOLIDUS = "\\".charCodeAt(0);
const CC_ASTERISK = "*".charCodeAt(0); const CC_ASTERISK = "*".charCodeAt(0);
@ -117,22 +117,33 @@ const consumeDelimToken = (input, pos, callbacks) => {
}; };
/** @type {CharHandler} */ /** @type {CharHandler} */
const consumePotentialComment = (input, pos, callbacks) => { const consumeComments = (input, pos, callbacks) => {
pos++; // If the next two input code point are U+002F SOLIDUS (/) followed by a U+002A
if (pos === input.length) return pos; // ASTERISK (*), consume them and all following code points up to and including
let cc = input.charCodeAt(pos); // the first U+002A ASTERISK (*) followed by a U+002F SOLIDUS (/), or up to an
if (cc !== CC_ASTERISK) return pos; // EOF code point. Return to the start of this step.
for (;;) { //
pos++; // If the preceding paragraph ended by consuming an EOF code point, this is a parse error.
// But we are silent on errors.
if (
// Already checked in `CHAR_MAP`
// input.charCodeAt(pos) === CC_SOLIDUS &&
input.charCodeAt(pos + 1) === CC_ASTERISK
) {
pos += 2;
if (pos === input.length) return pos; if (pos === input.length) return pos;
cc = input.charCodeAt(pos); while (pos < input.length) {
while (cc === CC_ASTERISK) { if (
input.charCodeAt(pos) === CC_ASTERISK &&
input.charCodeAt(pos + 1) === CC_SOLIDUS
) {
pos += 2;
break;
}
pos++; pos++;
if (pos === input.length) return pos;
cc = input.charCodeAt(pos);
if (cc === CC_SLASH) return pos + 1;
} }
} }
return pos;
}; };
/** @type {function(number): CharHandler} */ /** @type {function(number): CharHandler} */
@ -552,8 +563,8 @@ const CHAR_MAP = Array.from({ length: 0x80 }, (_, cc) => {
// https://drafts.csswg.org/css-syntax/#consume-token // https://drafts.csswg.org/css-syntax/#consume-token
switch (cc) { switch (cc) {
// Consume comments. // Consume comments.
case CC_SLASH: case CC_SOLIDUS:
return consumePotentialComment; return consumeComments;
// whitespace // whitespace
case CC_LINE_FEED: case CC_LINE_FEED:
case CC_CARRIAGE_RETURN: case CC_CARRIAGE_RETURN:
@ -652,7 +663,7 @@ module.exports = (input, callbacks) => {
module.exports.eatComments = (input, pos) => { module.exports.eatComments = (input, pos) => {
loop: for (;;) { loop: for (;;) {
const cc = input.charCodeAt(pos); const cc = input.charCodeAt(pos);
if (cc === CC_SLASH) { if (cc === CC_SOLIDUS) {
if (pos === input.length) return pos; if (pos === input.length) return pos;
let cc = input.charCodeAt(pos + 1); let cc = input.charCodeAt(pos + 1);
if (cc !== CC_ASTERISK) return pos; if (cc !== CC_ASTERISK) return pos;
@ -665,7 +676,7 @@ module.exports.eatComments = (input, pos) => {
pos++; pos++;
if (pos === input.length) return pos; if (pos === input.length) return pos;
cc = input.charCodeAt(pos); cc = input.charCodeAt(pos);
if (cc === CC_SLASH) { if (cc === CC_SOLIDUS) {
pos++; pos++;
continue loop; continue loop;
} }
@ -679,7 +690,7 @@ module.exports.eatComments = (input, pos) => {
module.exports.eatWhitespaceAndComments = (input, pos) => { module.exports.eatWhitespaceAndComments = (input, pos) => {
loop: for (;;) { loop: for (;;) {
const cc = input.charCodeAt(pos); const cc = input.charCodeAt(pos);
if (cc === CC_SLASH) { if (cc === CC_SOLIDUS) {
if (pos === input.length) return pos; if (pos === input.length) return pos;
let cc = input.charCodeAt(pos + 1); let cc = input.charCodeAt(pos + 1);
if (cc !== CC_ASTERISK) return pos; if (cc !== CC_ASTERISK) return pos;
@ -692,7 +703,7 @@ module.exports.eatWhitespaceAndComments = (input, pos) => {
pos++; pos++;
if (pos === input.length) return pos; if (pos === input.length) return pos;
cc = input.charCodeAt(pos); cc = input.charCodeAt(pos);
if (cc === CC_SLASH) { if (cc === CC_SOLIDUS) {
pos++; pos++;
continue loop; continue loop;
} }