Skip to content

Commit 1bb4658

Browse files
committed
fix(css): scope prefixing to top-level rules and prefix functional pseudos
Two edge cases: the sibling set that tracks at-rules and selectors has no nesting key, so restrict rule-level prefixing to top-level rules — a nested `@keyframes` or pseudo no longer adds or drops against a same-named top-level rule in another scope (a nested rule's declarations are still prefixed). And a functional pseudo like `:dir(rtl)` prints as one token, so match and rewrite its name apart from the argument, which is kept in the sibling signature so `:dir(rtl)` and `:dir(ltr)` stay distinct.
1 parent c54796e commit 1bb4658

3 files changed

Lines changed: 80 additions & 10 deletions

File tree

‎lib/css/syntax.js‎

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6022,6 +6022,18 @@ const _prefixedSelectorName = (name) => {
60226022
return _prefixedSelectorNames.get(name);
60236023
};
60246024

6025+
// A selector prelude part split at its `(`: a functional pseudo prints as one
6026+
// token (`dir(rtl)`), so its name is matched and rewritten apart from the
6027+
// argument that carries over unchanged. A bare pseudo has no `(`.
6028+
/**
6029+
* @param {string} part a printed prelude token
6030+
* @returns {string} its name, without any `(argument)`
6031+
*/
6032+
const _selectorPartName = (part) => {
6033+
const paren = part.indexOf("(");
6034+
return paren === -1 ? part : part.slice(0, paren);
6035+
};
6036+
60256037
/**
60266038
* A qualified rule prefixed against the target when its selector is a single
60276039
* prefixable pseudo (`::placeholder`): a copy with the pseudo's engine spelling
@@ -6050,17 +6062,24 @@ const _prefixQualifiedRule = (path, ruleText, parts, soft, body, writer) => {
60506062
if (
60516063
i !== 0 &&
60526064
parts[i - 1] === ":" &&
6053-
(PREFIXED_SELECTORS.has(parts[i].toLowerCase()) ||
6054-
_prefixedSelectorName(parts[i].toLowerCase()) !== undefined)
6065+
(PREFIXED_SELECTORS.has(_selectorPartName(parts[i]).toLowerCase()) ||
6066+
_prefixedSelectorName(_selectorPartName(parts[i]).toLowerCase()) !==
6067+
undefined)
60556068
) {
60566069
if (nameIndex !== -1) return ruleText;
60576070
nameIndex = i;
60586071
}
60596072
}
60606073
if (nameIndex === -1) return ruleText;
6061-
const found = _prefixedSelectorName(parts[nameIndex].toLowerCase());
6062-
const base = found === undefined ? parts[nameIndex].toLowerCase() : found[0];
6063-
const signature = `s${_join([...parts.slice(0, nameIndex), base, ...parts.slice(nameIndex + 1)], false, _TRIM_COMBINATORS)}`;
6074+
// A functional pseudo (`dir(rtl)`) keeps its argument; only the name is
6075+
// matched and swapped, and the argument is part of the sibling signature so
6076+
// `:dir(rtl)` and `:dir(ltr)` stay distinct.
6077+
const raw = parts[nameIndex];
6078+
const nameOnly = _selectorPartName(raw).toLowerCase();
6079+
const args = raw.slice(_selectorPartName(raw).length);
6080+
const found = _prefixedSelectorName(nameOnly);
6081+
const base = found === undefined ? nameOnly : found[0];
6082+
const signature = `s${_join([...parts.slice(0, nameIndex), base + args, ...parts.slice(nameIndex + 1)], false, _TRIM_COMBINATORS)}`;
60646083
/**
60656084
* @param {string} name the pseudo's spelling to print with
60666085
* @returns {string} the rule with that pseudo
@@ -6090,7 +6109,7 @@ const _prefixQualifiedRule = (path, ruleText, parts, soft, body, writer) => {
60906109
// of this signature, so a later one that overrides an earlier keeps its
60916110
// prefixed twin winning too.
60926111
if (seen.has(`${signature}\0${prefix}`)) continue;
6093-
out += withName(prefix + base);
6112+
out += withName(prefix + base + args);
60946113
}
60956114
return out + ruleText;
60966115
};
@@ -8716,7 +8735,7 @@ const _printAttributeSelector = (path, children, writer) => {
87168735
* @param {CssPath} path the accessor positioned on the rule
87178736
* @param {PrintContext} writer the print context
87188737
* @param {boolean} minify whether printing minified
8719-
* @param {string[]=} outParts filled with the prelude's tokens, for the prefixer
8738+
* @param {string[]=} outParts filled with the prelude's tokens, for prefixing
87208739
* @returns {string} the rule's prelude text
87218740
*/
87228741
const _rulePrelude = (path, writer, minify, outParts) => {
@@ -9109,7 +9128,7 @@ const printer = (path, writer) => {
91099128
}
91109129
case T_AT_RULE:
91119130
case T_QUALIFIED_RULE: {
9112-
// The prefixer reads the prelude's tokens, not its joined text.
9131+
// Prefixing reads the prelude's tokens, not its joined text.
91139132
const preludeParts =
91149133
minify && _seenPrefixRules !== null
91159134
? /** @type {string[]} */ ([])
@@ -9284,8 +9303,11 @@ const printer = (path, writer) => {
92849303
const text = `${prelude}${soft}{${body}${nl}}`;
92859304
// Prefix copies turn one rule into several, so a rewritten rule is not
92869305
// offered for joining — its text no longer describes a single block.
9306+
// Top-level only: `_seenPrefixRules` has no nesting key, so a nested
9307+
// `@keyframes` / pseudo must not add or drop against a same-named
9308+
// top-level twin in another scope. Declarations are prefixed above.
92879309
const out =
9288-
minify && _seenPrefixRules !== null
9310+
minify && _seenPrefixRules !== null && path.parent === null
92899311
? path.type() === T_AT_RULE
92909312
? _prefixAtRule(path, text, prelude, writer)
92919313
: _prefixQualifiedRule(

‎test/CssSyntax.unittest.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4384,6 +4384,12 @@ describe("CssSyntax minify — vendor prefixes (properties)", () => {
43844384
expect(minify("a{color:red}", ["chrome 40"])).toBe("a{color:red}");
43854385
});
43864386

4387+
it("drops another engine's prefix a chrome-only target never reads", () => {
4388+
expect(
4389+
minify("a{-moz-user-select:none;user-select:none}", ["chrome 120"])
4390+
).toBe("a{user-select:none}");
4391+
});
4392+
43874393
it("reads a browser version's minor part, not just the major", () => {
43884394
// `appearance` loses its prefix in Safari at 15.4; 15.3 still needs `-webkit-`.
43894395
expect(minify("a{appearance:none}", ["safari 15.3"])).toBe(
@@ -4479,6 +4485,23 @@ describe("CssSyntax minify — vendor prefixes (at-rules)", () => {
44794485
);
44804486
});
44814487

4488+
it("leaves a nested at-rule alone — its scope has no top-level twin", () => {
4489+
expect(
4490+
minify("@media screen{@keyframes s{to{opacity:1}}}", ["chrome 40"])
4491+
).toBe("@media screen{@keyframes s{to{opacity:1}}}");
4492+
});
4493+
4494+
it("does not suppress a scoped copy from a top-level prefixed rule", () => {
4495+
expect(
4496+
minify(
4497+
"@-webkit-keyframes s{to{opacity:1}}@media screen{@keyframes s{to{opacity:.3}}}",
4498+
["chrome 40"]
4499+
)
4500+
).toBe(
4501+
"@-webkit-keyframes s{to{opacity:1}}@media screen{@keyframes s{to{opacity:.3}}}"
4502+
);
4503+
});
4504+
44824505
it("does nothing without a target list", () => {
44834506
expect(minify("@keyframes s{to{opacity:1}}")).toBe(
44844507
"@keyframes s{to{opacity:1}}"
@@ -4550,6 +4573,32 @@ describe("CssSyntax minify — vendor prefixes (selectors)", () => {
45504573
);
45514574
});
45524575

4576+
it("prefixes a functional pseudo, carrying its argument", () => {
4577+
expect(minify(":dir(rtl){color:red}", ["firefox 40"])).toBe(
4578+
":-moz-dir(rtl){color:red}:dir(rtl){color:red}"
4579+
);
4580+
});
4581+
4582+
it("keeps two functional pseudos with different arguments distinct", () => {
4583+
expect(
4584+
minify(":dir(rtl){color:red}:dir(ltr){color:blue}", ["firefox 40"])
4585+
).toBe(
4586+
":-moz-dir(rtl){color:red}:dir(rtl){color:red}:-moz-dir(ltr){color:blue}:dir(ltr){color:blue}"
4587+
);
4588+
});
4589+
4590+
it("leaves a pseudo inside a functional selector untouched", () => {
4591+
expect(minify(":is(:autofill){color:red}", ["chrome 40"])).toBe(
4592+
":is(:autofill){color:red}"
4593+
);
4594+
});
4595+
4596+
it("leaves a nested rule alone — its scope has no top-level twin", () => {
4597+
expect(
4598+
minify("@media screen{::placeholder{color:red}}", ["chrome 40"])
4599+
).toBe("@media screen{::placeholder{color:red}}");
4600+
});
4601+
45534602
it("does nothing without a target list", () => {
45544603
expect(minify("::placeholder{color:red}")).toBe("::placeholder{color:red}");
45554604
});

‎tooling/generate-css-data.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,6 @@ const mapLiteral = (entries) =>
25192519
`new Map([${entries.map(([key, value]) => `["${key}", "${value}"]`).join(", ")}])`;
25202520

25212521
/**
2522-
25232522
* @param {[string, number][]} entries string-keyed, number-valued pairs
25242523
* @returns {string} the `Map` literal
25252524
*/

0 commit comments

Comments
 (0)