Skip to content

Commit 517524f

Browse files
fix(css): claim a style query's comments, and widen what names @namespace
A container style query is written back from its source, comments included, so the copies the writer had queued have to be claimed there — flushed after the rule instead, the comment was emitted a second time. An at-keyword carrying an escape can still name `@namespace`, and only decoding tells the two apart, so the prologue scan now treats any escaped at-keyword as one. Over-wide on purpose: a false positive keeps an empty rule, which is the side that cannot change what the sheet means. The equivalence harness maps a surrogate escape to U+FFFD as §4.3.7 says, and normalizes a zero in every length unit CSS Values 4 states rather than the nine it named — the printer folds `0vw` too.
1 parent 0405a14 commit 517524f

4 files changed

Lines changed: 85 additions & 8 deletions

File tree

‎.changeset/040-css-html-safe-minify.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Only make CSS and HTML transformations an engine cannot tell apart: keep as written every value, selector and attribute a rewrite would change the meaning of, read a lone `{}` block as a declaration's whole value, and build the tree an engine builds for a declarative shadow root and for a formatting end tag the active list no longer names.
5+
Only make CSS and HTML transformations an engine cannot tell apart.

‎lib/css/syntax.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10481,7 +10481,12 @@ const printer = (path, writer) => {
1048110481
const written = path.source();
1048210482
// Unclosed at EOF, the source carries no `)` for the walk to have
1048310483
// closed — printed as usual so the parenthesis is written back.
10484-
if (written.endsWith(")")) return written;
10484+
if (written.endsWith(")")) {
10485+
// Written whole, comments included, so the queued copies are claimed
10486+
// here rather than flushed a second time after the rule.
10487+
writer.takeInserts(path.start(), path.end());
10488+
return written;
10489+
}
1048510490
}
1048610491
let inner;
1048710492
if (path.childCount() === 1) {
@@ -11506,7 +11511,10 @@ let _placeShorthandAllowed = true;
1150611511

1150711512
// Set when the source names one at all, so a stylesheet without any pays a
1150811513
// single scan and keeps nothing.
11509-
const NAMESPACE_AT_RULE_RE = /@namespace/i;
11514+
// An at-keyword that could name `@namespace`: the spelling itself, or any
11515+
// at-keyword carrying an escape, which only decoding would tell apart. Over-wide
11516+
// on purpose — a false positive keeps an empty rule, which is the safe side.
11517+
const NAMESPACE_AT_RULE_RE = /@namespace|@[\w-]*\\/i;
1151011518

1151111519
// Whether a top-level `@namespace` could still be read here. Only a rule the
1151211520
// engine keeps closes the run one may stand in (CSS Namespaces 3 §3.1) — an

‎test/CssSyntax.unittest.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,6 +2791,18 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
27912791
// 3px against 1.5px at `zoom:2`).
27922792
["a{outline-width:initial}", "a{outline-width:initial}"],
27932793
["a{text-align:initial}", "a{text-align:start}"],
2794+
// The source carries its own comments, so the queued copy is claimed
2795+
// rather than flushed again after the rule.
2796+
[
2797+
"@container style(--a: /*! keep */ b){.a{color:red}}",
2798+
"@container style(--a: /*! keep */ b){.a{color:red}}"
2799+
],
2800+
// Only decoding tells this at-keyword from `@namespace`, so the empty
2801+
// rule the prologue needs is kept either way.
2802+
[
2803+
".e{}@name\\73pace url(x);.a{color:red}",
2804+
".e{}@name\\73pace url(x);.a{color:red}"
2805+
],
27942806
["a{font-size:initial}", "a{font-size:initial}"]
27952807
])("%s", (css, expected) => {
27962808
expect(minify(css)).toBe(expected);

‎test/helpers/syntaxEquivalence.js‎

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,71 @@ const installHelpers = () => {
152152
let end = at + 1 + hex[0].length;
153153
if (/[\t\n\f\r ]/.test(text[end])) end++;
154154
const code = Number.parseInt(hex[0], 16);
155+
// §4.3.7: zero, a surrogate and anything past the maximum all name U+FFFD.
155156
const named =
156-
code === 0 || code > 0x10ffff ? "\uFFFD" : String.fromCodePoint(code);
157+
code === 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)
158+
? "\uFFFD"
159+
: String.fromCodePoint(code);
157160
return [named, end];
158161
};
159162

163+
// cspell:ignore rlh cqmin cqmax vmin vmax dvmin dvmax lvmin lvmax svmin svmax
164+
// Every length unit CSS Values 4 states, longest first so `vmin` is not read
165+
// as `vm` — a zero is the same zero in any of them.
166+
const LENGTH_UNITS = [
167+
"cqmin",
168+
"cqmax",
169+
"svmin",
170+
"svmax",
171+
"lvmin",
172+
"lvmax",
173+
"dvmin",
174+
"dvmax",
175+
"vmin",
176+
"vmax",
177+
"rlh",
178+
"rem",
179+
"rex",
180+
"rch",
181+
"ric",
182+
"svw",
183+
"svh",
184+
"svi",
185+
"svb",
186+
"lvw",
187+
"lvh",
188+
"lvi",
189+
"lvb",
190+
"dvw",
191+
"dvh",
192+
"dvi",
193+
"dvb",
194+
"cqw",
195+
"cqh",
196+
"cqi",
197+
"cqb",
198+
"px",
199+
"cm",
200+
"mm",
201+
"in",
202+
"pt",
203+
"pc",
204+
"em",
205+
"ex",
206+
"ch",
207+
"ic",
208+
"lh",
209+
"vw",
210+
"vh",
211+
"vi",
212+
"vb",
213+
"q"
214+
];
215+
const ZERO_LENGTH_RE = new RegExp(
216+
`(^|[^\\w.#%-])0(?:\\.0*)?(?:${LENGTH_UNITS.join("|")})\\b`,
217+
"gi"
218+
);
219+
160220
// A character an escape can be dropped from without the value reading
161221
// differently — everything a name is spelled out of.
162222
const BARE_ESCAPED = /[\w\u00A0-\uFFFF-]/;
@@ -270,10 +330,7 @@ const installHelpers = () => {
270330
.replace(/(^|[^\w.%-])0*(\.\d)/g, "$10$2")
271331
// A zero length is the same zero however it is spelled, and a value
272332
// held as written is the one place the printer's `0px` → `0` shows.
273-
.replace(
274-
/(^|[^\w.#%-])0(?:\.0*)?(?:px|em|rem|q|in|pt|pc|cm|mm)\b/gi,
275-
"$10"
276-
)
333+
.replace(ZERO_LENGTH_RE, "$10")
277334
.trim()
278335
);
279336
};

0 commit comments

Comments
 (0)