Skip to content

Commit 5ee3f70

Browse files
authored
fix(css): take out the declaration a lowered shorthand writes over (#22270)
* fix(css): take out the declaration a lowered shorthand writes over A shorthand the target cannot read is lowered into the line it keeps plus the longhands beside it, and that line is a value every target reads. The drop read the shorthand by its source spelling, so an earlier declaration of the property looked like the fallback the author wrote and stayed: `text-decoration:underline;text-decoration:underline dotted` kept both where a second pass took one out. Read by what is written instead. * fix(css): ask only the shorthand whose lowering keeps it `_writtenValue` reads the value off the first declaration the lowering wrote, which is the shorthand only where the lowering keeps it. `inset`, `place-*` and the pair shorthands are written as longhands alone, so it would have read a longhand's value under the shorthand's name. Nothing consumed it, the drop declining those for its own reasons, but the answer was wrong. Asked about `text-decoration` alone now.
1 parent 5d886df commit 5ee3f70

9 files changed

Lines changed: 234 additions & 1 deletion

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+
Take out the declaration a lowered shorthand writes over, which its source spelling made look like a fallback.

‎lib/css/syntax-parser.js‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11630,7 +11630,7 @@ const _writeIsDead = (path, writer, earlier, later, keyframe) => {
1163011630
return _laterWriteWins(
1163111631
path,
1163211632
later,
11633-
_printedValue(laterText, laterColon),
11633+
_writtenValue(laterText, laterColon, property),
1163411634
earlier,
1163511635
_printedValue(earlierText, earlierColon),
1163611636
property,
@@ -13629,6 +13629,11 @@ const _withColorFallback = (one) => {
1362913629
return fallback === null ? one : fallback + one;
1363013630
};
1363113631

13632+
// The shorthands whose lowering keeps the shorthand itself, which is the one
13633+
// shape a later value can be read off. `inset`, `place-*` and the pair
13634+
// shorthands are written as longhands alone, so nothing later writes those.
13635+
const LOWERED_SHORTHANDS = new Set(["text-decoration"]);
13636+
1363213637
/**
1363313638
* The longhand declarations a shorthand the target cannot read says, from the
1363413639
* shorthand's printed text — the inverse of the merges `_mergeBoxLonghands`
@@ -13703,6 +13708,34 @@ const _expandUnsupportedShorthand = (text) => {
1370313708
return out;
1370413709
};
1370513710

13711+
/**
13712+
* The value a later declaration is read for, which is the one it is written as.
13713+
* A shorthand the target cannot read is lowered into longhands, and the
13714+
* shorthand it keeps carries the line alone — a value every target reads. So an
13715+
* earlier declaration of that property is not the fallback its spelling makes
13716+
* it look like: what is written over it is readable, and it is dead.
13717+
* @param {string} text the declaration, as printed
13718+
* @param {number} colon where its `:` stands
13719+
* @param {string} property its lowercased property name
13720+
* @returns {string} the value to read it by
13721+
*/
13722+
const _writtenValue = (text, colon, property) => {
13723+
// Decided before the expansion is built: only these are ever written as
13724+
// something else, and every other property pays one lookup.
13725+
if (!LOWERED_SHORTHANDS.has(property) || !_S._transforms.lowerUnsupported) {
13726+
return _printedValue(text, colon);
13727+
}
13728+
const expanded = _expandUnsupportedShorthand(text);
13729+
if (expanded === null) return _printedValue(text, colon);
13730+
// The shorthand stands in front of the longhands written beside it, which is
13731+
// what `LOWERED_SHORTHANDS` names the shorthands this may be asked about by.
13732+
const at = expanded.indexOf(":");
13733+
const end = expanded.indexOf(";", at);
13734+
return at === -1 || end === -1
13735+
? _printedValue(text, colon)
13736+
: expanded.slice(at + 1, end);
13737+
};
13738+
1370613739
/**
1370713740
* Whether a declaration sets a custom property `unusedSymbols` says nothing
1370813741
* reads. Only a declaration: a rule's own name is decided by its prelude.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases css minify-lowered-shorthand-dead-write minify-lowered-shorthand-dead-write should compile 1`] = `".a,.b,.c{text-decoration:underline;text-decoration-style:dotted}.d{& i{top:0}text-decoration:underline;text-decoration-style:dotted}.e{text-decoration:overline!important;text-decoration:underline;text-decoration-style:dotted}.f{text-decoration-line:underline;text-decoration:underline;text-decoration-style:dotted}.g{text-decoration:overline;text-decoration:var(--x)}"`;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases css minify-lowered-shorthand-dead-write minify-lowered-shorthand-dead-write should compile 1`] = `".a,.b,.c{text-decoration:underline;text-decoration-style:dotted}.d{& i{top:0}text-decoration:underline;text-decoration-style:dotted}.e{text-decoration:overline!important;text-decoration:underline;text-decoration-style:dotted}.f{text-decoration-line:underline;text-decoration:underline;text-decoration-style:dotted}.g{text-decoration:overline;text-decoration:var(--x)}"`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./style.css";
2+
3+
it("should take out the declaration a lowered shorthand writes over", () => {
4+
expect(true).toBe(true);
5+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* The shorthand is lowered into the line it keeps plus a longhand beside it,
2+
and the line every target reads — so the declaration in front of it is
3+
written over rather than read as the fallback its spelling suggests. */
4+
.a { text-decoration: underline; text-decoration: underline dotted }
5+
.b { text-decoration: overline; text-decoration: underline dotted }
6+
7+
/* Every earlier one goes, not just the last. */
8+
.c { text-decoration: overline; text-decoration: blink; text-decoration: underline dotted }
9+
10+
/* A rule written between them parts nothing the later one writes over. */
11+
.d { text-decoration: overline; & i { top: 0 } text-decoration: underline dotted }
12+
13+
/* `!important` wins the tie wherever both are read. */
14+
.e { text-decoration: overline !important; text-decoration: underline dotted }
15+
16+
/* A longhand is not the property the shorthand repeats. */
17+
.f { text-decoration-line: underline; text-decoration: underline dotted }
18+
19+
/* A substitution is a value nothing here reads. */
20+
.g { text-decoration: overline; text-decoration: var(--x) }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
const cssMinify = require("../../../../lib/css/cssMinify");
6+
7+
module.exports = {
8+
async afterExecute(options) {
9+
const css = fs.readFileSync(
10+
path.join(options.output.path, "bundle0.css"),
11+
"utf8"
12+
);
13+
expect(css).toMatchSnapshot();
14+
15+
// All three lose the declaration written over, so all three print the one
16+
// block and are written as the one rule they are.
17+
expect(css).toContain(
18+
".a,.b,.c{text-decoration:underline;text-decoration-style:dotted}"
19+
);
20+
expect(css).toContain(
21+
".d{& i{top:0}text-decoration:underline;text-decoration-style:dotted}"
22+
);
23+
expect(css).toContain(".e{text-decoration:overline!important;");
24+
expect(css).toContain(".f{text-decoration-line:underline;");
25+
expect(css).toContain(
26+
".g{text-decoration:overline;text-decoration:var(--x)}"
27+
);
28+
29+
const again = await cssMinify({ "bundle0.css": css }, undefined, {
30+
environment: { browsers: ["safari 15"] }
31+
});
32+
expect(again.code).toBe(css);
33+
}
34+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
// Reads `text-decoration` as CSS 2.1's line alone, so the shorthand is
6+
// written out as the line plus the longhands beside it.
7+
target: "browserslist: safari 15",
8+
mode: "production",
9+
output: {
10+
pathinfo: false
11+
},
12+
optimization: {
13+
minimize: true,
14+
minimizer: ["..."]
15+
},
16+
experiments: {
17+
css: true
18+
}
19+
};

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10828,6 +10828,117 @@ describe("CssSyntax minify — the rules a hoist leaves side by side", () => {
1082810828
});
1082910829
});
1083010830

10831+
describe("CssSyntax minify — the declaration a lowered shorthand kills", () => {
10832+
// Reads `text-decoration` as CSS 2.1's line alone, so the shorthand is
10833+
// written out as the line plus the longhands beside it — and reads those.
10834+
const T = ["safari 15"];
10835+
10836+
it("takes out an earlier declaration the lowered one writes over", () => {
10837+
expect(
10838+
minifyFor("a{text-decoration:underline;text-decoration:underline dotted}", T)
10839+
).toBe("a{text-decoration:underline;text-decoration-style:dotted}");
10840+
});
10841+
10842+
it("takes it out whatever the earlier one said", () => {
10843+
expect(
10844+
minifyFor("a{text-decoration:overline;text-decoration:underline dotted}", T)
10845+
).toBe("a{text-decoration:underline;text-decoration-style:dotted}");
10846+
});
10847+
10848+
it("takes out every earlier one", () => {
10849+
expect(
10850+
minifyFor(
10851+
"a{text-decoration:overline;text-decoration:blink;text-decoration:underline dotted}",
10852+
T
10853+
)
10854+
).toBe("a{text-decoration:underline;text-decoration-style:dotted}");
10855+
});
10856+
10857+
it("takes one out past a rule written between them", () => {
10858+
expect(
10859+
minifyFor(
10860+
"a{text-decoration:overline;& b{top:0}text-decoration:underline dotted}",
10861+
T
10862+
)
10863+
).toBe("a{& b{top:0}text-decoration:underline;text-decoration-style:dotted}");
10864+
});
10865+
10866+
it("keeps the earlier one where the target reads the later whole", () => {
10867+
// Nothing is lowered, so the later value is the one a target may not read
10868+
// — and the earlier one is the fallback the author wrote.
10869+
const css = "a{text-decoration:overline;text-decoration:underline dotted}";
10870+
expect(minifyFor(css, ["chrome 130"])).toBe(css);
10871+
});
10872+
10873+
it("keeps it where the longhands the lowering writes are unread too", () => {
10874+
// The lowered form is no more readable than the shorthand was, so the
10875+
// earlier declaration is still the fallback it was written as.
10876+
expect(
10877+
minifyFor(
10878+
"a{text-decoration:overline;text-decoration:underline dotted}",
10879+
["ie 11"]
10880+
)
10881+
).toBe(
10882+
"a{text-decoration:overline;text-decoration:underline;text-decoration-style:dotted}"
10883+
);
10884+
});
10885+
10886+
it("keeps an `!important` earlier one, which wins the tie", () => {
10887+
expect(
10888+
minifyFor(
10889+
"a{text-decoration:overline!important;text-decoration:underline dotted}",
10890+
T
10891+
)
10892+
).toBe(
10893+
"a{text-decoration:overline!important;text-decoration:underline;text-decoration-style:dotted}"
10894+
);
10895+
});
10896+
10897+
it("keeps an earlier longhand, which the shorthand does not repeat", () => {
10898+
expect(
10899+
minifyFor(
10900+
"a{text-decoration-line:underline;text-decoration:underline dotted}",
10901+
T
10902+
)
10903+
).toBe(
10904+
"a{text-decoration-line:underline;text-decoration:underline;text-decoration-style:dotted}"
10905+
);
10906+
});
10907+
10908+
it("keeps an earlier one where the later is a substitution", () => {
10909+
const css = "a{text-decoration:overline;text-decoration:var(--x)}";
10910+
expect(minifyFor(css, T)).toBe(css);
10911+
});
10912+
10913+
it.each([
10914+
// These lowerings write the longhands alone, so nothing later writes the
10915+
// property at all — what stands in front of it is read as it was written.
10916+
[
10917+
"inset",
10918+
"a{inset:10px;inset:10px 20px 30px 40px}",
10919+
"a{top:10px;right:10px;bottom:10px;left:10px;top:10px;right:20px;bottom:30px;left:40px}"
10920+
],
10921+
[
10922+
"place-items",
10923+
"a{place-items:center;place-items:center start}",
10924+
"a{place-items:center;place-items:center start}"
10925+
],
10926+
[
10927+
"overflow",
10928+
"a{overflow:hidden;overflow:hidden auto}",
10929+
"a{overflow:hidden;overflow:hidden auto}"
10930+
],
10931+
["gap", "a{gap:1px;gap:1px 2px}", "a{gap:1px;gap:1px 2px}"]
10932+
])("keeps what stands in front of a lowered %s", (_name, css, expected) => {
10933+
expect(minifyFor(css, ["chrome 80"])).toBe(expected);
10934+
});
10935+
10936+
it("keeps both with the rewrite off", () => {
10937+
const css = "a{text-decoration:overline;text-decoration:underline dotted}";
10938+
expect(minifyForWith(css, T, { lowerUnsupported: false })).toBe(css);
10939+
});
10940+
});
10941+
1083110942
describe("CssSyntax minify — nesting the target cannot read", () => {
1083210943
it("writes a rule nested in another on its own", () => {
1083310944
expect(minifyFor("a{color:red;& b{color:blue}}", ["chrome 100"])).toBe(

0 commit comments

Comments
 (0)