Skip to content

Commit 2d336ba

Browse files
fix(css): keep a lone border none, and a gradient fallback for a unit a target cannot read (#22567)
A lone `none` sets a border's width to `medium`, which a later rule's style shows as 3px, so writing it as `0` hid that border. A gradient's support was read by its function alone, so a fallback before one using `dvh` was dropped for Safari 15; each unit is now checked against browser-compat-data.
1 parent 8ec8084 commit 2d336ba

10 files changed

Lines changed: 141 additions & 48 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 CSS `border: none` and gradient fallbacks for units a target cannot read.

‎cspell.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
"contenturl",
8888
"contextifies",
8989
"contextify",
90+
"cqmax",
91+
"cqmin",
9092
"crispedges",
9193
"crossorigin",
9294
"cspellcache",
@@ -128,6 +130,8 @@
128130
"dpcm",
129131
"dppx",
130132
"duringmedia",
133+
"dvmax",
134+
"dvmin",
131135
"embedurl",
132136
"Emscripten",
133137
"endregion",
@@ -250,6 +254,8 @@
250254
"longtest",
251255
"lowlink",
252256
"Lucida",
257+
"lvmax",
258+
"lvmin",
253259
"mangleable",
254260
"materializecss",
255261
"MATHML",
@@ -384,6 +390,7 @@
384390
"RAWTEXT",
385391
"rawtext",
386392
"RBDT",
393+
"rcap",
387394
"RCDATA",
388395
"readonly",
389396
"realises",
@@ -478,6 +485,8 @@
478485
"subpath",
479486
"subresource",
480487
"substack",
488+
"svmax",
489+
"svmin",
481490
"symlinked",
482491
"syncloader",
483492
"systemjs",

‎lib/css/data.js‎

Lines changed: 27 additions & 2 deletions
Large diffs are not rendered by default.

‎lib/css/syntax-parser.js‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13718,8 +13718,8 @@ const _GRADIENT_KEYWORDS = new Set([
1371813718
"transparent"
1371913719
]);
1372013720

13721-
// A number, a length, a percentage or an angle.
13722-
const _GRADIENT_AMOUNT_RE = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:[a-z]+|%)?$/i;
13721+
// A number, a length, a percentage or an angle, its unit captured.
13722+
const _GRADIENT_AMOUNT_RE = /^[+-]?(?:\d+\.?\d*|\.\d+)([a-z]+|%)?$/i;
1372313723

1372413724
/**
1372513725
* Whether a target reading the gradient function reads all of its arguments: a
@@ -13732,11 +13732,14 @@ const _gradientArgumentsRead = (inner) => {
1373213732
for (const argument of _splitTopLevelArguments(inner)) {
1373313733
for (const component of _splitPrintedComponents(argument.trim())) {
1373413734
const lowered = toLowerCaseIfNeeded(component);
13735-
if (
13736-
_GRADIENT_KEYWORDS.has(lowered) ||
13737-
_GRADIENT_AMOUNT_RE.test(component)
13738-
) {
13739-
continue;
13735+
if (_GRADIENT_KEYWORDS.has(lowered)) continue;
13736+
const amount = _GRADIENT_AMOUNT_RE.exec(lowered);
13737+
if (amount !== null) {
13738+
// A unit some target does not read drops the gradient there.
13739+
const unit = amount[1];
13740+
if (unit === undefined || unit === "%") continue;
13741+
if (_readsAll(_valueSupportedFrom(`unit ${unit}`))) continue;
13742+
return false;
1374013743
}
1374113744
if (_LONE_HEX_RE.test(component)) {
1374213745
if (component.length === 4 || component.length === 7) continue;

‎lib/css/syntax-printer.js‎

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ const {
101101
getKeywordOnlyProperties,
102102
getPrefixedProperties,
103103
getPrefixedSpellingKeywords,
104-
getShorthandInitialKeywords,
105-
getShorthandLonghands
104+
getShorthandInitialKeywords
106105
} = require("./data");
107106
const {
108107
A,
@@ -4169,22 +4168,6 @@ const _standardSpelling = (property) => {
41694168
return standard === undefined ? property : standard;
41704169
};
41714170

4172-
/**
4173-
* Whether a shorthand sets a style and the width it zeroes when `none`: a border
4174-
* or a border side. An outline or column rule computes its `medium` width as 3px.
4175-
* @param {string} property the shorthand's lowercased name
4176-
* @returns {boolean} true when its `none` and `0` compute alike
4177-
*/
4178-
const _styleZeroesWidth = (property) => {
4179-
if (!property.startsWith("border")) return false;
4180-
const longhands = getShorthandLonghands().get(property);
4181-
return (
4182-
longhands !== undefined &&
4183-
longhands.has(`${property}-style`) &&
4184-
longhands.has(`${property}-width`)
4185-
);
4186-
};
4187-
41884171
/**
41894172
* Shorten a shorthand declaration's value to an equivalent spelling. A prefixed
41904173
* spelling is a different property, and neither table lists one.
@@ -4254,15 +4237,6 @@ const _collapseShorthand = (path, property, node, writer) => {
42544237
dropped = kept;
42554238
}
42564239
}
4257-
// A `none` style computes the width it pairs with to `0`, so the lone `none`
4258-
// sets every longhand to what a lone `0` does.
4259-
if (
4260-
components.length === 1 &&
4261-
equalsLowerCase(components[0], "none") &&
4262-
_styleZeroesWidth(property)
4263-
) {
4264-
return ["0"];
4265-
}
42664240
if (property === "display" && components.length === 2) {
42674241
const pair = `${toLowerCaseIfNeeded(components[0])} ${toLowerCaseIfNeeded(
42684242
components[1]

‎test/configCases/css/minimize-corpus/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ Array [
16371637
"/*! webpack: a zero time keeps a unit, and \`s\` is the shorter one it can carry */.t98{transition-duration:0s}",
16381638
"/*! webpack: a shorthand slot holding its own initial goes, and a slot taking a
16391639
length or a color is read by the classes a sibling's own value is in — every
1640-
slot holding its initial leaves the shortest of them saying all of them */.t98a{border:0}.t98b{column-rule:none}.t98c{border:solid red}",
1640+
slot holding its initial leaves the shortest of them saying all of them */.t98a{border:none}.t98b{column-rule:none}.t98c{border:solid red}",
16411641
"/*! declined: a sibling filling the slot keeps the initial there */.t98d{border:medium 1px}.t98e{border:currentcolor red}",
16421642
"/*! webpack: a component spelling the property's own initial says nothing beside
16431643
another — omitting its group leaves exactly that keyword */.t99{grid-auto-flow:dense}",

‎test/configCases/css/minimize-corpus/__snapshots__/ConfigTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ Array [
16371637
"/*! webpack: a zero time keeps a unit, and \`s\` is the shorter one it can carry */.t98{transition-duration:0s}",
16381638
"/*! webpack: a shorthand slot holding its own initial goes, and a slot taking a
16391639
length or a color is read by the classes a sibling's own value is in — every
1640-
slot holding its initial leaves the shortest of them saying all of them */.t98a{border:0}.t98b{column-rule:none}.t98c{border:solid red}",
1640+
slot holding its initial leaves the shortest of them saying all of them */.t98a{border:none}.t98b{column-rule:none}.t98c{border:solid red}",
16411641
"/*! declined: a sibling filling the slot keeps the initial there */.t98d{border:medium 1px}.t98e{border:currentcolor red}",
16421642
"/*! webpack: a component spelling the property's own initial says nothing beside
16431643
another — omitting its group leaves exactly that keyword */.t99{grid-auto-flow:dense}",

‎test/specCases/syntaxEquivalence.spectest.js‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,19 @@ const LOWERING_FIXTURES = [
16391639
["#de", "color"]
16401640
]
16411641
},
1642+
{
1643+
name: "a lone border none a later rule gives a style",
1644+
css: ".a{border:none}.a.b{border-style:solid}.c{border-left:none}.c.d{border-left-style:solid}",
1645+
browsers: ["chrome 130"],
1646+
produces: [".a{border:none}", ".c{border-left:none}"],
1647+
html: '<div class="a b"></div><div class="c d"></div>',
1648+
probes: [
1649+
[".a", "border-top-width"],
1650+
[".a", "border-top-style"],
1651+
[".c", "border-left-width"],
1652+
[".c", "border-left-style"]
1653+
]
1654+
},
16421655
{
16431656
name: "a slot holding the value an unwritten one takes",
16441657
css:
@@ -1648,7 +1661,7 @@ const LOWERING_FIXTURES = [
16481661
"#c{background:left top url(data:image/gif;base64,R0lGODlhAQABAAAAACw=)}" +
16491662
"#d{mask:url(data:image/gif;base64,R0lGODlhAQABAAAAACw=) border-box}",
16501663
browsers: ["chrome 130"],
1651-
produces: ["background:red", "border-left:0", "column-rule:red"],
1664+
produces: ["background:red", "border-left:none", "column-rule:red"],
16521665
html: "<button id=b>x</button><div id=c></div><div id=d></div>",
16531666
probes: [
16541667
["#b", "background-position"],
@@ -1728,7 +1741,7 @@ const LOWERING_FIXTURES = [
17281741
produces: [
17291742
"@media (width>=1px)",
17301743
"@supports (color:red){",
1731-
"border:0",
1744+
"border:none",
17321745
"transition:opacity.3s",
17331746
"color:#333",
17341747
"#c{color:blue}"

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4785,8 +4785,8 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
47854785
// A slot taking a length or a color is read the same way, by the classes
47864786
// a sibling's own value is in: neither `solid` nor `red` is a width.
47874787
["a{border:medium solid red}", "a{border:solid red}"],
4788-
["a{border:medium none currentcolor}", "a{border:0}"],
4789-
["a{border:none medium}", "a{border:0}"],
4788+
["a{border:medium none currentcolor}", "a{border:none}"],
4789+
["a{border:none medium}", "a{border:none}"],
47904790
["a{column-rule:medium none currentcolor}", "a{column-rule:none}"],
47914791
// ...and a sibling that is one keeps it.
47924792
["a{border:medium 1px}", "a{border:medium 1px}"],
@@ -12211,7 +12211,7 @@ describe("CssSyntax minify — a slot holding its own initial", () => {
1221112211
// The slots are read by what each takes, so a width and a color are
1221212212
// answered for as well as a keyword.
1221312213
expect(minify("a{border-left:currentcolor medium none}")).toBe(
12214-
"a{border-left:0}"
12214+
"a{border-left:none}"
1221512215
);
1221612216
expect(minify("a{column-rule:medium none red}")).toBe("a{column-rule:red}");
1221712217
expect(minify("a{outline:medium none currentcolor}")).toBe(
@@ -14690,6 +14690,27 @@ describe("CssSyntax minify — a fallback the target reads past", () => {
1469014690
);
1469114691
});
1469214692

14693+
it.each([
14694+
// Safari read `dvh` from 15.4, so 15 still needs the `vh` gradient.
14695+
["safari 15", true],
14696+
["safari 16", false]
14697+
])("keeps a gradient fallback where %s reads no unit the later one names", (browser, kept) => {
14698+
const css =
14699+
"a{background-image:linear-gradient(red 10vh,blue);background-image:linear-gradient(red 10dvh,blue)}";
14700+
expect(minifyFor(css, [browser])).toBe(
14701+
kept ? css : "a{background-image:linear-gradient(red 10dvh,blue)}"
14702+
);
14703+
});
14704+
14705+
it("reads no unit the tables do not name", () => {
14706+
const css = "a{background:red;background:linear-gradient(red 1foo,blue)}";
14707+
expect(minifyFor(css, MODERN)).toBe(css);
14708+
// An absolute length is read wherever the gradient is.
14709+
expect(
14710+
minifyFor("a{background:red;background:linear-gradient(red 1px,blue 2em)}", MODERN)
14711+
).toBe("a{background:linear-gradient(red 1px,blue 2em)}");
14712+
});
14713+
1469314714
it("reads a gradient of keywords, amounts and colors every engine names", () => {
1469414715
expect(
1469514716
minifyFor("a{background:red;background:linear-gradient(red,blue)}", MODERN)
@@ -15212,11 +15233,12 @@ describe("CssSyntax minify — what a comparison with other minifiers found", ()
1521215233
});
1521315234

1521415235
it.each([
15215-
["a{border:none}", "a{border:0}"],
15216-
["a{border:NONE!important}", "a{border:0!important}"],
15217-
["a{border-top:none}", "a{border-top:0}"],
15218-
["a{border-block-end:none}", "a{border-block-end:0}"],
15219-
// `medium` computes as 3px for these whatever the style, so `0` differs.
15236+
// `none` sets the width to `medium`, which a later style shows as 3px.
15237+
["a{border:none}", "a{border:none}"],
15238+
["a{border:NONE!important}", "a{border:none!important}"],
15239+
["a{border-top:none}", "a{border-top:none}"],
15240+
["a{border-block-end:none}", "a{border-block-end:none}"],
15241+
[".a{border:none}.a.b{border-style:solid}", ".a{border:none}.a.b{border-style:solid}"],
1522015242
["a{outline:none}", "a{outline:none}"],
1522115243
["a{column-rule:none}", "a{column-rule:none}"],
1522215244
// No width beside the style here, so `none` stays the one word.

‎tooling/generate-css-data.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5694,6 +5694,47 @@ const collectSelectorSupport = () => {
56945694
return table.sort((a, b) => (a[0] < b[0] ? -1 : 1));
56955695
};
56965696

5697+
/**
5698+
* When each browser first read each length and angle unit, keyed `"unit <name>"`.
5699+
* A row grouping several units names them in its description; an absolute
5700+
* length has no row of its own, so the `<length>` type's row answers for it.
5701+
* @returns {[string, [string, number][]][]} the versions, by unit
5702+
*/
5703+
const collectUnitSupport = () => {
5704+
/** @type {Map<string, [string, number][]>} */
5705+
const out = new Map();
5706+
for (const type of ["length", "angle"]) {
5707+
const node = /** @type {EXPECTED_ANY} */ (bcd.css.types)[type];
5708+
for (const name of Object.keys(node)) {
5709+
if (name.startsWith("__")) continue;
5710+
/** @type {string[]} */
5711+
const units = [];
5712+
if (/^[a-z]+$/i.test(name)) {
5713+
units.push(name);
5714+
} else {
5715+
const listed = /<code>([a-z]+)<\/code>/gi;
5716+
const description = String(node[name].__compat.description || "");
5717+
let match;
5718+
while ((match = listed.exec(description)) !== null) {
5719+
units.push(match[1]);
5720+
}
5721+
}
5722+
if (units.length === 0) {
5723+
throw new Error(`css.types.${type}.${name} names no unit: bcd moved`);
5724+
}
5725+
const versions = collectSupportedFrom([`css.types.${type}.${name}`]);
5726+
for (const unit of units) out.set(`unit ${unit.toLowerCase()}`, versions);
5727+
}
5728+
}
5729+
const base = collectSupportedFrom(["css.types.length"]);
5730+
for (const [unit, group] of SUPPLEMENT.absoluteUnitScale) {
5731+
if (group === "length" && !out.has(`unit ${unit}`)) {
5732+
out.set(`unit ${unit}`, base);
5733+
}
5734+
}
5735+
return [...out].sort((a, b) => (a[0] < b[0] ? -1 : 1));
5736+
};
5737+
56975738
/**
56985739
* When each browser first read each gradient function, by its name.
56995740
* @returns {[string, [string, number][]][]} the versions, by function name
@@ -7281,6 +7322,7 @@ const collectData = async () => {
72817322
const valueSupport = [
72827323
...collectValueSupport(colorValueFunctions),
72837324
...collectGradientSupport(),
7325+
...collectUnitSupport(),
72847326
// Where a substitution function is read, any declaration holding it parses.
72857327
// `attr()` is left out: its row is the `content` reading, not this one.
72867328
...[...substitutionFunctions]

0 commit comments

Comments
 (0)