Skip to content

Commit 8f6262e

Browse files
fix(css): read an at-rule named with CSS escapes as that name
An at-keyword may be written with escapes — `@\6c ayer` is `@layer` — but every table the minifier looks a name up in was keyed by the raw spelling, so such a rule took no branch any of them names. Two of those lookups are refusals rather than optimizations, which is where this is a defect rather than a missed one: a sheet writing `@\6c ayer a;` twice around a rule had the first statement dropped, moving where the layer sits in the cascade, and the same held for `@\69 mport`. What kept most sheets from reaching that was the namespace prologue scan treating any escaped at-keyword as a possible `@namespace`, which disabled every top-level transform for the whole stylesheet. The fold is stated once, in `_atKeyword`, and reached through a new `path.atKeyword()` for a node and `_namesAtRule` for printed text. Layer gathering stays on the plain spelling deliberately: it slices the layer's name at a fixed `NAMED_LAYER_OPENER_HEAD` an escape makes wrong, so an escaped `@layer` is gathered by nothing — the safe answer — while the refusals that keep two layers apart do read the name.
1 parent 0af51c4 commit 8f6262e

10 files changed

Lines changed: 322 additions & 65 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Read an at-rule whose name is written with CSS escapes as the name it spells.

‎lib/css/syntax.js‎

Lines changed: 135 additions & 65 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases css minimize-escaped-at-rule-names minimize-escaped-at-rule-names should compile 1`] = `
4+
Array [
5+
"/*! the first statement of a layer is where it sits in the cascade, so a later
6+
copy of it never stands in for it */@\\\\6c ayer first;.t01{color:red}@\\\\6c ayer first;",
7+
"/*! a keyframe selector is read under the rule it is written in */@\\\\6b eyframes t02{0%{opacity:0}to{opacity:1}}",
8+
"/*! a media feature in the prelude is read as a range */@\\\\6d edia (width>=1px){.t03{color:red}}",
9+
"/*! a condition whose block is empty paints nothing */.t04{color:red}",
10+
"/*! ...but each anonymous layer is a layer of its own, so the pair stands */@\\\\6c ayer{.t05{color:red}}@\\\\6c ayer{.t05{color:blue}}",
11+
"/*! a name no table names takes no branch */@\\\\75 nknown-rule (color:red){.t06{color:red}}",
12+
]
13+
`;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases css minimize-escaped-at-rule-names minimize-escaped-at-rule-names should compile 1`] = `
4+
Array [
5+
"/*! the first statement of a layer is where it sits in the cascade, so a later
6+
copy of it never stands in for it */@\\\\6c ayer first;.t01{color:red}@\\\\6c ayer first;",
7+
"/*! a keyframe selector is read under the rule it is written in */@\\\\6b eyframes t02{0%{opacity:0}to{opacity:1}}",
8+
"/*! a media feature in the prelude is read as a range */@\\\\6d edia (width>=1px){.t03{color:red}}",
9+
"/*! a condition whose block is empty paints nothing */.t04{color:red}",
10+
"/*! ...but each anonymous layer is a layer of its own, so the pair stands */@\\\\6c ayer{.t05{color:red}}@\\\\6c ayer{.t05{color:blue}}",
11+
"/*! a name no table names takes no branch */@\\\\75 nknown-rule (color:red){.t06{color:red}}",
12+
]
13+
`;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "./style.css";
2+
3+
it("should read an at-rule whose name is written with escapes", () => {
4+
// The emitted stylesheet is snapshotted in test.config.js (afterExecute);
5+
// this keeps a runnable entry so the chunk and its `.css` are produced.
6+
expect(true).toBe(true);
7+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* An at-keyword may be written with CSS escapes, and `@\6c ayer` is then the
2+
same name as `@layer` — the tokenizer resolves the escape before anything
3+
reads the name. Each case below spells one that way and asks for the
4+
transform the plain spelling already gets. */
5+
6+
/*! the first statement of a layer is where it sits in the cascade, so a later
7+
copy of it never stands in for it */
8+
@\6c ayer first;
9+
.t01 { color: red }
10+
@\6c ayer first;
11+
12+
/*! a keyframe selector is read under the rule it is written in */
13+
@\6b eyframes t02 { from { opacity: 0 } to { opacity: 1 } }
14+
15+
/*! a media feature in the prelude is read as a range */
16+
@\6d edia (min-width: 1px) { .t03 { color: red } }
17+
18+
/*! a condition whose block is empty paints nothing */
19+
.t04 { color: red }
20+
@\73 upports (color: red) {}
21+
22+
/*! ...but each anonymous layer is a layer of its own, so the pair stands */
23+
@\6c ayer { .t05 { color: red } }
24+
@\6c ayer { .t05 { color: blue } }
25+
26+
/*! a name no table names takes no branch */
27+
@\75 nknown-rule (color: red) { .t06 { color: red } }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
const readMinifiedSections = require("../../../helpers/readMinifiedSections");
4+
5+
module.exports = {
6+
findBundle() {
7+
return ["bundle0.js"];
8+
},
9+
afterExecute(options) {
10+
expect(
11+
readMinifiedSections(options.output.path, "bundle0.css")
12+
).toMatchSnapshot();
13+
}
14+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
target: "web",
6+
mode: "production",
7+
output: {
8+
pathinfo: false
9+
},
10+
optimization: {
11+
minimize: true,
12+
// `"..."` keeps the default minimizer, which is what reads the names.
13+
minimizer: ["..."]
14+
},
15+
experiments: {
16+
css: true
17+
}
18+
};

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11269,3 +11269,89 @@ describe("CssSyntax minify — a fallback the target reads past", () => {
1126911269
);
1127011270
});
1127111271
});
11272+
11273+
describe("SourceProcessor — an at-rule named with CSS escapes", () => {
11274+
/**
11275+
* @param {string} sheet the stylesheet
11276+
* @param {object=} options what the minifying print is asked for
11277+
* @returns {string} the minified stylesheet
11278+
*/
11279+
const minify = (sheet, options) =>
11280+
new SourceProcessor().process(sheet, { mode: "minify", ...options }).code;
11281+
11282+
/**
11283+
* The same at-rule name with its first letter written as a hexadecimal CSS
11284+
* escape, which the syntax makes a legal spelling of that same name. Built
11285+
* rather than written out so no fragment of a name stands in the source.
11286+
* @param {string} name the at-rule name, the `@` excluded
11287+
* @returns {string} the escaped spelling, the `@` included
11288+
*/
11289+
const escaped = (name) =>
11290+
`@\\${name.charCodeAt(0).toString(16)} ${name.slice(1)}`;
11291+
11292+
it("keeps a layer statement a later copy of it restates", () => {
11293+
// The place a layer is first named is its place in the cascade, so the
11294+
// earlier statement is the one that says where it sits.
11295+
const layer = `${escaped("layer")} a;`;
11296+
expect(minify(`@supports (color:red){${layer}s{p:v}${layer}}`)).toBe(
11297+
`@supports (color:red){${layer}s{p:v}${escaped("layer")} a}`
11298+
);
11299+
});
11300+
11301+
it("keeps an import a later copy of it restates", () => {
11302+
const rule = `${escaped("import")} url(x);`;
11303+
expect(minify(`@media print{${rule}t{p:v}${rule}}`)).toBe(
11304+
`@media print{${rule}t{p:v}${escaped("import")} url(x)}`
11305+
);
11306+
});
11307+
11308+
it("reads a keyframe selector under the rule it is written in", () => {
11309+
expect(minify(`${escaped("keyframes")} x{from{opacity:0}}`)).toBe(
11310+
`${escaped("keyframes")} x{0%{opacity:0}}`
11311+
);
11312+
});
11313+
11314+
it("reads a media feature in the prelude as a range", () => {
11315+
expect(minify(`${escaped("media")} (min-width:1px){.a{color:red}}`)).toBe(
11316+
`${escaped("media")} (width>=1px){.a{color:red}}`
11317+
);
11318+
});
11319+
11320+
it("drops a condition whose block is empty", () => {
11321+
expect(minify(`.a{color:red}${escaped("supports")} (color:red){}`)).toBe(
11322+
".a{color:red}"
11323+
);
11324+
});
11325+
11326+
it("leaves two anonymous layers as the two layers they are", () => {
11327+
// Each `@layer {` opens a layer of its own, so joining the pair would hand
11328+
// the blocks back to specificity.
11329+
const sheet = `${escaped("layer")} {.a{color:red}}${escaped("layer")} {.a{color:blue}}`;
11330+
expect(minify(sheet)).toBe(
11331+
`${escaped("layer")}{.a{color:red}}${escaped("layer")}{.a{color:blue}}`
11332+
);
11333+
});
11334+
11335+
it("keeps an empty rule a namespace after it would move up into", () => {
11336+
const sheet = `@media print{}${escaped("namespace")} url(x);`;
11337+
expect(minify(sheet)).toBe(sheet);
11338+
});
11339+
11340+
it("lets a rule before one gather a later rule's selectors", () => {
11341+
// The name is read rather than the backslash, so a sheet writing one keeps
11342+
// every transform a sheet without one gets.
11343+
expect(
11344+
minify(
11345+
`.a{color:red}.b{margin:0}.c{color:red}${escaped("media")} print{.z{color:blue}}`,
11346+
{ mergeDistantRules: true }
11347+
)
11348+
).toBe(
11349+
`.a,.c{color:red}.b{margin:0}${escaped("media")} print{.z{color:blue}}`
11350+
);
11351+
});
11352+
11353+
it("reads a name no table names as the unknown rule it is", () => {
11354+
const sheet = `.a{color:red}${escaped("unknown-rule")} (color:red){}`;
11355+
expect(minify(sheet)).toBe(sheet);
11356+
});
11357+
});

‎types.d.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28358,6 +28358,7 @@ declare class SourceProcessorSyntaxClass_2 extends SourceProcessorClass<
2835828358
nameStart(n?: NodeSyntax): number;
2835928359
nameEnd(n?: NodeSyntax): number;
2836028360
unescapedName(n?: NodeSyntax): string;
28361+
atKeyword(n?: NodeSyntax): string;
2836128362
children(n?: NodeSyntax): ComponentValue[];
2836228363
prelude(n?: NodeSyntax): ComponentValue[];
2836328364
childCount(n?: NodeSyntax): number;
@@ -31500,6 +31501,7 @@ declare namespace exports {
3150031501
nameStart(n?: NodeSyntax): number;
3150131502
nameEnd(n?: NodeSyntax): number;
3150231503
unescapedName(n?: NodeSyntax): string;
31504+
atKeyword(n?: NodeSyntax): string;
3150331505
children(n?: NodeSyntax): ComponentValue[];
3150431506
prelude(n?: NodeSyntax): ComponentValue[];
3150531507
childCount(n?: NodeSyntax): number;
@@ -31713,6 +31715,7 @@ declare namespace exports {
3171331715
nameStart(n?: NodeSyntax): number;
3171431716
nameEnd(n?: NodeSyntax): number;
3171531717
unescapedName(n?: NodeSyntax): string;
31718+
atKeyword(n?: NodeSyntax): string;
3171631719
children(n?: NodeSyntax): ComponentValue[];
3171731720
prelude(n?: NodeSyntax): ComponentValue[];
3171831721
childCount(n?: NodeSyntax): number;
@@ -31763,6 +31766,7 @@ declare namespace exports {
3176331766
nameStart(n?: NodeSyntax): number;
3176431767
nameEnd(n?: NodeSyntax): number;
3176531768
unescapedName(n?: NodeSyntax): string;
31769+
atKeyword(n?: NodeSyntax): string;
3176631770
children(n?: NodeSyntax): ComponentValue[];
3176731771
prelude(n?: NodeSyntax): ComponentValue[];
3176831772
childCount(n?: NodeSyntax): number;

0 commit comments

Comments
 (0)