Skip to content

Commit a5433d9

Browse files
fix(css): match an at-keyword with CSS whitespace, not JavaScript's
`AT_RULE_NAME_SOURCE`, `ANONYMOUS_LAYER_RE` and the prelude trim next to them used `\s` and `String.trim`, both of which treat U+00A0 as whitespace. CSS does not: it is an identifier code point, so a name carrying one keeps it, and truncating there reads `@layer ` as the `@layer` it is not. No output moves. Both helpers only ever refuse, and the deciding path for the tables they feed is the node-based `path.atKeyword()`, which reads the name the tokenizer built. This makes the two agree rather than leaving one of them right by being shadowed.
1 parent ca2d800 commit a5433d9

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎lib/css/syntax.js‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4007,10 +4007,10 @@ const unescapeIdentifier = makeCacheable(_unescapeIdentifier);
40074007
*/
40084008
const _atKeyword = (raw) => asciiLowerCaseName(unescapeIdentifier(raw));
40094009

4010-
// An at-keyword as it may be written: an escape is part of the name, and a hex
4011-
// one ends at a whitespace the name does not carry — which is why matching one
4012-
// cannot be a single character class.
4013-
const AT_RULE_NAME_SOURCE = String.raw`(?:\\[\da-f]{1,6}[ \t\n\r\f]?|\\[^\n]|[^\s({;\\])+`;
4010+
// An at-keyword as it may be written: a hex escape ends at a whitespace the name
4011+
// does not carry, so this is no single class — and that whitespace is CSS's five
4012+
// rather than `\s`, which swallows U+00A0, an ident code point a name keeps.
4013+
const AT_RULE_NAME_SOURCE = String.raw`(?:\\[\da-f]{1,6}[ \t\n\r\f]?|\\[^\n]|[^ \t\n\r\f({;\\])+`;
40144014

40154015
/**
40164016
* Whether a text writes an at-rule one of `names` names, under any spelling of
@@ -4793,7 +4793,10 @@ const _enclosingRuleScope = (scope, head) => {
47934793
// CSS Cascade 5 §6.4.1: an `!important` declaration is read from the earliest
47944794
// layer, so a copy in a later `@layer {` makes nothing dead.
47954795
let _anonymousLayers = 0;
4796-
const BARE_AT_RULE_RE = new RegExp(`^@(${AT_RULE_NAME_SOURCE})\\s*\\{$`, "i");
4796+
const BARE_AT_RULE_RE = new RegExp(
4797+
`^@(${AT_RULE_NAME_SOURCE})[ \\t\\n\\r\\f]*\\{$`,
4798+
"i"
4799+
);
47974800

47984801
/**
47994802
* What an opener contributes to the key the rules under it are read by. The
@@ -15381,7 +15384,7 @@ const _rangeEquals = (a, aStart, aEnd, b, bStart, bEnd) => {
1538115384
};
1538215385

1538315386
// A layer with no name: the one at-rule prelude that is not the same rule twice.
15384-
const ANONYMOUS_LAYER_RE = /^@layer\s*$/i;
15387+
const ANONYMOUS_LAYER_RE = /^@layer[ \t\n\r\f]*$/i;
1538515388

1538615389
/**
1538715390
* Whether a prelude opens a layer of its own rather than naming one, which no
@@ -15397,7 +15400,7 @@ const _isAnonymousLayerPrelude = (prelude) => {
1539715400
return (
1539815401
match !== null &&
1539915402
_atKeyword(match[1]) === "layer" &&
15400-
prelude.slice(match[0].length).trim().length === 0
15403+
_trimCssWhiteSpace(prelude.slice(match[0].length)).length === 0
1540115404
);
1540215405
};
1540315406

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11464,6 +11464,16 @@ describe("SourceProcessor — an at-rule named with CSS escapes", () => {
1146411464
);
1146511465
});
1146611466

11467+
it("keeps a non-ASCII code point inside the name it is written in", () => {
11468+
// U+00A0 is an identifier code point, not CSS whitespace, so `@layer` and
11469+
// `@layer\u00a0x` are two names rather than one rule and its prelude.
11470+
const sheet = `@layer\u00a0x{.p{color:red}}@layer\u00a0x{.q{color:blue}}`;
11471+
expect(minify(sheet)).toBe(sheet);
11472+
expect(minify("@layer x{.p{color:red}}@layer x{.q{color:blue}}")).toBe(
11473+
"@layer x{.p{color:red}.q{color:blue}}"
11474+
);
11475+
});
11476+
1146711477
it("reads a name no table names as the unknown rule it is", () => {
1146811478
const sheet = `.a{color:red}${escaped("unknown-rule")} (color:red){}`;
1146911479
expect(minify(sheet)).toBe(sheet);

0 commit comments

Comments
 (0)