Skip to content

Commit 43bbd32

Browse files
fix(css): keep a rewritten nested rule, and group a custom media list
Two the review found, both confirmed against the code first. A nested rule the prefixes rewrote was dropped whole: the caller handed `_writeNested` no block for it, and the hoist then queued nothing and answered null, so the rule went nowhere. It is several rules with preludes of their own and no one rule to take out, so it stays where it was written; the rules written in it still come out against it. A `@custom-media` naming alternatives was written into a condition whatever stood beside it, leaving an `and` binding to the last alternative alone — `screen and (--m)` for `(a),(b)` says `(screen and a) or b`. CSS has no parentheses to group a list inside a condition, so one is now written only where the condition is the whole prelude; a query of one condition is written wherever it is asked for.
1 parent 167a23a commit 43bbd32

2 files changed

Lines changed: 81 additions & 11 deletions

File tree

‎lib/css/syntax.js‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10347,9 +10347,10 @@ const _declarationsPrecedeRules = (path, rule) => {
1034710347
* @param {Node} node the rule itself, which is what its own entries name
1034810348
* @param {string} prelude the rule's printed prelude
1034910349
* @param {string | null} rest the rule's own block, or null where it writes none
10350+
* @param {boolean} hoistable false where the rule is several rules with preludes of their own, which stay where they were written
1035010351
* @returns {string | null} the text to write after the rule, or null when the rule goes on the list itself
1035110352
*/
10352-
const _writeNested = (path, node, prelude, rest) => {
10353+
const _writeNested = (path, node, prelude, rest, hoistable) => {
1035310354
// Only what was written in this rule: the walk is post-order, so an earlier
1035410355
// sibling's rules are on the list too, waiting for the parent they share.
1035510356
/** @type {{ prelude: string, rest: string, owner: Node | null }[]} */
@@ -10361,7 +10362,7 @@ const _writeNested = (path, node, prelude, rest) => {
1036110362
one.prelude = _resolveNestedPrelude(one.prelude, prelude);
1036210363
taken.unshift(one);
1036310364
}
10364-
const hoisted = _hoistedOutOf(path, path.parent);
10365+
const hoisted = hoistable && _hoistedOutOf(path, path.parent);
1036510366
const owner = hoisted ? path.parent : null;
1036610367
if (hoisted) {
1036710368
if (rest !== null) _pendingNested.push({ prelude, rest, owner });
@@ -10418,16 +10419,35 @@ const _takeCustomMedia = (prelude) => {
1041810419
*
1041910420
* Only a name already taken down: the rules are read in the order they are
1042010421
* written, so one stating the name after this asks for it is not there to be
10421-
* written out, and the condition is left as the author had it.
10422+
* written out, and the condition is left as the author had it. A query naming
10423+
* alternatives is only written where the condition is the whole prelude —
10424+
* anywhere else the `and` beside it would bind to the last alternative alone,
10425+
* and CSS has no parentheses to write a list inside a condition with.
1042210426
* @param {string[]} parts the prelude's printed parts
1042310427
* @returns {void}
1042410428
*/
1042510429
const _writeCustomMedia = (parts) => {
10426-
for (let i = 0; i < parts.length; i++) {
10430+
// The prelude's one condition, where it has exactly one: `parts[0]` is the
10431+
// rule's own name, and the rest is the condition and the spaces around it.
10432+
let written = 0;
10433+
let alone = -1;
10434+
for (let i = 1; i < parts.length; i++) {
10435+
if (parts[i].trim().length === 0) continue;
10436+
written++;
10437+
alone = i;
10438+
}
10439+
for (let i = 1; i < parts.length; i++) {
1042710440
const found = _CUSTOM_MEDIA_REFERENCE_RE.exec(parts[i]);
1042810441
if (found === null) continue;
1042910442
const query = _customMedia.get(found[1]);
10430-
if (query !== undefined) parts[i] = query;
10443+
if (query === undefined) continue;
10444+
if (
10445+
!(written === 1 && alone === i) &&
10446+
_splitTopLevelArguments(query).length !== 1
10447+
) {
10448+
continue;
10449+
}
10450+
parts[i] = query;
1043110451
}
1043210452
};
1043310453

@@ -15590,7 +15610,8 @@ const printer = (path, writer) => {
1559015610
path,
1559115611
/** @type {Node} */ (_currentNode),
1559215612
prelude,
15593-
null
15613+
null,
15614+
true
1559415615
) || ""
1559515616
);
1559615617
}
@@ -15669,12 +15690,13 @@ const printer = (path, writer) => {
1566915690
);
1567015691
}
1567115692
// A rewritten rule is several rules with preludes of their own, so it
15672-
// stays where it was written rather than being taken out.
15693+
// stays put — the rules written in it still come out against it.
1567315694
const nested = _writeNested(
1567415695
path,
1567515696
/** @type {Node} */ (_currentNode),
1567615697
prelude,
15677-
out === text ? rest : null
15698+
rest,
15699+
out === text
1567815700
);
1567915701
if (nested === null) return "";
1568015702
if (out !== text) return out + nested;

‎test/CssSyntax.unittest.js‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9413,6 +9413,22 @@ describe("CssSyntax minify — nesting the target cannot read", () => {
94139413
).toBe(css);
94149414
});
94159415

9416+
it("keeps a rule the prefixes rewrote where it was written", () => {
9417+
// Prefix copies are several rules with preludes of their own, so there is
9418+
// no one rule to take out — it stays, and its siblings still come out.
9419+
expect(
9420+
minifyFor("a{color:red;&:autofill{color:blue}}", ["chrome 100"])
9421+
).toBe("a{color:red;&:-webkit-autofill{color:blue}&:autofill{color:blue}}");
9422+
expect(
9423+
minifyFor("a{color:red;&:autofill{color:blue}& b{color:teal}}", [
9424+
"chrome 100"
9425+
])
9426+
).toBe(
9427+
"a{color:red;&:-webkit-autofill{color:blue}&:autofill{color:blue}}" +
9428+
"a b{color:teal}"
9429+
);
9430+
});
9431+
94169432
it("leaves a rule written in an at-rule where it stands", () => {
94179433
// The at-rule is what it is written in, and taking it out of that would
94189434
// take it out of the query too.
@@ -9501,16 +9517,48 @@ describe("CssSyntax minify — `@custom-media`", () => {
95019517
expect(minifyFor(sheet, ["chrome 120"])).toBe(sheet);
95029518
});
95039519

9520+
it("writes a query naming alternatives only as the whole condition", () => {
9521+
// A list written beside an `and` would leave it binding to the last
9522+
// alternative alone, and CSS has no parentheses to group one with.
9523+
const list = "@custom-media --m (width>400px),(orientation:portrait);";
9524+
expect(
9525+
minifyForWith(`${list}@media (--m){a{color:red}}`, ["chrome 120"], {
9526+
customMedia: true
9527+
})
9528+
).toBe("@media (width>400px),(orientation:portrait){a{color:red}}");
9529+
expect(
9530+
minifyForWith(
9531+
`${list}@media screen and (--m){a{color:red}}`,
9532+
["chrome 120"],
9533+
{ customMedia: true }
9534+
)
9535+
).toBe("@media screen and (--m){a{color:red}}");
9536+
});
9537+
9538+
it("writes a query of one condition wherever it is asked for", () => {
9539+
expect(
9540+
minifyForWith(
9541+
"@custom-media --w (width>400px);@media screen and (--w){a{color:red}}",
9542+
["chrome 120"],
9543+
{ customMedia: true }
9544+
)
9545+
).toBe("@media screen and (width>400px){a{color:red}}");
9546+
});
9547+
95049548
it("leaves a rule stating no query alone", () => {
95059549
const css = "@custom-media --m;@media (--m){a{color:red}}";
95069550
expect(minifyForWith(css, ["chrome 120"], { customMedia: true })).toBe(css);
95079551
});
95089552

95099553
it("leaves a name nothing states alone", () => {
9554+
// Beside a rule that states another, so the substitution does run and
9555+
// finds nothing for this one.
95109556
expect(
9511-
minifyForWith("@media (--nope){a{color:red}}", ["chrome 120"], {
9512-
customMedia: true
9513-
})
9557+
minifyForWith(
9558+
"@custom-media --m (width>400px);@media (--nope){a{color:red}}",
9559+
["chrome 120"],
9560+
{ customMedia: true }
9561+
)
95149562
).toBe("@media (--nope){a{color:red}}");
95159563
});
95169564

0 commit comments

Comments
 (0)