Skip to content

Commit e6f8598

Browse files
fix(css): keep a fallback when its overriding value is read by no engine
A later declaration only overrides an earlier one where the targets read it. The gradient check accepted any keyword anywhere, so an invalid `radial-gradient(red at center, blue)` dropped a valid fallback under a browserslist target, and `dropOverriddenDeclarations` without one assumed every later value is read. The browser equivalence suite now also runs the corpora with that option and with sorted attributes and token lists.
1 parent 8a5c37e commit e6f8598

12 files changed

Lines changed: 151 additions & 30 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 fallback when the declaration overriding it is invalid or read by no shipping engine, with a browserslist target or `dropOverriddenDeclarations`.

‎declarations/WebpackOptions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3172,7 +3172,7 @@ export interface OptimizationMinimizeCss {
31723172
*/
31733173
customSelectors?: boolean;
31743174
/**
3175-
* Drop a declaration a later one in the same block overrides even where nothing states that the target can read the later value. Off by default. With a `browserslist` target this is already done wherever every browser it names is known to read the later value, so the option only widens the case where no target is selected and there are no engines to name — which is what csso and cssnano do unconditionally. What it gives up is the fallback pair: `color:#c65d06;color:lab(50% 100 -100)` loses the hex, so an engine that cannot read `lab()` is left with nothing. A selection naming a browser the compat tables do not cover is still answered for the whole of it rather than by this option, since naming a browser states a target the option does not override.
3175+
* Drop a declaration a later one in the same block overrides even where no target is selected, assuming the engines shipping today: wherever Chromium, Gecko and WebKit each read the later value. Off by default. With a `browserslist` target this is already done wherever every browser it names is known to read the later value, so the option only widens the case where no target is selected and there are no engines to name — which is what csso and cssnano do unconditionally. What it gives up is the fallback pair: `color:#c65d06;color:lab(50% 100 -100)` loses the hex, so an engine that cannot read `lab()` is left with nothing. A selection naming a browser the compat tables do not cover is still answered for the whole of it rather than by this option, since naming a browser states a target the option does not override.
31763176
* @since 5.112.0
31773177
*/
31783178
dropOverriddenDeclarations?: boolean;

‎lib/css/syntax-parser.js‎

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4450,7 +4450,7 @@ const TOP_LEVEL_CONSUMERS = {
44504450
* @property {boolean=} convertLengthUnits rewrite a length into a shorter unit it is exactly equal in (`16px` -> `1pc`); off by default because it earns nothing once the asset is compressed, and only read while printing. A time is always rewritten
44514451
* @property {boolean=} convertApproximateColors write a polar or Lab color as the nearest hex even where that hex only approximates it: a channel too near a `.5` boundary for two engines to round it alike, and a color outside the sRGB gamut, which hex can only clip. Off by default, and only read while printing; a space engines read through transfers of their own (`a98-rgb`, `prophoto-rgb`) is left alone either way
44524452
* @property {boolean=} mergeDistantRules give a rule the selectors of a later one printing the same block, and an at-rule the block of a later one stating the same condition, past the rules between them; off by default, since it reorders the cascade and is sound only where nothing between declares what the block being moved does for an element it can match — a condition between counting for what its own rules declare
4453-
* @property {boolean=} dropOverriddenDeclarations drop a declaration a later one in the same block overrides even where nothing states that the target can read the later value; off by default, and only read while printing. With a `browserslist` selection the drop is already made wherever every browser it names is known to read the later value, so this widens the case where no target is selected — a selection naming a browser the tables do not cover still answers for the whole of it
4453+
* @property {boolean=} dropOverriddenDeclarations drop a declaration a later one in the same block overrides even where no target is selected, wherever Chromium, Gecko and WebKit each read the later value; off by default, and only read while printing. With a `browserslist` selection the drop is already made wherever every browser it names is known to read the later value, so this widens the case where no target is selected — a selection naming a browser the tables do not cover still answers for the whole of it
44544454
* @property {boolean=} rewriteCustomProperties shorten a custom property's value the way any other value is shortened (`--x:#ffffff` -> `#fff`); off by default because `getPropertyValue()` hands that text back, and only read while printing. What it may rewrite is what any other value's tokens may be, a color in a substitution's fallback included — that being the property's value rather than the function's own argument
44554455
* @property {EmbeddedSourceRenderer=} renderEmbeddedSource renders source this stylesheet embeds: the payload of a `url()` `data:` URL whose media type names a language webpack knows (SVG, CSS, HTML, JSON, JavaScript). Absent, a data URL is emitted exactly as written
44564456
* @property {{ [name: string]: string }=} pseudoClasses each pseudo-class to write as a class instead (`{ "focus-visible": "focus-visible" }`), so a script can apply it where the engine does not. Only read while printing
@@ -9675,6 +9675,17 @@ const _readByEveryEngine = (profile) => {
96759675
return true;
96769676
};
96779677

9678+
/**
9679+
* Whether the targets read an ability: the selection's browsers, or with none
9680+
* each engine shipping today, which is what `dropOverriddenDeclarations` assumes.
9681+
* @param {number | undefined} profile its `getSupportProfiles()` row
9682+
* @returns {boolean} true when the later value it names is read
9683+
*/
9684+
const _readsOverridingValue = (profile) =>
9685+
_prefixBrowsers === null
9686+
? profile !== undefined && _readByEveryEngine(profile)
9687+
: _readsAll(profile);
9688+
96789689
/**
96799690
* The vendor spellings at least one target browser still needs for a construct —
96809691
* a target at version V needs spelling S when some `[browser, from, to]` of S has
@@ -14594,9 +14605,9 @@ const _familyValue = (property, longhands, values) => {
1459414605
* @returns {boolean} true when no target is left reading the earlier one
1459514606
*/
1459614607
const _valueReadByTarget = (property, value) => {
14597-
// No selection names no browser to answer for, so nothing is known to read the
14598-
// later value: the earlier one stands unless asked to assume otherwise.
14599-
if (_prefixBrowsers === null) return _dropOverriddenDeclarations;
14608+
// No selection names no browser to answer for, so the earlier one stands
14609+
// unless the option assumes each engine shipping today, read against below.
14610+
if (_prefixBrowsers === null && !_dropOverriddenDeclarations) return false;
1460014611
// A selection naming a browser the tables do not cover answers for less than
1460114612
// the whole of it. Naming a browser states a target, which the option that
1460214613
// assumes one does not override, so this stands whatever it says.
@@ -14606,7 +14617,7 @@ const _valueReadByTarget = (property, value) => {
1460614617
const substitutions = _heldSubstitutions(value);
1460714618
if (substitutions !== null) {
1460814619
for (const name of substitutions) {
14609-
if (!_readsAll(_valueSupportedFrom(name))) return false;
14620+
if (!_readsOverridingValue(_valueSupportedFrom(name))) return false;
1461014621
}
1461114622
return true;
1461214623
}
@@ -14616,7 +14627,7 @@ const _valueReadByTarget = (property, value) => {
1461614627
const components = _splitPrintedComponents(value);
1461714628
if (components.length > 1) return _textDecorationRead(components);
1461814629
}
14619-
return _readsAll(
14630+
return _readsOverridingValue(
1462014631
_valueSupportedFrom(`${property} ${toLowerCaseIfNeeded(value)}`)
1462114632
);
1462214633
}
@@ -14628,7 +14639,7 @@ const _valueReadByTarget = (property, value) => {
1462814639
return false;
1462914640
}
1463014641
const fn = toLowerCaseIfNeeded(value.slice(0, open));
14631-
if (!_readsAll(_valueSupportedFrom(fn))) return false;
14642+
if (!_readsOverridingValue(_valueSupportedFrom(fn))) return false;
1463214643
if (fn.endsWith("gradient")) {
1463314644
return _gradientArgumentsRead(value.slice(open + 1, -1));
1463414645
}
@@ -14687,27 +14698,51 @@ const _GRADIENT_AMOUNT_RE = /^[+-]?(?:\d+\.?\d*|\.\d+)([a-z]+|%)?$/i;
1468714698
* @returns {boolean} true when the target reads every one
1468814699
*/
1468914700
const _gradientArgumentsRead = (inner) => {
14690-
for (const argument of _splitTopLevelArguments(inner)) {
14691-
for (const component of _splitPrintedComponents(argument.trim())) {
14701+
const args = _splitTopLevelArguments(inner);
14702+
for (let index = 0; index < args.length; index++) {
14703+
// A line, shape or position opens the list alone; a stop is one color
14704+
// and its amounts. Anything else makes the whole gradient invalid.
14705+
let words = 0;
14706+
let colors = 0;
14707+
for (const component of _splitPrintedComponents(args[index].trim())) {
1469214708
const lowered = toLowerCaseIfNeeded(component);
14693-
if (_GRADIENT_KEYWORDS.has(lowered)) continue;
14709+
if (_GRADIENT_KEYWORDS.has(lowered)) {
14710+
if (lowered === "transparent" || lowered === "currentcolor") {
14711+
colors++;
14712+
} else {
14713+
words++;
14714+
}
14715+
if (words !== 0 && (index !== 0 || colors !== 0)) return false;
14716+
if (colors > 1) return false;
14717+
continue;
14718+
}
1469414719
const amount = _GRADIENT_AMOUNT_RE.exec(lowered);
1469514720
if (amount !== null) {
1469614721
// A unit some target does not read drops the gradient there.
1469714722
const unit = amount[1];
1469814723
if (unit === undefined || unit === "%") continue;
14699-
if (_readsAll(_valueSupportedFrom(`unit ${unit}`))) continue;
14700-
return false;
14724+
if (!_readsOverridingValue(_valueSupportedFrom(`unit ${unit}`))) {
14725+
return false;
14726+
}
14727+
continue;
1470114728
}
14729+
let color = false;
1470214730
if (_LONE_HEX_RE.test(component)) {
14703-
if (component.length === 4 || component.length === 7) continue;
14704-
if (_S._hexAlphaAllowed) continue;
14705-
return false;
14706-
}
14707-
if (getColorNameToRgb().has(lowered) && !LATER_COLOR_NAMES.has(lowered)) {
14708-
continue;
14731+
if (
14732+
component.length !== 4 &&
14733+
component.length !== 7 &&
14734+
!_S._hexAlphaAllowed
14735+
) {
14736+
return false;
14737+
}
14738+
color = true;
14739+
} else if (
14740+
getColorNameToRgb().has(lowered) &&
14741+
!LATER_COLOR_NAMES.has(lowered)
14742+
) {
14743+
color = true;
1470914744
}
14710-
return false;
14745+
if (!color || ++colors > 1 || words !== 0) return false;
1471114746
}
1471214747
}
1471314748
return true;

‎schemas/WebpackOptions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4125,7 +4125,7 @@
41254125
"added": "5.112.0"
41264126
},
41274127
"dropOverriddenDeclarations": {
4128-
"description": "Drop a declaration a later one in the same block overrides even where nothing states that the target can read the later value. Off by default. With a `browserslist` target this is already done wherever every browser it names is known to read the later value, so the option only widens the case where no target is selected and there are no engines to name — which is what csso and cssnano do unconditionally. What it gives up is the fallback pair: `color:#c65d06;color:lab(50% 100 -100)` loses the hex, so an engine that cannot read `lab()` is left with nothing. A selection naming a browser the compat tables do not cover is still answered for the whole of it rather than by this option, since naming a browser states a target the option does not override.",
4128+
"description": "Drop a declaration a later one in the same block overrides even where no target is selected, assuming the engines shipping today: wherever Chromium, Gecko and WebKit each read the later value. Off by default. With a `browserslist` target this is already done wherever every browser it names is known to read the later value, so the option only widens the case where no target is selected and there are no engines to name — which is what csso and cssnano do unconditionally. What it gives up is the fallback pair: `color:#c65d06;color:lab(50% 100 -100)` loses the hex, so an engine that cannot read `lab()` is left with nothing. A selection naming a browser the compat tables do not cover is still answered for the whole of it rather than by this option, since naming a browser states a target the option does not override.",
41294129
"type": "boolean",
41304130
"added": "5.112.0"
41314131
},

‎test/configCases/css/minimize-dead-fallbacks/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ Array [
2424
one of the unknown ones */.t12{color:red;color:color-mix(in oklch,red,blue)}",
2525
"/*! declined: a transition list keeps its dead prefixed entry, since dropping
2626
it shortens every list \`getComputedStyle\` reads off the shorthand */.t13{transition:transform.15s linear,-webkit-transform.15s linear}",
27+
"/*! declined: \`at\` cannot follow a color stop, so the later gradient is invalid
28+
and the earlier one is what every engine reads */.t14{background-image:linear-gradient(red,blue);background-image:radial-gradient(red at center,blue)}",
2729
]
2830
`;

‎test/configCases/css/minimize-dead-fallbacks/__snapshots__/ConfigTest.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ Array [
2424
one of the unknown ones */.t12{color:red;color:color-mix(in oklch,red,blue)}",
2525
"/*! declined: a transition list keeps its dead prefixed entry, since dropping
2626
it shortens every list \`getComputedStyle\` reads off the shorthand */.t13{transition:transform.15s linear,-webkit-transform.15s linear}",
27+
"/*! declined: \`at\` cannot follow a color stop, so the later gradient is invalid
28+
and the earlier one is what every engine reads */.t14{background-image:linear-gradient(red,blue);background-image:radial-gradient(red at center,blue)}",
2729
]
2830
`;

‎test/configCases/css/minimize-dead-fallbacks/style.css‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@
4040
/*! declined: a transition list keeps its dead prefixed entry, since dropping
4141
it shortens every list `getComputedStyle` reads off the shorthand */
4242
.t13 { transition: transform .15s linear, -webkit-transform .15s linear }
43+
/*! declined: `at` cannot follow a color stop, so the later gradient is invalid
44+
and the earlier one is what every engine reads */
45+
.t14 { background-image: linear-gradient(red, blue); background-image: radial-gradient(red at center, blue) }

‎test/configCases/css/minimize-drop-overridden-declarations/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ Array [
1212
engine and the earlier goes whatever this option says */.t12{--x:lab(50% 100 -100)}",
1313
"/*! a rule between them parts the two, so neither overrides the other */.t13{color:red;& .inner{color:lime}color:blue}",
1414
"/*! a keyframe ignores \`!important\`, so one there stands over nothing */@keyframes t14{0%{color:red!important;color:blue}}",
15+
"/*! no shipping engine reads a later value that is invalid or names what none
16+
of them has, so the earlier one is what they all use and stays */.t15{background-image:linear-gradient(red,blue);background-image:radial-gradient(red at center,blue)}.t16{color:red;color:device-cmyk(0 1 1 0)}",
1517
]
1618
`;

‎test/configCases/css/minimize-drop-overridden-declarations/__snapshots__/ConfigTest.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ Array [
1212
engine and the earlier goes whatever this option says */.t12{--x:lab(50% 100 -100)}",
1313
"/*! a rule between them parts the two, so neither overrides the other */.t13{color:red;& .inner{color:lime}color:blue}",
1414
"/*! a keyframe ignores \`!important\`, so one there stands over nothing */@keyframes t14{0%{color:red!important;color:blue}}",
15+
"/*! no shipping engine reads a later value that is invalid or names what none
16+
of them has, so the earlier one is what they all use and stays */.t15{background-image:linear-gradient(red,blue);background-image:radial-gradient(red at center,blue)}.t16{color:red;color:device-cmyk(0 1 1 0)}",
1517
]
1618
`;

‎test/configCases/css/minimize-drop-overridden-declarations/style.css‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@
2424
.t13 { color: red; & .inner { color: lime } color: blue }
2525
/*! a keyframe ignores `!important`, so one there stands over nothing */
2626
@keyframes t14 { 0% { color: red !important; color: blue } }
27+
/*! no shipping engine reads a later value that is invalid or names what none
28+
of them has, so the earlier one is what they all use and stays */
29+
.t15 { background-image: linear-gradient(red, blue); background-image: radial-gradient(red at center, blue) }
30+
.t16 { color: red; color: device-cmyk(0 1 1 0) }

0 commit comments

Comments
 (0)