Skip to content

Commit ad75636

Browse files
perf(css): drop what a declaration, a slot or a query already says
Five things the minifier wrote out that say nothing: - A declaration a later one overrides, where the two are spelled from one vocabulary — a pair of numbers carrying one unit, a pair of hexes on one side of the alpha four digits state, a pair of color names as old as naming a color, or `inherit`. That is what parts an override from a fallback without asking the target: `color:#eee` before `color:lab(…)` is the older color standing where the newer one is not read, while `color:#eee;color:#333` is one an engine reading either reads both of. Two adjacent rules with one selector join on the same terms, where they used to be kept apart by any property they shared. - A shorthand slot holding its own initial, where the slot takes more than keywords: `border:medium none currentcolor` is `border:none`. The initial table now carries what each slot takes beyond its spellings, so a sibling filling it is read by the classes its own value is in — a length, a color — rather than by a list of names, and a value that classes as nothing keeps the slot. - A `<time>` of zero nothing after it depends on, in a transition or an animation, and an iteration count of one. - The `all` a media query states before an `and`, which matches what the condition alone does. - An operand a condition already states, in `@supports`, `@media` and `@container` alike. `rebeccapurple` and `transparent` are the names left out of the first, derived from the compatibility rows they carry of their own: an engine may read neither while reading the rest. Held to Chromium: every rewrite computes as what it replaced, over the equivalence fixtures a new one covers.
1 parent 32c32fc commit ad75636

18 files changed

Lines changed: 1333 additions & 346 deletions

File tree

‎lib/css/data.js‎

Lines changed: 648 additions & 262 deletions
Large diffs are not rendered by default.

‎lib/css/syntax.js‎

Lines changed: 377 additions & 35 deletions
Large diffs are not rendered by default.

‎test/CssSyntax.unittest.js‎

Lines changed: 189 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,9 +2077,9 @@ describe("CssSyntax — minify transforms, in-process", () => {
20772077
// Both are the same declaration however they are spelled, so a rule an
20782078
// identical later sibling repeats is dead whichever way each spells it.
20792079
it("makes two spellings of one property the same bytes", () => {
2080-
expect(min("a{color:red}a{COLOR:blue}")).toBe(
2081-
"a{color:red}a{color:blue}"
2082-
);
2080+
// Both spell one property, so the later rule is read as the override it
2081+
// is rather than as another property.
2082+
expect(min("a{color:red}a{COLOR:blue}")).toBe("a{color:blue}");
20832083
expect(min("a{COLOR:red}a{color:red}")).toBe("a{color:red}");
20842084
});
20852085

@@ -2886,7 +2886,7 @@ describe("CssSyntax — skip set (CssProcessOptions.skip)", () => {
28862886
// A `}` closes no block here, so it is a parse error whose bad declaration
28872887
// runs to the next `;` — what a browser reads a `style=""` as.
28882888
expect(minify("color:red;}color:blue")).toBe("color:red");
2889-
expect(minify("color:red;};color:blue")).toBe("color:red;color:blue");
2889+
expect(minify("color:red;};color:blue")).toBe("color:blue");
28902890
// Inside a value it is a token like any other.
28912891
expect(minify("color:red}color:blue")).toBe("color:red}color:blue");
28922892
});
@@ -3589,7 +3589,6 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
35893589
// order they were written in.
35903590
["a{flex-flow:row nowrap}", "a{flex-flow:row}"],
35913591
["a{flex-flow:nowrap row}", "a{flex-flow:row}"],
3592-
["a{border:none medium}", "a{border:medium}"],
35933592
["a{list-style:disc outside}", "a{list-style:disc}"],
35943593
["a{mask:url(a.svg) match-source add}", "a{mask:url(a.svg)}"],
35953594
[
@@ -3611,7 +3610,16 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
36113610
["a{background:padding-box border-box none}", "a{background:none}"],
36123611
["a{border-image:none 30}", "a{border-image:30}"],
36133612
["a{mask:none luminance}", "a{mask:luminance}"],
3614-
["a{TRANSITION:opacity 1s EASE}", "a{transition:opacity 1s}"]
3613+
["a{TRANSITION:opacity 1s EASE}", "a{transition:opacity 1s}"],
3614+
// A slot taking a length or a color is read the same way, by the classes
3615+
// a sibling's own value is in: neither `solid` nor `red` is a width.
3616+
["a{border:medium solid red}", "a{border:solid red}"],
3617+
["a{border:medium none currentcolor}", "a{border:none}"],
3618+
["a{border:none medium}", "a{border:none}"],
3619+
["a{column-rule:medium none currentcolor}", "a{column-rule:none}"],
3620+
// ...and a sibling that is one keeps it.
3621+
["a{border:medium 1px}", "a{border:medium 1px}"],
3622+
["a{border:currentcolor red}", "a{border:currentcolor red}"]
36153623
])("%s", (css, expected) => {
36163624
expect(minify(css)).toBe(expected);
36173625
});
@@ -3647,12 +3655,6 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
36473655
["the first layer is empty", "a{transition:,opacity 1s ease}"],
36483656
["the last layer is empty", "a{transition:opacity 1s ease,}"],
36493657
["the property is no shorthand", "a{border-style:none}"],
3650-
// `<line-width>` reaches `<length>`, which no spelling names, so a
3651-
// sibling filling that slot cannot be recognized.
3652-
[
3653-
"the slot takes a value no spelling names",
3654-
"a{border:medium solid red}"
3655-
],
36563658
// An image function fills `<bg-image>` as much as `none` does.
36573659
["a call fills the image slot", "a{background:none url(a.png)}"],
36583660
[
@@ -3811,7 +3813,7 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
38113813

38123814
it("keeps the one between, which says what neither of them does", () => {
38133815
expect(minify("@media all{a{color:red}a{color:blue}a{color:red}}")).toBe(
3814-
"@media all{a{color:blue}a{color:red}}"
3816+
"@media all{a{color:red}}"
38153817
);
38163818
});
38173819

@@ -4197,10 +4199,23 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
41974199
expect(minifyFor(css, ["chrome 85"])).toBe(css);
41984200
});
41994201

4202+
it("joins the same selector's two rules where one overrides the other", () => {
4203+
// One block holds a property once, so a property both of them set joins
4204+
// only where the later declaration is one the earlier cannot have been
4205+
// read behind — which is the same question a repeated property inside
4206+
// one block answers.
4207+
expect(minify("a{color:red}a{color:blue}")).toBe("a{color:blue}");
4208+
expect(minify("a{color:red;top:0}a{color:blue;left:0}")).toBe(
4209+
"a{top:0;color:blue;left:0}"
4210+
);
4211+
// ...and not where the earlier one may still be what an engine reads.
4212+
const fallback = "a{color:#fff}a{color:var(--x)}";
4213+
expect(minify(fallback)).toBe(fallback);
4214+
const important = "a{color:red!important}a{color:blue}";
4215+
expect(minify(important)).toBe(important);
4216+
});
4217+
42004218
it.each([
4201-
// One block holds a property once, so the earlier declaration of a
4202-
// property both of them set would be the one it loses.
4203-
["they set the same property", "a{color:red}a{color:blue}"],
42044219
// A shorthand holds every longhand its name prefixes.
42054220
[
42064221
"one holds the other's longhand",
@@ -5706,6 +5721,164 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
57065721
});
57075722
});
57085723

5724+
describe("a condition saying what another already says", () => {
5725+
it("drops the media type a query states for nothing", () => {
5726+
expect(minify("@media all and (min-width:100px){a{color:red}}")).toBe(
5727+
"@media (width>=100px){a{color:red}}"
5728+
);
5729+
expect(minify("@media print,all and (color){a{color:red}}")).toBe(
5730+
"@media print,(color){a{color:red}}"
5731+
);
5732+
// `all` on its own is the whole query and says everything, and `not` or
5733+
// `only` before it is not the same query at all — `only` is there to
5734+
// hide the rule from an engine reading CSS 2's grammar.
5735+
expect(minify("@media all{a{color:red}}")).toBe(
5736+
"@media all{a{color:red}}"
5737+
);
5738+
expect(minify("@media only all and (color){a{color:red}}")).toBe(
5739+
"@media only all and (color){a{color:red}}"
5740+
);
5741+
expect(minify("@media not all and (color){a{color:red}}")).toBe(
5742+
"@media not all and (color){a{color:red}}"
5743+
);
5744+
expect(minify("@media screen and (color){a{color:red}}")).toBe(
5745+
"@media screen and (color){a{color:red}}"
5746+
);
5747+
});
5748+
5749+
it("drops an operand a condition already states", () => {
5750+
expect(
5751+
minify("@supports (display:grid) and (display:grid){a{color:red}}")
5752+
).toBe("@supports (display:grid){a{color:red}}");
5753+
expect(minify("@supports (a:b) or (a:b) or (c:d){a{color:red}}")).toBe(
5754+
"@supports (a:b) or (c:d){a{color:red}}"
5755+
);
5756+
expect(
5757+
minify("@supports ((a:b) or (c:d)) and ((a:b) or (c:d)){a{color:red}}")
5758+
).toBe("@supports ((a:b) or (c:d)){a{color:red}}");
5759+
expect(minify("@media (color) and (color){a{color:red}}")).toBe(
5760+
"@media (color){a{color:red}}"
5761+
);
5762+
expect(
5763+
minify("@container (width>400px) and (width>400px){a{color:red}}")
5764+
).toBe("@container (width>400px){a{color:red}}");
5765+
// A bare operand is a part of one rather than one this can compare, and
5766+
// two queries are not two operands of anything.
5767+
const negated = "@supports not (a:b) and not (a:b){a{color:red}}";
5768+
expect(minify(negated)).toBe(negated);
5769+
const listed = "@media (color),(color){a{color:red}}";
5770+
expect(minify(listed)).toBe(listed);
5771+
// ...and a chain mixing the two operators is no flat list of either's.
5772+
const mixed = "@supports (a:b) and (c:d) or (e:f){a{color:red}}";
5773+
expect(minify(mixed)).toBe(mixed);
5774+
});
5775+
});
5776+
5777+
describe("a time or a count a layer runs with anyway", () => {
5778+
it("drops the zero time nothing after it depends on", () => {
5779+
expect(minify("a{transition:opacity .3s 0s}")).toBe(
5780+
"a{transition:opacity.3s}"
5781+
);
5782+
expect(minify("a{transition:opacity 0s}")).toBe("a{transition:opacity}");
5783+
expect(minify("a{transition:opacity .3s 0s ease-in,color 1s 0s}")).toBe(
5784+
"a{transition:opacity.3s ease-in,color 1s}"
5785+
);
5786+
// The first `<time>` fills the duration and the second the delay, so
5787+
// dropping the first would hand the delay's value to the duration.
5788+
expect(minify("a{transition:opacity 0s .3s}")).toBe(
5789+
"a{transition:opacity 0s.3s}"
5790+
);
5791+
});
5792+
5793+
it("drops an animation's own defaults", () => {
5794+
expect(minify("a{animation:1s ease 0s 1 normal none running x}")).toBe(
5795+
"a{animation:1s none x}"
5796+
);
5797+
expect(minify("a{animation:x 1s 1}")).toBe("a{animation:x 1s}");
5798+
expect(minify("a{animation:1s x,2s 0s y}")).toBe(
5799+
"a{animation:1s x,2s y}"
5800+
);
5801+
// A count that is not one, and the keyword that says every count.
5802+
expect(minify("a{animation:x 1s 2}")).toBe("a{animation:x 1s 2}");
5803+
// A quoted name could carry the comma the layer split reads, and an
5804+
// empty layer is a value this does not part.
5805+
expect(minify('a{animation:"x" 1s 0s}')).toBe('a{animation:"x"1s 0s}');
5806+
expect(minify("a{animation:1s x,,2s y}")).toBe("a{animation:1s x,,2s y}");
5807+
expect(minify("a{animation:x 1s infinite}")).toBe(
5808+
"a{animation:x 1s infinite}"
5809+
);
5810+
});
5811+
});
5812+
5813+
describe("a declaration a later one overrides", () => {
5814+
it("drops it where the two are spelled from one vocabulary", () => {
5815+
expect(minify("a{color:#fff;color:#333}")).toBe("a{color:#333}");
5816+
expect(minify("a{color:red;color:blue}")).toBe("a{color:blue}");
5817+
expect(minify("a{color:#fff;color:red}")).toBe("a{color:red}");
5818+
expect(minify("a{margin-top:1px;margin-top:2px}")).toBe(
5819+
"a{margin-top:2px}"
5820+
);
5821+
expect(minify("a{opacity:.5;opacity:1}")).toBe("a{opacity:1}");
5822+
expect(minify("a{color:red;color:inherit}")).toBe("a{color:inherit}");
5823+
// Whatever stands between them, which can only lose to the later one
5824+
// wherever it would have beaten this.
5825+
expect(minify("a{color:red;background:red;color:blue}")).toBe(
5826+
"a{background:red;color:blue}"
5827+
);
5828+
});
5829+
5830+
it.each([
5831+
// The later value is the one an engine may not read, which is what a
5832+
// fallback pair is written for.
5833+
["a substitution", "a{color:#fff;color:var(--x)}"],
5834+
["a call", "a{width:100px;width:calc(100% - 1px)}"],
5835+
["a newer color spelling", "a{color:#eee;color:lab(50% 40 -30)}"],
5836+
["a hex the other states no alpha for", "a{color:#fff;color:#33333380}"],
5837+
["a unit the earlier one does not write", "a{width:1em;width:2dvh}"],
5838+
[
5839+
"a name that arrived later",
5840+
"a{color:red;color:rebeccapurple}",
5841+
"a{color:red;color:#639}"
5842+
],
5843+
// A keyword an engine may not know is not one either — and `display`
5844+
// is where that pair is written on purpose.
5845+
["a keyword", "a{display:block;display:flex}"],
5846+
// An `!important` earlier declaration still wins.
5847+
["an earlier one that wins", "a{color:red!important;color:blue}"]
5848+
])("keeps it behind %s", (_name, css, printed = css) => {
5849+
expect(minify(css)).toBe(printed);
5850+
});
5851+
5852+
it("reads two rules' declarations only where it may drop one", () => {
5853+
// An empty declaration between two is nothing to read; one without a
5854+
// value is not a declaration this can answer for.
5855+
expect(minify("a{color:red;;top:0}a{left:0}")).toBe(
5856+
"a{color:red;top:0;left:0}"
5857+
);
5858+
const partial = "a{color:red;top}a{left:0}";
5859+
expect(minify(partial)).toBe(partial);
5860+
// ...and the later rule holding one is read for its properties alone,
5861+
// which is what the join then refuses on.
5862+
expect(minify("a{color:red}a{top}")).toBe("a{color:red;top}");
5863+
// A `;` inside a string parts no declaration, and neither does an escape
5864+
// the string carries.
5865+
expect(minify('a{content:"a\\";b";color:red}a{left:0}')).toBe(
5866+
"a{content:'a\";b';color:red;left:0}"
5867+
);
5868+
expect(minify('a{content:"a\\"b\'c";color:red}a{left:0}')).toBe(
5869+
'a{content:"a\\"b\'c";color:red;left:0}'
5870+
);
5871+
// ...and with the transform off, two rules stay two.
5872+
const pair = "a{color:red}a{color:blue}";
5873+
expect(
5874+
new SourceProcessor().process(pair, {
5875+
mode: "minify",
5876+
transforms: { removeDeadRules: false }
5877+
}).code
5878+
).toBe(pair);
5879+
});
5880+
});
5881+
57095882
describe("media-feature range intervals", () => {
57105883
it("collapses an `and` of two one-sided ranges, either order", () => {
57115884
expect(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ Array [
5454
"/*! declined: a nested rule parts the declarations around it into their own
5555
rules, so the first is read at its own place rather than resolving to the
5656
second — dropping it loses a rule the engine builds */.t24b{color:red;& i{top:0}color:red}",
57-
"/*! declined: the importance differs, so they are not the same declaration */.t25{color:red;color:red!important}",
57+
"/*! webpack: the importance differs, so the two are not the same declaration —
58+
but the important one wins wherever the plain one would have been read */.t25{color:red!important}",
5859
"/*! declined: a value spelled differently is a fallback for an engine that cannot
5960
read the later one, and a bare name says nothing about which engines do — it
6061
may be invalid, or newer than the value it stands after */.t25b{color:red;color:canvastext}",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ Array [
5454
"/*! declined: a nested rule parts the declarations around it into their own
5555
rules, so the first is read at its own place rather than resolving to the
5656
second — dropping it loses a rule the engine builds */.t24b{color:red;& i{top:0}color:red}",
57-
"/*! declined: the importance differs, so they are not the same declaration */.t25{color:red;color:red!important}",
57+
"/*! webpack: the importance differs, so the two are not the same declaration —
58+
but the important one wins wherever the plain one would have been read */.t25{color:red!important}",
5859
"/*! declined: a value spelled differently is a fallback for an engine that cannot
5960
read the later one, and a bare name says nothing about which engines do — it
6061
may be invalid, or newer than the value it stands after */.t25b{color:red;color:canvastext}",

‎test/configCases/css/minimize-declined/style.css‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@
9191
rules, so the first is read at its own place rather than resolving to the
9292
second — dropping it loses a rule the engine builds */
9393
.t24b { color: red; & i { top: 0 } color: red }
94-
/*! declined: the importance differs, so they are not the same declaration */
94+
/*! webpack: the importance differs, so the two are not the same declaration —
95+
but the important one wins wherever the plain one would have been read */
9596
.t25 { color: red; color: red !important }
9697
/*! declined: a value spelled differently is a fallback for an engine that cannot
9798
read the later one, and a bare name says nothing about which engines do — it

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Array [
99
"/*! declined: a vendor pseudo is one an engine may not parse */.e{.n10::-webkit-scrollbar{top:0}.n11::-webkit-scrollbar{top:0}}",
1010
"/*! values: a number, a time, a zero angle and a color all fold the same nested */.f{.n12{transition-duration:.5s;transform:rotate(0);color:#fff;width:5px}}",
1111
"/*! shorthand: four sides merge into the one shorthand inside a nested rule */.g{.n13{margin:1px 2px}}",
12-
"/*! shorthand: ...and a slot holding its own initial goes */.h{.n14{transition:opacity 1s 0s}}",
12+
"/*! shorthand: ...and a slot holding its own initial goes */.h{.n14{transition:opacity 1s}}",
1313
"/*! filter: an argument that is the amount an omitted one means says nothing */.i{.n15{filter:blur()grayscale()}}",
1414
"/*! declaration: only the last of a set of identical ones can be read */.j{.n16{color:red}}",
1515
"/*! declaration: ...whatever nested rule stands between them */.k{color:red;.n17{top:0}color:red}",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Array [
99
"/*! declined: a vendor pseudo is one an engine may not parse */.e{.n10::-webkit-scrollbar{top:0}.n11::-webkit-scrollbar{top:0}}",
1010
"/*! values: a number, a time, a zero angle and a color all fold the same nested */.f{.n12{transition-duration:.5s;transform:rotate(0);color:#fff;width:5px}}",
1111
"/*! shorthand: four sides merge into the one shorthand inside a nested rule */.g{.n13{margin:1px 2px}}",
12-
"/*! shorthand: ...and a slot holding its own initial goes */.h{.n14{transition:opacity 1s 0s}}",
12+
"/*! shorthand: ...and a slot holding its own initial goes */.h{.n14{transition:opacity 1s}}",
1313
"/*! filter: an argument that is the amount an omitted one means says nothing */.i{.n15{filter:blur()grayscale()}}",
1414
"/*! declaration: only the last of a set of identical ones can be read */.j{.n16{color:red}}",
1515
"/*! declaration: ...whatever nested rule stands between them */.k{color:red;.n17{top:0}color:red}",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ Array [
7474
"/*! webpack: ...and a keyframe selector list is one too */@keyframes t74{0%,50%{color:red}}",
7575
"/*! webpack: the same selector twice is one rule */.t80{color:red;margin:0}",
7676
"/*! webpack: ...and two identical rules are the one that can be read */.t81{color:red}",
77-
"/*! declined: the earlier declaration of a property both set would be lost */.t82{color:red}.t82{color:blue}",
77+
"/*! webpack: a property both set joins where the later declaration is one the
78+
earlier cannot have been read behind */.t82{color:blue}",
7879
"/*! declined: a shorthand holds every longhand its name prefixes */.t83{background:red}.t83{background-image:none}",
7980
"/*! declined: declarations after a nested rule are an implicit \`&\` rule */.t84{color:red;&:hover{top:0}}.t84{margin:0}",
8081
"/*! webpack: a selector list is a set, so one order lets equal lists compare */.t90a,.t90b{color:red}",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ Array [
7474
"/*! webpack: ...and a keyframe selector list is one too */@keyframes t74{0%,50%{color:red}}",
7575
"/*! webpack: the same selector twice is one rule */.t80{color:red;margin:0}",
7676
"/*! webpack: ...and two identical rules are the one that can be read */.t81{color:red}",
77-
"/*! declined: the earlier declaration of a property both set would be lost */.t82{color:red}.t82{color:blue}",
77+
"/*! webpack: a property both set joins where the later declaration is one the
78+
earlier cannot have been read behind */.t82{color:blue}",
7879
"/*! declined: a shorthand holds every longhand its name prefixes */.t83{background:red}.t83{background-image:none}",
7980
"/*! declined: declarations after a nested rule are an implicit \`&\` rule */.t84{color:red;&:hover{top:0}}.t84{margin:0}",
8081
"/*! webpack: a selector list is a set, so one order lets equal lists compare */.t90a,.t90b{color:red}",

0 commit comments

Comments
 (0)