Skip to content

Commit 02186d1

Browse files
authored
fix(css): keep the calc() a bare number would revive (#22205)
Four properties read a bare number as a length of their own where Gecko refuses that same number inside a `calc()`, so dropping the parentheses brought a declaration it had thrown away back to life — measured over every property `mdn-data` and the prefix table name, in all three engines. Both the fold and the declaration printer ask, since one reads the declaration as written and the other the standard spelling it maps to.
1 parent 39f17ad commit 02186d1

12 files changed

Lines changed: 153 additions & 10 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 the `calc()` where a bare number would revive a refused declaration.

‎.gitattributes‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ test/configCases/html/minimize-lookup-decoded-value/** eol=lf
3535
test/configCases/html/minimize-rewrite-referenced-value/** eol=lf
3636
test/configCases/html/minimize-spell-back-referenced-value/** eol=lf
3737
test/configCases/css/minify-at-rules-and-values/** eol=lf
38+
test/configCases/css/minify-calc-number-outside/** eol=lf
3839
test/configCases/css/minify-color-names/** eol=lf
3940
test/configCases/css/minify-initial-keyword-engine-gap/** eol=lf
4041
test/configCases/css/minify-modern-longhands/** eol=lf

‎lib/css/data.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,6 +3592,17 @@ const ZERO_UNIT_KEEPING_PROPERTIES = new Set([
35923592
// rather than folding to the value it equals.
35933593
const CALC_REJECTING_PROPERTIES = new Set(["overflow-clip-margin"]);
35943594

3595+
// The properties that read a bare number as a length of their own, where an
3596+
// engine refuses the same number inside `calc()` — so taking the parentheses
3597+
// off would revive a declaration it had thrown away.
3598+
const NUMBER_ONLY_OUTSIDE_CALC_PROPERTIES = new Set([
3599+
"-webkit-perspective",
3600+
"perspective",
3601+
"stroke-dasharray",
3602+
"stroke-dashoffset",
3603+
"stroke-width"
3604+
]);
3605+
35953606
// The range a `calc()` is clamped to where the literal outside it is invalid,
35963607
// keyed by property: `[unit, min, max]`.
35973608
/** @type {Map<string, [string, number, number]>} */
@@ -7940,6 +7951,8 @@ module.exports.NTH_PSEUDO_FUNCTIONS = NTH_PSEUDO_FUNCTIONS;
79407951
module.exports.OMITTABLE_INITIAL_KEYWORDS = OMITTABLE_INITIAL_KEYWORDS;
79417952
module.exports.ONE_VALUE_PAIR_SHORTHANDS = ONE_VALUE_PAIR_SHORTHANDS;
79427953
module.exports.ORDERED_LONGHANDS = ORDERED_LONGHANDS;
7954+
module.exports.NUMBER_ONLY_OUTSIDE_CALC_PROPERTIES =
7955+
NUMBER_ONLY_OUTSIDE_CALC_PROPERTIES;
79437956
module.exports.PAIR_LONGHANDS = PAIR_LONGHANDS;
79447957
module.exports.PLACE_SHORTHANDS = PLACE_SHORTHANDS;
79457958
module.exports.POSITION_PROPERTIES = POSITION_PROPERTIES;

‎lib/css/syntax.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const {
9898
NEVER,
9999
NTH_NAMED_EQUIVALENTS,
100100
NTH_PSEUDO_FUNCTIONS,
101+
NUMBER_ONLY_OUTSIDE_CALC_PROPERTIES,
101102
OMITTABLE_INITIAL_KEYWORDS,
102103
ONE_VALUE_PAIR_SHORTHANDS,
103104
ORDERED_LONGHANDS,
@@ -7467,6 +7468,18 @@ const _inCalcRejectingProperty = () =>
74677468
_valueDeclaration !== null &&
74687469
CALC_REJECTING_PROPERTIES.has(toLowerCaseIfNeeded(A.name(_valueDeclaration)));
74697470

7471+
/**
7472+
* Whether the declaration being printed reads a bare number as a length of its
7473+
* own, where the engine refuses that number inside a `calc()` — so a term that
7474+
* would be written bare keeps the `calc()` it was written with.
7475+
* @returns {boolean} true inside such a declaration
7476+
*/
7477+
const _inNumberOnlyOutsideCalcProperty = () =>
7478+
_valueDeclaration !== null &&
7479+
NUMBER_ONLY_OUTSIDE_CALC_PROPERTIES.has(
7480+
toLowerCaseIfNeeded(A.name(_valueDeclaration))
7481+
);
7482+
74707483
// A folded term, as a number and its unit.
74717484
const _TERM_VALUE_RE = /^(-?(?:\d+\.?\d*|\.\d+))([a-z%]*)$/i;
74727485

@@ -16066,6 +16079,16 @@ const _unwrapCalc = (fragment, property) => {
1606616079
// outside; taking the parentheses off here would undo that.
1606716080
if (_losesClamp(property, number, unit)) return fragment;
1606816081
if (unit === "" && Number(number) === 0) return fragment;
16082+
// A bare number these read as a length of their own is one the engine refuses
16083+
// inside `calc()`, so the parentheses stay. Both names are asked: the prefixed
16084+
// spelling is the one that differs, and `property` is the standard one.
16085+
if (
16086+
unit === "" &&
16087+
(NUMBER_ONLY_OUTSIDE_CALC_PROPERTIES.has(property) ||
16088+
_inNumberOnlyOutsideCalcProperty())
16089+
) {
16090+
return fragment;
16091+
}
1606916092
if (unit === "" && number.includes(".") && INTEGER_PROPERTIES.has(property)) {
1607016093
return fragment;
1607116094
}
@@ -17335,6 +17358,7 @@ const printer = (path, writer) => {
1733517358
const folded =
1733617359
shape.test(term) &&
1733717360
!_inCalcRejectingProperty() &&
17361+
!(!/[a-z%]$/i.test(term) && _inNumberOnlyOutsideCalcProperty()) &&
1733817362
!_foldLosesClamp(term) &&
1733917363
!_foldBelowStepCount(term, path)
1734017364
? term
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-calc-number-outside minify-calc-number-outside should compile 1`] = `".a{stroke-width:calc(2)}.b{stroke-dasharray:calc(1000)}.c{stroke-dashoffset:calc(1.5)}.d{-webkit-perspective:calc(1000)}.e,.f{stroke-width:2px}.g{opacity:.5}.h{z-index:2}.i{width:10px}"`;
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-calc-number-outside minify-calc-number-outside should compile 1`] = `".a{stroke-width:calc(2)}.b{stroke-dasharray:calc(1000)}.c{stroke-dashoffset:calc(1.5)}.d{-webkit-perspective:calc(1000)}.e,.f{stroke-width:2px}.g{opacity:.5}.h{z-index:2}.i{width:10px}"`;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "./style.css";
2+
3+
// Dropping the parentheses off a unitless number revives a declaration the
4+
// engine had thrown away, on the properties that read a bare number as a length.
5+
it("should keep the calc() a bare number would revive", () => {
6+
expect(true).toBe(true);
7+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* A bare number these read as a length of their own, where Gecko refuses the
2+
same number inside a `calc()` — so the parentheses stay. */
3+
.a { stroke-width: calc(2) }
4+
.b { stroke-dasharray: calc(1000) }
5+
.c { stroke-dashoffset: calc(1.5) }
6+
.d { -webkit-perspective: calc(1000) }
7+
8+
/* The same properties with a unit, and properties that read a number as a
9+
number: the fold still writes those bare. */
10+
.e { stroke-width: calc(2px) }
11+
.f { stroke-width: calc(1px + 1px) }
12+
.g { opacity: calc(0.5) }
13+
.h { z-index: calc(2) }
14+
.i { width: calc(10px) }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
module.exports = {
7+
afterExecute(options) {
8+
const css = fs.readFileSync(
9+
path.join(options.output.path, "bundle0.css"),
10+
"utf8"
11+
);
12+
expect(css).toMatchSnapshot();
13+
14+
// Gecko refuses each of these inside a `calc()` and reads the bare number
15+
// as a length, so unwrapping would bring the declaration back to life.
16+
expect(css).toContain("stroke-width:calc(2)");
17+
expect(css).toContain("stroke-dasharray:calc(1000)");
18+
expect(css).toContain("stroke-dashoffset:calc(1.5)");
19+
expect(css).toContain("-webkit-perspective:calc(1000)");
20+
// A unit settles it, and so does a property that reads a number as one.
21+
expect(css).toContain("stroke-width:2px");
22+
expect(css).toContain("opacity:.5");
23+
expect(css).toContain("z-index:2");
24+
expect(css).toContain("width:10px");
25+
}
26+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
3+
const MinimizerPlugin = require("minimizer-webpack-plugin");
4+
const cssMinify = require("../../../../lib/css/cssMinify");
5+
6+
/** @type {import("../../../../").Configuration} */
7+
module.exports = {
8+
target: "web",
9+
mode: "production",
10+
output: {
11+
pathinfo: false
12+
},
13+
optimization: {
14+
minimize: true,
15+
minimizer: [
16+
{
17+
apply: (compiler) => {
18+
new MinimizerPlugin({
19+
test: /\.css(\?.*)?$/i,
20+
minify: cssMinify
21+
}).apply(/** @type {EXPECTED_ANY} */ (compiler));
22+
}
23+
}
24+
]
25+
},
26+
experiments: {
27+
css: true
28+
}
29+
};

0 commit comments

Comments
 (0)