Skip to content

Commit c050ac3

Browse files
fix(css): record a rule flushed by an at-rule it cannot join
A top-level node that is not a rule the printer can join — `@keyframes`, `@font-face` — is written out on its own path, and that path also writes out the rule waiting behind it. Neither was noted, so a later rule could take its selectors past declarations that were never recorded: with `.a{color:red} .b{color:blue} @Keyframes k{…} .c{color:red}`, `.c` joined `.a` and an element matching `.b` and `.c` read blue where it had read red. Over five framework stylesheets that path writes out a held rule 58 times. The note now happens inside `_emitHeld`, so every path that flushes one records it, and a node this cannot see into is noted as declaring everything. Reported by CodeRabbit on #21969.
1 parent 8ada8da commit c050ac3

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎lib/css/syntax.js‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5533,8 +5533,8 @@ const _emitTopLevel = (writer, node, offset, line, column, text, own) => {
55335533
* @param {HeldTopLevel} held the held rule
55345534
* @returns {TakenPiece | null} the piece it went out as
55355535
*/
5536-
const _emitHeld = (writer, held) =>
5537-
_emitTopLevel(
5536+
const _emitHeld = (writer, held) => {
5537+
const taken = _emitTopLevel(
55385538
writer,
55395539
held.node,
55405540
held.offset,
@@ -5544,6 +5544,11 @@ const _emitHeld = (writer, held) =>
55445544
held.own
55455545
);
55465546

5547+
if (_mergingDistantRules) _noteDistantRule(taken, held.entry);
5548+
5549+
return taken;
5550+
};
5551+
55475552
/**
55485553
* Note a top-level node the walk has just passed: what it declares, so a later
55495554
* candidate knows whether anything since can shadow its block, and the node
@@ -5674,6 +5679,9 @@ const _takeTopLevel = (node, writer, offset, line, column) => {
56745679
_emitHeld(writer, held);
56755680
}
56765681
const at = writer.takeRetractable(node, offset, line, column, own);
5682+
// Noted without its piece: one still open to being retracted is not one a
5683+
// later rule may put its selectors into.
5684+
if (_mergingDistantRules) _noteDistantRule(null, entry);
56775685
const scope = _prefixScope(null);
56785686
if (scope.retractable === null) scope.retractable = new Map();
56795687
scope.retractable.set(candidate.signature, at);
@@ -5690,6 +5698,9 @@ const _takeTopLevel = (node, writer, offset, line, column) => {
56905698
_emitHeld(writer, held);
56915699
}
56925700
_emitTopLevel(writer, node, offset, line, column, own, spans);
5701+
// Anything this cannot see into stands in the way of every later merge,
5702+
// which is what noting a node that is not a plain rule spells.
5703+
if (_mergingDistantRules) _noteDistantRule(null, entry);
56935704
return;
56945705
}
56955706
// A kept comment between the two was written above the second rule.
@@ -5705,8 +5716,7 @@ const _takeTopLevel = (node, writer, offset, line, column) => {
57055716
}
57065717
}
57075718
if (held !== null) {
5708-
const taken = _emitHeld(writer, held);
5709-
if (_mergingDistantRules) _noteDistantRule(taken, held.entry);
5719+
_emitHeld(writer, held);
57105720
}
57115721
if (_mergingDistantRules && _mergeIntoDistantRule(writer, entry)) {
57125722
// Its selectors went into a rule already out, so it prints nothing itself

‎test/CssSyntax.unittest.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10141,6 +10141,27 @@ describe("SourceProcessor — mergeDistantRules", () => {
1014110141
expect(minify(sheet, true)).toBe(sheet);
1014210142
});
1014310143

10144+
// An at-rule this cannot join is emitted on its own path, which is where a
10145+
// rule waiting behind it is written out — and it declares what it declares.
10146+
it.each([
10147+
["@keyframes", "@keyframes k{from{opacity:0}}"],
10148+
["@font-face", "@font-face{font-family:x;src:url(a.woff2)}"]
10149+
])("keeps what a rule flushed by `%s` declares", (name, between) => {
10150+
const sheet = `.a{color:red}.b{color:blue}${between}.c{color:red}`;
10151+
expect(minify(sheet, true)).toBe(minify(sheet));
10152+
});
10153+
10154+
it.each([
10155+
["@keyframes", "@keyframes k{from{opacity:0}}"],
10156+
["@font-face", "@font-face{font-family:x;src:url(a.woff2)}"]
10157+
])(
10158+
"declines across `%s`, which it cannot see into either",
10159+
(name, between) => {
10160+
const sheet = `.a{color:red}${between}.c{color:red}`;
10161+
expect(minify(sheet, true)).toBe(minify(sheet));
10162+
}
10163+
);
10164+
1014410165
it("merges where the rule between touches nothing the block does", () => {
1014510166
expect(minify(".a{color:red}.b{margin:0}.c{color:red}", true)).toContain(
1014610167
".a,.c"

0 commit comments

Comments
 (0)