Skip to content

Commit 1496047

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 4d41df2 commit 1496047

2 files changed

Lines changed: 79 additions & 7 deletions

File tree

‎lib/css/syntax.js‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5312,6 +5312,18 @@ const _prefixedSelectorName = (name) => {
53125312
return _prefixedSelectorNames.get(name);
53135313
};
53145314

5315+
// A selector prelude part split at its `(`: a functional pseudo prints as one
5316+
// token (`dir(rtl)`), so its name is matched and rewritten apart from the
5317+
// argument that carries over unchanged. A bare pseudo has no `(`.
5318+
/**
5319+
* @param {string} part a printed prelude token
5320+
* @returns {string} its name, without any `(argument)`
5321+
*/
5322+
const _selectorPartName = (part) => {
5323+
const paren = part.indexOf("(");
5324+
return paren === -1 ? part : part.slice(0, paren);
5325+
};
5326+
53155327
/**
53165328
* A qualified rule prefixed against the target when its selector is a single
53175329
* prefixable pseudo (`::placeholder`): a copy with the pseudo's engine spelling
@@ -5340,17 +5352,24 @@ const _prefixQualifiedRule = (path, ruleText, parts, soft, body, writer) => {
53405352
if (
53415353
i !== 0 &&
53425354
parts[i - 1] === ":" &&
5343-
(PREFIXED_SELECTORS.has(parts[i].toLowerCase()) ||
5344-
_prefixedSelectorName(parts[i].toLowerCase()) !== undefined)
5355+
(PREFIXED_SELECTORS.has(_selectorPartName(parts[i]).toLowerCase()) ||
5356+
_prefixedSelectorName(_selectorPartName(parts[i]).toLowerCase()) !==
5357+
undefined)
53455358
) {
53465359
if (nameIndex !== -1) return ruleText;
53475360
nameIndex = i;
53485361
}
53495362
}
53505363
if (nameIndex === -1) return ruleText;
5351-
const found = _prefixedSelectorName(parts[nameIndex].toLowerCase());
5352-
const base = found === undefined ? parts[nameIndex].toLowerCase() : found[0];
5353-
const signature = `s${_join([...parts.slice(0, nameIndex), base, ...parts.slice(nameIndex + 1)], false, _TRIM_COMBINATORS)}`;
5364+
// A functional pseudo (`dir(rtl)`) keeps its argument; only the name is
5365+
// matched and swapped, and the argument is part of the sibling signature so
5366+
// `:dir(rtl)` and `:dir(ltr)` stay distinct.
5367+
const raw = parts[nameIndex];
5368+
const nameOnly = _selectorPartName(raw).toLowerCase();
5369+
const args = raw.slice(_selectorPartName(raw).length);
5370+
const found = _prefixedSelectorName(nameOnly);
5371+
const base = found === undefined ? nameOnly : found[0];
5372+
const signature = `s${_join([...parts.slice(0, nameIndex), base + args, ...parts.slice(nameIndex + 1)], false, _TRIM_COMBINATORS)}`;
53545373
/**
53555374
* @param {string} name the pseudo's spelling to print with
53565375
* @returns {string} the rule with that pseudo
@@ -5380,7 +5399,7 @@ const _prefixQualifiedRule = (path, ruleText, parts, soft, body, writer) => {
53805399
// of this signature, so a later one that overrides an earlier keeps its
53815400
// prefixed twin winning too.
53825401
if (seen.has(`${signature}\0${prefix}`)) continue;
5383-
out += withName(prefix + base);
5402+
out += withName(prefix + base + args);
53845403
}
53855404
return out + ruleText;
53865405
};
@@ -7137,7 +7156,11 @@ const printer = (path, writer) => {
71377156
}
71387157
}
71397158
const ruleText = `${prelude}${soft}{${body}${nl}}`;
7140-
if (minify && _seenPrefixRules !== null) {
7159+
// Rule-level prefixing is top-level only (`path.parent === null`):
7160+
// `_seenPrefixRules` has no nesting key, so a nested `@keyframes` / pseudo
7161+
// must not add or drop against a same-named top-level twin in another
7162+
// scope. A nested rule's declarations are still prefixed in the block above.
7163+
if (minify && _seenPrefixRules !== null && path.parent === null) {
71417164
if (path.type() === T_AT_RULE) {
71427165
return _prefixAtRule(path, ruleText, prelude, writer);
71437166
}

‎test/CssSyntax.unittest.js‎

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

2699+
it("drops another engine's prefix a chrome-only target never reads", () => {
2700+
expect(
2701+
minify("a{-moz-user-select:none;user-select:none}", ["chrome 120"])
2702+
).toBe("a{user-select:none}");
2703+
});
2704+
26992705
it("reads a browser version's minor part, not just the major", () => {
27002706
// `appearance` loses its prefix in Safari at 15.4; 15.3 still needs `-webkit-`.
27012707
expect(minify("a{appearance:none}", ["safari 15.3"])).toBe(
@@ -2791,6 +2797,23 @@ describe("CssSyntax minify — vendor prefixes (at-rules)", () => {
27912797
);
27922798
});
27932799

2800+
it("leaves a nested at-rule alone — its scope has no top-level twin", () => {
2801+
expect(
2802+
minify("@media screen{@keyframes s{to{opacity:1}}}", ["chrome 40"])
2803+
).toBe("@media screen{@keyframes s{to{opacity:1}}}");
2804+
});
2805+
2806+
it("does not suppress a scoped copy from a top-level prefixed rule", () => {
2807+
expect(
2808+
minify(
2809+
"@-webkit-keyframes s{to{opacity:1}}@media screen{@keyframes s{to{opacity:.3}}}",
2810+
["chrome 40"]
2811+
)
2812+
).toBe(
2813+
"@-webkit-keyframes s{to{opacity:1}}@media screen{@keyframes s{to{opacity:.3}}}"
2814+
);
2815+
});
2816+
27942817
it("does nothing without a target list", () => {
27952818
expect(minify("@keyframes s{to{opacity:1}}")).toBe(
27962819
"@keyframes s{to{opacity:1}}"
@@ -2862,6 +2885,32 @@ describe("CssSyntax minify — vendor prefixes (selectors)", () => {
28622885
);
28632886
});
28642887

2888+
it("prefixes a functional pseudo, carrying its argument", () => {
2889+
expect(minify(":dir(rtl){color:red}", ["firefox 40"])).toBe(
2890+
":-moz-dir(rtl){color:red}:dir(rtl){color:red}"
2891+
);
2892+
});
2893+
2894+
it("keeps two functional pseudos with different arguments distinct", () => {
2895+
expect(
2896+
minify(":dir(rtl){color:red}:dir(ltr){color:blue}", ["firefox 40"])
2897+
).toBe(
2898+
":-moz-dir(rtl){color:red}:dir(rtl){color:red}:-moz-dir(ltr){color:blue}:dir(ltr){color:blue}"
2899+
);
2900+
});
2901+
2902+
it("leaves a pseudo inside a functional selector untouched", () => {
2903+
expect(minify(":is(:autofill){color:red}", ["chrome 40"])).toBe(
2904+
":is(:autofill){color:red}"
2905+
);
2906+
});
2907+
2908+
it("leaves a nested rule alone — its scope has no top-level twin", () => {
2909+
expect(
2910+
minify("@media screen{::placeholder{color:red}}", ["chrome 40"])
2911+
).toBe("@media screen{::placeholder{color:red}}");
2912+
});
2913+
28652914
it("does nothing without a target list", () => {
28662915
expect(minify("::placeholder{color:red}")).toBe("::placeholder{color:red}");
28672916
});

0 commit comments

Comments
 (0)