Skip to content

Commit c63ac95

Browse files
fix(css): keep the token boundary a dropped comment stood for (#21635)
* fix(css): part two value components a comment stands between A comment is no tree node, so `_valueComponents` saw two children with nothing between them and made one component of them. Any transform that rewrote the value then printed `1px 1px/*c*/1px 1px` back as `margin:1px 1px1px`, which the engine drops. The gap the comment leaves now parts them. * fix(css): keep the token boundary a dropped comment stood for A custom property's value printed its children's sources joined, which drops a comment between two of them and fuses the tokens it parts; it now prints the source slice, comment included. In a selector the separator a fusing junction needs is an empty comment, not whitespace — `a/*c*/b` is two adjacent type selectors, `a b` a descendant combinator. * fix(css): emit a custom property's kept comment once The value's source slice carries a kept comment along, and the writer also had it queued to re-emit before the next top-level node — so `--x:1px/*!c*/2px` printed it twice. The slice drops the queued copy, which also leaves the comment where it was written. * chore: regenerate types.d.ts for dropInserts * fix(css): minify a custom property's comment to the boundary it stands for A dropped comment in a custom property's value now prints as the token boundary it stood for — a space only where the tokens would otherwise fuse, nothing where they already part — instead of riding along in the source slice. Kept comments stay in place. takeInserts replaces dropInserts (one call, and it hands the printer the text) and binary-searches in, so a rule's declarations no longer rescan every insert queued before them. * fix(css): minify a comment nested in a custom property's value too A function or block in a custom property's value is no leaf, so the comments in one are the value's too. The walk now recurses into it, writing its delimiters back and printing each gap as the boundary it stood for. A source holding no `/*` is written back whole, so a value without comments costs what it did before. * test(css): cover a bare parenthesized block in a custom property --------- Co-authored-by: alexander-akait <[email protected]>
1 parent 267ef49 commit c63ac95

11 files changed

Lines changed: 243 additions & 8 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+
Minify a dropped CSS comment to the token boundary it stood for.

‎lib/css/syntax.js‎

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4343,8 +4343,10 @@ const _join = (parts, beautify, trim = _TRIM_NOTHING) => {
43434343
pending = false;
43444344
} else if (out.length !== 0 && _wouldFuseTokens(out, f)) {
43454345
// Nothing separated these in source (a comment stood here, or a token was
4346-
// rewritten), yet joining them would read back as one token.
4347-
out += " ";
4346+
// rewritten), yet joining them would read back as one token. Whitespace
4347+
// is a descendant combinator in a selector, so there only an empty
4348+
// comment parts them without saying anything the source did not.
4349+
out += trim === _TRIM_COMBINATORS ? "/**/" : " ";
43484350
}
43494351
out += f;
43504352
last = f.charCodeAt(f.length - 1);
@@ -6055,9 +6057,10 @@ const _SUBSTITUTION_RE = new RegExp(
60556057
/**
60566058
* Split a declaration value into its top-level components. Whitespace parts
60576059
* them, and so does a `/` delim — it separates `border-radius`'s two boxes and
6058-
* needs no whitespace of its own. Two children nothing parted stay one
6059-
* component (a comment stood there), which is conservative: it only costs a
6060-
* collapse.
6060+
* needs no whitespace of its own. A comment is no tree node, so the gap it
6061+
* leaves between two children parts them too: it ends both tokens, and a
6062+
* rewritten value joins its components with a space that has to stand where
6063+
* the comment did.
60616064
* @param {CssPath} path the accessor positioned on the declaration
60626065
* @param {ComponentValue[]} children the value's children
60636066
* @param {PrintContext} writer the print context (children's printed text)
@@ -6067,9 +6070,16 @@ const _valueComponents = (path, children, writer) => {
60676070
/** @type {string[]} */
60686071
const components = [];
60696072
let current = "";
6073+
let previousEnd = -1;
60706074
for (const child of children) {
60716075
const type = path.type(child);
60726076
const text = writer.get(child);
6077+
const start = path.start(child);
6078+
if (current.length !== 0 && start !== previousEnd) {
6079+
components.push(current);
6080+
current = "";
6081+
}
6082+
previousEnd = path.end(child);
60736083
if (type === T_WHITESPACE || (type === T_DELIM && text === "/")) {
60746084
if (current.length !== 0) components.push(current);
60756085
current = "";
@@ -6082,6 +6092,89 @@ const _valueComponents = (path, children, writer) => {
60826092
return components;
60836093
};
60846094

6095+
/**
6096+
* What a comment-only gap in a custom property's value prints as: the kept
6097+
* comments in it where they stood, otherwise the boundary it stands for — a
6098+
* space where the tokens either side would fuse, and nothing where they part.
6099+
* @param {PrintContext} writer the print context (holds the kept comments)
6100+
* @param {number} start first source offset of the gap
6101+
* @param {number} end offset past its last
6102+
* @param {string} out the value printed so far
6103+
* @param {string} next the token about to follow it (empty at the end)
6104+
* @returns {string} the gap's text
6105+
*/
6106+
const _customPropertyGap = (writer, start, end, out, next) => {
6107+
const kept = writer.takeInserts(start, end);
6108+
if (kept !== "") return kept;
6109+
return next !== "" && _wouldFuseTokens(out, next) ? " " : "";
6110+
};
6111+
6112+
/**
6113+
* A custom property's value as written — the tokens are the value, and the
6114+
* CSSOM hands this text back, so nothing in it is rewritten. A comment is the
6115+
* one exception: it is no tree node, so it survives as a gap between two of
6116+
* them, which {@link _customPropertyGap} prints as the boundary it stood for.
6117+
* @param {CssPath} path the accessor positioned on the declaration
6118+
* @param {ComponentValue[]} children the tokens to print
6119+
* @param {PrintContext} writer the print context (holds the kept comments)
6120+
* @param {number} from source offset the tokens start at
6121+
* @param {number} to source offset they end at
6122+
* @returns {string} the printed value
6123+
*/
6124+
const _customPropertyValue = (path, children, writer, from, to) => {
6125+
let out = "";
6126+
let at = from;
6127+
for (const child of children) {
6128+
const text = _customPropertyToken(path, child, writer);
6129+
const start = path.start(child);
6130+
// Every other token is a child of its own, so a gap is comments only.
6131+
if (at !== start) out += _customPropertyGap(writer, at, start, out, text);
6132+
out += text;
6133+
at = path.end(child);
6134+
}
6135+
if (at !== to) out += _customPropertyGap(writer, at, to, out, "");
6136+
return out;
6137+
};
6138+
6139+
/**
6140+
* One token of a custom property's value, as written — recursing into a
6141+
* function or block so a comment nested in one minifies too. A source holding
6142+
* no `/*` holds no comment at any depth, so it is written back whole.
6143+
* @param {CssPath} path the accessor positioned on the declaration
6144+
* @param {ComponentValue} child the token to print
6145+
* @param {PrintContext} writer the print context (holds the kept comments)
6146+
* @returns {string} the printed token
6147+
*/
6148+
const _customPropertyToken = (path, child, writer) => {
6149+
const source = path.source(child);
6150+
const type = path.type(child);
6151+
if (
6152+
!source.includes("/*") ||
6153+
(type !== T_FUNCTION && type !== T_SIMPLE_BLOCK)
6154+
) {
6155+
return source;
6156+
}
6157+
const start = path.start(child);
6158+
const end = path.end(child);
6159+
let opened = path.nameEnd(child) + 1;
6160+
let closer = ")";
6161+
if (type === T_SIMPLE_BLOCK) {
6162+
const block = path.blockToken(child);
6163+
opened = start + 1;
6164+
closer = block === "[" ? "]" : block === "{" ? "}" : ")";
6165+
}
6166+
// Closed at EOF: there is no closer to write back, and it ends the value.
6167+
const closed = _input[end - 1] === closer;
6168+
const inner = _customPropertyValue(
6169+
path,
6170+
path.children(child),
6171+
writer,
6172+
opened,
6173+
closed ? end - 1 : end
6174+
);
6175+
return `${_input.slice(start, opened)}${inner}${closed ? closer : ""}`;
6176+
};
6177+
60856178
/**
60866179
* Split top-level components into the layers a comma parts. A comma is no
60876180
* separator of its own, so it rides on the component it follows and a layer's
@@ -7371,8 +7464,13 @@ const printer = (path, writer) => {
73717464
// A custom property's value is the text `getPropertyValue()` hands
73727465
// back, so rewriting it builds a different CSSOM from the same
73737466
// document — which `SyntaxBrowserEquivalence` forbids, whatever a
7374-
// substitution would read. Verbatim, then.
7375-
value = children.map((c) => path.source(c)).join("");
7467+
// substitution would read. As written, then; only the comments in it
7468+
// minify, down to the boundary they stand for.
7469+
const from = path.start(children[0]);
7470+
const to = path.end(children[children.length - 1]);
7471+
value = minify
7472+
? _customPropertyValue(path, children, writer, from, to)
7473+
: _input.slice(from, to);
73767474
} else if (property === "unicode-range") {
73777475
// `U+…` tokenizes as numbers, so the generic numeric normalization
73787476
// would corrupt it; each range is shortened as the urange it is.

‎lib/util/SourceProcessor.js‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,34 @@ class PrintContext {
229229
this._inserts.push([pos, text]);
230230
}
231231

232+
/**
233+
* Take the queued inserts sitting in `[start, end)` — a printer that writes
234+
* that source range out itself places them, so the writer must not flush
235+
* them ahead of the next top-level node as well.
236+
* @param {number} start first source offset of the range
237+
* @param {number} end offset past its last
238+
* @returns {string} their text, in source order
239+
*/
240+
takeInserts(start, end) {
241+
const inserts = this._inserts;
242+
// `_insertIdx` only moves on a flush, so walking from it would scan every
243+
// insert the enclosing rule queued before this range again — quadratic over
244+
// a rule's declarations. They arrive in source order, so binary search in.
245+
let low = this._insertIdx;
246+
let high = inserts.length;
247+
while (low < high) {
248+
const middle = (low + high) >> 1;
249+
if (inserts[middle][0] < start) low = middle + 1;
250+
else high = middle;
251+
}
252+
let out = "";
253+
for (let i = low; i < inserts.length && inserts[i][0] < end; i++) {
254+
out += inserts[i][1];
255+
inserts[i][1] = "";
256+
}
257+
return out;
258+
}
259+
232260
/**
233261
* Emit every queued insert positioned before source offset `pos` (all of them
234262
* for `Infinity`), in source order, so kept comments keep their place relative
@@ -239,7 +267,7 @@ class PrintContext {
239267
const inserts = this._inserts;
240268
let i = this._insertIdx;
241269
while (i < inserts.length && inserts[i][0] < pos) {
242-
this._emit(inserts[i][1]);
270+
if (inserts[i][1] !== "") this._emit(inserts[i][1]);
243271
i++;
244272
}
245273
this._insertIdx = i;

‎test/CssSyntax.unittest.js‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,62 @@ describe("CssSyntax — minify token-boundary safety", () => {
837837
);
838838
});
839839

840+
it("parts a selector's tokens with a comment rather than whitespace", () => {
841+
// Whitespace between two of a selector's tokens is a descendant combinator,
842+
// so the separator a fusing junction needs there is an empty comment: `a b`
843+
// would match what `a/**/b` (two adjacent type selectors) never does.
844+
expect(min("a/**/b{c:1}")).toBe("a/**/b{c:1}");
845+
expect(min("@scope (div/**/span){a{c:1}}")).toBe(
846+
"@scope (div/**/span){a{c:1}}"
847+
);
848+
// A comment the source spelled out where nothing would fuse still goes.
849+
expect(min("a/**/ b{c:1}")).toBe("a b{c:1}");
850+
});
851+
852+
it("keeps a custom property's value as the source wrote it", () => {
853+
// The value is the text `getPropertyValue()` hands back, so it is not
854+
// rewritten — but a dropped comment leaves the boundary it stood for, which
855+
// is a space only where the tokens it parts would otherwise fuse.
856+
expect(min("a{--x:1px/*c*/2px}")).toBe("a{--x:1px 2px}");
857+
expect(min("a{--x:1px 1px/*c*/1px 1px}")).toBe("a{--x:1px 1px 1px 1px}");
858+
expect(min("a{--x:1px /*c*/ 2px}")).toBe("a{--x:1px 2px}");
859+
// Leading and trailing whitespace is not part of it.
860+
expect(min("a{--x: 1px 2px }")).toBe("a{--x:1px 2px}");
861+
// A `/*` inside a string is no comment.
862+
expect(min('a{--x:"a/*c*/b"}')).toBe('a{--x:"a/*c*/b"}');
863+
// A kept comment stays where it stood, so it is not also re-emitted before
864+
// the next top-level node — and it parts the tokens itself.
865+
expect(min("a{--x:1px/*!c*/2px}b{c:1}")).toBe("a{--x:1px/*!c*/2px}b{c:1}");
866+
expect(min("a{--x:1px/*c*//*!k*/2px}")).toBe("a{--x:1px/*!k*/2px}");
867+
// One rule's custom properties take their own, in the order they stand in.
868+
expect(
869+
min("/*!t*/a{--x:1px/*!k*/2px;--y:3px/*c*/4px;--z:5px/*!j*/6px}")
870+
).toBe("/*!t*/a{--x:1px/*!k*/2px;--y:3px 4px;--z:5px/*!j*/6px}");
871+
// Outside one it still moves ahead of the rule that follows it.
872+
expect(min("a{c:red/*!c*/}b{c:1}")).toBe("a{c:red}/*!c*/b{c:1}");
873+
});
874+
875+
it("minifies a comment nested in a custom property's value", () => {
876+
// A function or block is no leaf, so the comments in one are the value's
877+
// too — at any depth, and in a block of every shape.
878+
expect(min("a{--x:foo(a/*c*/b)}")).toBe("a{--x:foo(a b)}");
879+
expect(min("a{--x:foo(bar(a/*c*/b))}")).toBe("a{--x:foo(bar(a b))}");
880+
expect(min("a{--x:[a/*c*/b]}")).toBe("a{--x:[a b]}");
881+
expect(min("a{--x:{a:1/*c*/2}}")).toBe("a{--x:{a:1 2}}");
882+
expect(min("a{--x:(a/*c*/b)}")).toBe("a{--x:(a b)}");
883+
// Against the delimiters nothing fuses, so the comment simply goes.
884+
expect(min("a{--x:foo(/*c*/a/*c*/)}")).toBe("a{--x:foo(a)}");
885+
expect(min("a{--x:foo(1px/*c*/2px)/*c*/bar()}")).toBe(
886+
"a{--x:foo(1px 2px)bar()}"
887+
);
888+
// Whitespace is a token of its own, so it is still written as it stands.
889+
expect(min("a{--x:foo( /*c*/ a )}")).toBe("a{--x:foo( a )}");
890+
// A kept one is placed where it stood, at depth too.
891+
expect(min("a{--x:foo(a/*!k*/b)}")).toBe("a{--x:foo(a/*!k*/b)}");
892+
// A function closed at EOF has no `)` to write back.
893+
expect(min("a{--x:foo(a/*c*/b")).toBe("a{--x:foo(a b}");
894+
});
895+
840896
it("separates rewritten numbers that would fuse", () => {
841897
// `1.0.5` is two numbers; normalized to `1` and `.5` they would join as the
842898
// single number `1.5`.
@@ -2314,6 +2370,26 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
23142370
});
23152371
});
23162372

2373+
describe("a comment parting two components", () => {
2374+
// The comment ends both tokens, so a rewritten value has to keep them apart.
2375+
it.each([
2376+
["a{margin:1px 1px/*c*/1px 1px}", "a{margin:1px}"],
2377+
["a{margin:1px/*c*/2px 1px 2px}", "a{margin:1px 2px}"],
2378+
["a{box-shadow:1px 1px/*c*/2px 0 red}", "a{box-shadow:1px 1px 2px red}"],
2379+
[
2380+
"a{transition:opacity/*c*/1s ease-in 2s}",
2381+
"a{transition:opacity 1s 2s ease-in}"
2382+
],
2383+
["a{background-position:left/*c*/top}", "a{background-position:0%0%}"],
2384+
[
2385+
"a{grid-template-columns:1fr/*c*/1fr}",
2386+
"a{grid-template-columns:1fr 1fr}"
2387+
]
2388+
])("%s", (css, expected) => {
2389+
expect(minify(css)).toBe(expected);
2390+
});
2391+
});
2392+
23172393
describe("a value holding a substitution", () => {
23182394
// The engine keeps such a value as the token stream it was written as until
23192395
// the substitution resolves, so every rewrite inside one is declined.

‎test/configCases/css/minimize-selectors/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,7 @@ Array [
7070
"/*! webpack: parted from the compound it is a descendant combinator */* .t71{color:red}",
7171
"/*! webpack: ...and on its own it is the selector */*{color:red}",
7272
"/*! webpack: a namespaced universal is not redundant */svg|*,*|*.t72{color:red}",
73+
"/*! webpack: whitespace between two type selectors is a descendant combinator,
74+
so the comment parting these stays as an empty one */a/**/b{color:red}",
7375
]
7476
`;

‎test/configCases/css/minimize-selectors/__snapshots__/ConfigTest.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,7 @@ Array [
7070
"/*! webpack: parted from the compound it is a descendant combinator */* .t71{color:red}",
7171
"/*! webpack: ...and on its own it is the selector */*{color:red}",
7272
"/*! webpack: a namespaced universal is not redundant */svg|*,*|*.t72{color:red}",
73+
"/*! webpack: whitespace between two type selectors is a descendant combinator,
74+
so the comment parting these stays as an empty one */a/**/b{color:red}",
7375
]
7476
`;

‎test/configCases/css/minimize-selectors/style.css‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,6 @@
167167
* { color: red }
168168
/*! webpack: a namespaced universal is not redundant */
169169
svg|*, *|*.t72 { color: red }
170+
/*! webpack: whitespace between two type selectors is a descendant combinator,
171+
so the comment parting these stays as an empty one */
172+
a/*c*/b { color: red }

‎test/configCases/css/minimize-values/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,8 @@ Array [
148148
5-digit hash would invalidate the shorthand and drop the width and style too */.t137{outline-width:3px;outline-style:dashed;outline-color:#12345}",
149149
"/*! webpack: declined: \`list-style-type\` takes any identifier, so no other value
150150
in the family is unambiguous — an image included */.t133{list-style-type:square;list-style-position:inside;list-style-image:url(data:image/gif;base64,R0lGODlhAQABAAAAACw=)}",
151+
"/*! webpack: a comment parts two components, so a rewrite keeps them apart */.t138{margin:1px}.t139{transition:opacity 1s 2s ease-in}",
152+
"/*! webpack: a custom property's value stays as written, but a comment in it
153+
minifies to the boundary it stands for */.t140{--t140:1px 1px 1px 1px}.t141{--t141:1px 1px/*!k*/1px 1px}.t142{--t142:minmax(1px 2px, calc(3px 4px))}",
151154
]
152155
`;

‎test/configCases/css/minimize-values/__snapshots__/ConfigTest.snap‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,8 @@ Array [
148148
5-digit hash would invalidate the shorthand and drop the width and style too */.t137{outline-width:3px;outline-style:dashed;outline-color:#12345}",
149149
"/*! webpack: declined: \`list-style-type\` takes any identifier, so no other value
150150
in the family is unambiguous — an image included */.t133{list-style-type:square;list-style-position:inside;list-style-image:url(data:image/gif;base64,R0lGODlhAQABAAAAACw=)}",
151+
"/*! webpack: a comment parts two components, so a rewrite keeps them apart */.t138{margin:1px}.t139{transition:opacity 1s 2s ease-in}",
152+
"/*! webpack: a custom property's value stays as written, but a comment in it
153+
minifies to the boundary it stands for */.t140{--t140:1px 1px 1px 1px}.t141{--t141:1px 1px/*!k*/1px 1px}.t142{--t142:minmax(1px 2px, calc(3px 4px))}",
151154
]
152155
`;

‎test/configCases/css/minimize-values/style.css‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,11 @@
327327
/*! webpack: declined: `list-style-type` takes any identifier, so no other value
328328
in the family is unambiguous — an image included */
329329
.t133 { list-style-type: square; list-style-position: inside; list-style-image: url("data:image/gif;base64,R0lGODlhAQABAAAAACw=") }
330+
/*! webpack: a comment parts two components, so a rewrite keeps them apart */
331+
.t138 { margin: 1px 1px/*c*/1px 1px }
332+
.t139 { transition: opacity/*c*/1s ease-in 2s }
333+
/*! webpack: a custom property's value stays as written, but a comment in it
334+
minifies to the boundary it stands for */
335+
.t140 { --t140: 1px 1px/*c*/1px 1px }
336+
.t141 { --t141: 1px 1px/*!k*/1px 1px }
337+
.t142 { --t142: minmax(1px/*c*/2px, calc(3px/*c*/4px)) }

0 commit comments

Comments
 (0)