Skip to content

Commit 128a610

Browse files
fix(css): join no rule onto a selector an engine does not parse
Adjacent rules with the same block were joined whatever pseudo a selector held: a @page pseudo-class, a pseudo given an argument it takes none of or missing one it needs, a pseudo-element with more after it, or one no shipping Chromium reads. Each makes the joined rule invalid, so its other selector lost the block too.
1 parent d5d84cd commit 128a610

6 files changed

Lines changed: 206 additions & 124 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+
Keep a CSS rule apart from an identical neighbor whose selector no browser parses, which a join would drop it with.

‎lib/css/data.js‎

Lines changed: 100 additions & 104 deletions
Large diffs are not rendered by default.

‎lib/css/syntax-parser.js‎

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const {
5353
MERGEABLE_AT_RULES,
5454
MERGE_LONGHANDS,
5555
NEVER,
56+
NTH_PSEUDO_FUNCTIONS,
5657
ONE_VALUE_PAIR_SHORTHANDS,
5758
ORDERED_LONGHANDS,
5859
PAIR_LONGHANDS,
@@ -9166,6 +9167,26 @@ const _readsAll = (profile) => {
91669167
return true;
91679168
};
91689169

9170+
// The browsers standing for the three engines that ship today.
9171+
const _ENGINE_BROWSERS = ["chrome", "firefox", "safari"].map((name) =>
9172+
SUPPORT_BROWSERS.indexOf(name)
9173+
);
9174+
9175+
/**
9176+
* Whether each engine shipping today reads an ability in some release, which is
9177+
* what a selector needs where no selection names the browsers to answer for.
9178+
* @param {number} profile its `getSupportProfiles()` row
9179+
* @returns {boolean} true when Chromium, Gecko and WebKit all read it
9180+
*/
9181+
const _readByEveryEngine = (profile) => {
9182+
const row = profile * SUPPORT_BROWSERS.length;
9183+
const profiles = getSupportProfiles();
9184+
for (const at of _ENGINE_BROWSERS) {
9185+
if (profiles[row + at] === NEVER) return false;
9186+
}
9187+
return true;
9188+
};
9189+
91699190
/**
91709191
* The vendor spellings at least one target browser still needs for a construct —
91719192
* a target at version V needs spelling S when some `[browser, from, to]` of S has
@@ -14828,21 +14849,29 @@ const _STRING = String.raw`"(?:[^"\\]|\\[\s\S])*"|'(?:[^'\\]|\\[\s\S])*'`;
1482814849
// The matchers Selectors 4 §6 defines, and only `i` after one: an engine that
1482914850
// cannot read the `s` modifier drops the selector, and with it the list.
1483014851
const _ATTRIBUTE = String.raw`\[(?:${_IDENT}\|)?${_IDENT}(?:[~|^$*]?=(?:${_IDENT}|${_STRING})(?: [iI])?)?\]`;
14831-
// A pseudo-class or pseudo-element with no selector inside it: argument-less, or
14832-
// the `An+B` of `:nth-*()`, whose grammar is closed. One holding a selector list
14833-
// (`:not()`, `:is()`, `:has()`) would have to have its argument checked too.
14834-
const _PSEUDO = String.raw`::?[-\w]+(?:\(\s*(?:[-+\dn ]+|[oO][dD][dD]|[eE][vV][eE][nN])\s*\))?`;
14835-
const _COMPOUND = `(?:\\*|${_IDENT}|[.#]${_IDENT}|${_ATTRIBUTE})(?:[.#]${_IDENT}|${_ATTRIBUTE}|${_PSEUDO})*`;
14852+
const _LEGACY_ELEMENT_NAMES = [...LEGACY_PSEUDO_ELEMENTS].join("|");
14853+
// A pseudo-class with no selector inside it: argument-less, or the `An+B` of an
14854+
// `:nth-*()`, whose grammar is closed — the one argument a shape may carry.
14855+
const _PSEUDO_CLASS = String.raw`:(?!(?:${_LEGACY_ELEMENT_NAMES})(?![-\w]))(?:(?:${[
14856+
...NTH_PSEUDO_FUNCTIONS
14857+
].join("|")})\(\s*(?:[-+\dn ]+|odd|even)\s*\)|[-\w]+(?![-\w(]))`;
14858+
// A pseudo-element ends the selector it is in: anything after it is no shape
14859+
// an engine parses.
14860+
const _PSEUDO_ELEMENT = String.raw`(?:::[-\w]+|:(?:${_LEGACY_ELEMENT_NAMES}))(?![-\w(])`;
14861+
const _COMPOUND = `(?:\\*|${_IDENT}|[.#]${_IDENT}|${_ATTRIBUTE})(?:[.#]${_IDENT}|${_ATTRIBUTE}|${_PSEUDO_CLASS})*`;
14862+
const _COMBINATOR = "(?:\\s*[>+~]\\s*|\\s+)";
1483614863
const _JOINABLE_SELECTOR_RE = new RegExp(
14837-
`^${_COMPOUND}(?:(?:\\s*[>+~]\\s*|\\s+)${_COMPOUND})*$`
14864+
`^(?:${_COMPOUND}${_COMBINATOR})*${_COMPOUND}(?:${_PSEUDO_ELEMENT})?$`,
14865+
"i"
1483814866
);
1483914867

1484014868
// The same, for a rule nested in another: `&` joins there because an engine that
1484114869
// cannot read it cannot read the block holding it either, so both selectors are
1484214870
// dropped together rather than one taking the other down.
1484314871
const _NESTED_COMPOUND = `(?:&(?:${_COMPOUND})?|${_COMPOUND})`;
1484414872
const _JOINABLE_NESTED_RE = new RegExp(
14845-
`^${_NESTED_COMPOUND}(?:(?:\\s*[>+~]\\s*|\\s+)${_NESTED_COMPOUND})*$`
14873+
`^(?:${_NESTED_COMPOUND}${_COMBINATOR})*${_NESTED_COMPOUND}(?:${_PSEUDO_ELEMENT})?$`,
14874+
"i"
1484614875
);
1484714876

1484814877
// A keyframe selector is a percentage or `to` — `from` is already printed `0%`.
@@ -14922,7 +14951,10 @@ const _pseudosReadable = (prelude) => {
1492214951
end++;
1492314952
}
1492414953
if (end === from) return false;
14925-
const spelling = toLowerCaseIfNeeded(prelude.slice(i, end));
14954+
// A pseudo taking an argument is a different entry from the bare one.
14955+
const spelling = `${toLowerCaseIfNeeded(prelude.slice(i, end))}${
14956+
prelude.charCodeAt(end) === CC_LEFT_PARENTHESIS ? "(" : ""
14957+
}`;
1492614958
let reads = _selectorReadMemo.get(spelling);
1492714959
if (reads === undefined) {
1492814960
// A pseudo-element reads with one colon as well as two, and the table
@@ -14935,7 +14967,11 @@ const _pseudosReadable = (prelude) => {
1493514967
// A pseudo the table does not name is one no engine is known to parse —
1493614968
// a vendor spelling, a CSS-modules one, or simply newer than the data —
1493714969
// and no selection makes it safe, so this precedes the target check.
14938-
reads = since !== undefined && _readsAll(since);
14970+
reads =
14971+
since !== undefined &&
14972+
(_prefixBrowsers === null
14973+
? _readByEveryEngine(since)
14974+
: _readsAll(since));
1493914975
_selectorReadMemo.set(spelling, reads);
1494014976
}
1494114977
if (!reads) return false;
@@ -15806,7 +15842,10 @@ const _nothingBetweenShadows = (items, texts, from, to, declared, subjects) => {
1580615842

1580715843
// The last compound of a selector the joinable shape accepted, once attribute
1580815844
// selectors and pseudos are gone: what either holds may look like a combinator.
15809-
const _SUBJECT_STRIP_RE = new RegExp(`${_ATTRIBUTE}|${_PSEUDO}`, "g");
15845+
const _SUBJECT_STRIP_RE = new RegExp(
15846+
`${_ATTRIBUTE}|${_PSEUDO_CLASS}|${_PSEUDO_ELEMENT}`,
15847+
"gi"
15848+
);
1581015849
const _SUBJECT_TYPE_RE = /(?:^|[\s>+~])([-\w\u00A0-\uFFFF]+)[^\s>+~]*$/;
1581115850

1581215851
/**

‎test/specCases/syntaxEquivalence.spectest.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,33 @@ const LOWERING_FIXTURES = [
16551655
[".c", "border-left-style"]
16561656
]
16571657
},
1658+
{
1659+
name: "a rule beside one whose selector no engine parses",
1660+
// Each odd rule is one an engine drops, so joining it to the rule before
1661+
// would take that rule's color down with it.
1662+
css:
1663+
".a{color:green}.b:first{color:green}.c{color:green}.d:nth-child{color:green}" +
1664+
".e{color:green}.f::part{color:green}.g{color:green}.h:before.i{color:green}" +
1665+
".j{color:green}.k:hover(odd){color:green}.l{color:green}.m:playing{color:green}",
1666+
browsers: ["chrome 130", "firefox 130", "safari 18"],
1667+
produces: [
1668+
".a{color:green}.b:first{",
1669+
".c{color:green}.d:nth-child{",
1670+
".e{color:green}.f::part{",
1671+
".g{color:green}.h:before.i{",
1672+
".j{color:green}.k:hover(odd){",
1673+
".l{color:green}.m:playing{"
1674+
],
1675+
html: '<i class="a"></i><i class="c"></i><i class="e"></i><i class="g"></i><i class="j"></i><i class="l"></i>',
1676+
probes: [
1677+
[".a", "color"],
1678+
[".c", "color"],
1679+
[".e", "color"],
1680+
[".g", "color"],
1681+
[".j", "color"],
1682+
[".l", "color"]
1683+
]
1684+
},
16581685
{
16591686
name: "a layer block written where its layer is first named",
16601687
css:

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6062,6 +6062,14 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
60626062
// The `s` modifier is one an engine may not read, and one selector it
60636063
// drops invalidates the whole list it was joined into.
60646064
["a matcher carries a modifier past `i`", "[a=b s]{top:0}[c]{top:0}"],
6065+
// Each of these is a selector no engine parses, which a join would take
6066+
// the valid one down with.
6067+
["a pseudo-class belongs to `@page`", "a{top:0}b:first{top:0}"],
6068+
["a pseudo takes no argument", "a{top:0}b:hover(odd){top:0}"],
6069+
["a pseudo goes without the argument it needs", "a{top:0}b:nth-child{top:0}"],
6070+
["...as does a pseudo-element", "a{top:0}b::part{top:0}"],
6071+
["a pseudo-element is followed in its compound", "a{top:0}b:before.c{top:0}"],
6072+
["...or by another compound", "a{top:0}b:before c{top:0}"],
60656073
// CSS Cascade 5 §6.4.1: every `@layer {` opens a layer of its own, and the
60666074
// later one wins whatever the selectors say.
60676075
[
@@ -6083,6 +6091,10 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
60836091
expect(minifyFor(css, ["chrome 130", "safari 15.4"])).toBe(joined);
60846092
// No selection names no browser to answer for, so it is assumed.
60856093
expect(minify(css)).toBe(joined);
6094+
// ...but only where every engine reads it in some release: one no
6095+
// Chromium reads yet would take the other selector down there.
6096+
const unshipped = "a:playing{top:0}b:playing{top:0}";
6097+
expect(minify(unshipped)).toBe(unshipped);
60866098
// A pseudo nothing states support for never joins, at any target.
60876099
const unknown = "a:totally-made-up{top:0}b:totally-made-up{top:0}";
60886100
expect(minifyFor(unknown, ["chrome 130"])).toBe(unknown);

‎tooling/generate-css-data.js‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5936,27 +5936,30 @@ const collectSupportedFrom = (paths) => {
59365936
};
59375937

59385938
/**
5939-
* When each browser first read every standard pseudo-class and pseudo-element,
5940-
* keyed by the spelling a selector carries (`:hover`, `::before`, `:nth-child`).
5941-
* Only a pseudo every target reads may be joined into a selector list, because
5942-
* one selector an engine cannot parse invalidates the whole list.
5939+
* When each browser first read every standard pseudo-class and pseudo-element a
5940+
* style rule's selector may name, keyed by the shape it is written in: `:hover`
5941+
* bare, `:nth-child(` with its argument. Only a pseudo every target reads may be
5942+
* joined into a selector list, because one selector an engine cannot parse
5943+
* invalidates the whole list.
59435944
* @returns {[string, [string, number][]][]} the versions, by spelling
59445945
*/
59455946
const collectSelectorSupport = () => {
59465947
/** @type {[string, [string, number][]][]} */
59475948
const table = [];
5948-
// `::cue` and `::cue()` are two entries naming one pseudo, and the spelling
5949-
// is what the print looks up — so the second would shadow the first in the
5950-
// `Map` this becomes rather than say anything new.
59515949
/** @type {Set<string>} */
59525950
const spellings = new Set();
59535951
for (const entry of Object.values(selectors)) {
59545952
if (entry.status !== "standard") continue;
5955-
const match = /^(::?)([-\w]+)/.exec(entry.syntax || "");
5953+
// `:first`, `:left`, `:right` and `:blank` select pages, so a style rule
5954+
// naming one is a selector no engine parses.
5955+
if (entry.groups.includes("CSS Paged Media")) continue;
5956+
const match = /^(::?)([-\w]+)(\()?/.exec(entry.syntax || "");
59565957
if (match === null) continue;
5957-
const [, colons, name] = match;
5958+
const [, colons, name, call] = match;
59585959
if (!bcd.css.selectors[name]) continue;
5959-
const spelling = `${colons}${name}`;
5960+
// `::cue` and `::cue()` are two pseudos to a parser, each valid only in
5961+
// its own shape.
5962+
const spelling = `${colons}${name}${call || ""}`;
59605963
if (spellings.has(spelling)) continue;
59615964
spellings.add(spelling);
59625965
table.push([spelling, collectSupportedFrom([`css.selectors.${name}`])]);

0 commit comments

Comments
 (0)