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