Skip to content

Commit caf4d77

Browse files
fix(css): read an escaped property name as the name it spells
§4.3.7 lets any letter of a property name be written as an escape, so `\66 ont-weight` is `font-weight`. Every per-property rule read the name as the source spelled it, so an escaped one matched nothing. The worst of it is not a missed optimization: a custom property was not recognized as one, so its value was rewritten. `\2d -x:0px 0px 0px 0px` minified to `0 0 0 0`, and `var()` substitutes that value somewhere else — the spec keeps it as authored for exactly that reason. The rest are optimizations that silently did not apply: `\66 ont-weight:bold` kept `bold` where `font-weight:bold` folds to `700`, and a box shorthand written with an escape collapsed to `0 0 0 0` rather than `0`. The lookups now read the unescaped name and the printed name keeps the source's spelling. It costs an `indexOf` where nothing is escaped, which is every declaration in practice. Found by the CSS respelling sweep, which asks whether how a value was spelled decides what it minifies to.
1 parent f4d235e commit caf4d77

7 files changed

Lines changed: 104 additions & 5 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+
Read an escaped CSS property name as the name it spells.

‎lib/css/syntax-printer.js‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4178,18 +4178,23 @@ const printer = (path, writer) => {
41784178
}
41794179
case T_DECLARATION: {
41804180
const name = path.name();
4181+
// WHY: a property is matched by what its name means, not by how it was
4182+
// spelled — §4.3.7 lets any of its letters be written as an escape — so
4183+
// every lookup below reads the unescaped name while the printed one
4184+
// keeps the source's. It costs an `indexOf` where nothing is escaped.
4185+
const meaning = path.unescapedName();
41814186
// Counted rather than listed: a declaration's value is one component 92
41824187
// times in 100, and the list would be built only to be indexed twice.
41834188
const count = path.childCount();
41844189
// `@property`'s `initial-value` is read against the sibling `syntax`
41854190
// descriptor, so it is as opaque as a custom property's own value.
41864191
const custom =
4187-
name.startsWith("--") ||
4188-
(_S._inPropertyRule && equalsLowerCase(name, "initial-value")) ||
4192+
meaning.startsWith("--") ||
4193+
(_S._inPropertyRule && equalsLowerCase(meaning, "initial-value")) ||
41894194
// `@function`'s `result` is the token stream the call substitutes, so
41904195
// it is opaque the same way — and empty when the function returns the
41914196
// guaranteed-invalid value.
4192-
(_S._inFunctionRule && equalsLowerCase(name, "result")) ||
4197+
(_S._inFunctionRule && equalsLowerCase(meaning, "result")) ||
41934198
// A `{}` block standing as the whole value is a token stream the
41944199
// engine holds as written too — no grammar reads it, so a rewrite of
41954200
// it builds a different CSSOM from the same document.
@@ -4211,8 +4216,8 @@ const printer = (path, writer) => {
42114216
// Property names match ASCII case-insensitively and a custom property's
42124217
// does not, so skipping it skips the lowercasing. One fold serves both
42134218
// which value rules apply and how the name prints.
4214-
lowered = custom ? name : toLowerCaseIfNeeded(name);
4215-
const property = custom ? name : _standardSpelling(lowered);
4219+
lowered = custom ? meaning : toLowerCaseIfNeeded(meaning);
4220+
const property = custom ? meaning : _standardSpelling(lowered);
42164221
if (custom) {
42174222
// `getPropertyValue()` hands this text back, so a rewritten token
42184223
// would be a different CSSOM; only the boundaries between them go.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases css escaped-property-names escaped-property-names should compile 1`] = `
4+
Array [
5+
"/*! the value rules a property name selects */.t01{font-weight:700}",
6+
"/*! the same property, spelled with an escape */.t02{\\\\66 ont-weight:700}",
7+
"/*! a shorthand whose value repeats what the notation implies */.t03{margin:0}",
8+
"/*! the same shorthand, spelled with an escape */.t04{\\\\6d argin:0}",
9+
"/*! a custom property's value is kept as authored */.t05{--kept:0px 0px 0px 0px}",
10+
"/*! the same custom property, its leading dashes escaped */.t06{\\\\2d -kept:0px 0px 0px 0px}",
11+
"/*! a prefixed spelling reads through to the standard property */.t07{-webkit-box-flex:1}",
12+
"/*! the same prefixed spelling, with an escape */.t08{-\\\\77 ebkit-box-flex:1}",
13+
]
14+
`;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "./style.css";
2+
3+
it("should read a property name by what it means, not how it was spelled", () => {
4+
// The emitted stylesheet is snapshotted in test.config.js (afterExecute);
5+
// this keeps a runnable entry so the chunk and its `.css` are produced.
6+
expect(true).toBe(true);
7+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* A property name may be written with the escapes §4.3.7 resolves, so `\63 olor`
2+
is `color`. Every per-property rule has to read the name it means: the pairs
3+
below write the same declaration twice and each pair has to minify alike,
4+
apart from the escape the printed name keeps. */
5+
6+
/*! the value rules a property name selects */
7+
.t01 { font-weight: bold }
8+
/*! the same property, spelled with an escape */
9+
.t02 { \66 ont-weight: bold }
10+
/*! a shorthand whose value repeats what the notation implies */
11+
.t03 { margin: 0px 0px 0px 0px }
12+
/*! the same shorthand, spelled with an escape */
13+
.t04 { \6d argin: 0px 0px 0px 0px }
14+
/*! a custom property's value is kept as authored */
15+
.t05 { --kept: 0px 0px 0px 0px }
16+
/*! the same custom property, its leading dashes escaped */
17+
.t06 { \2d -kept: 0px 0px 0px 0px }
18+
/*! a prefixed spelling reads through to the standard property */
19+
.t07 { -webkit-box-flex: 1 }
20+
/*! the same prefixed spelling, with an escape */
21+
.t08 { -\77 ebkit-box-flex: 1 }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
const readMinifiedSections = require("../../../helpers/readMinifiedSections");
4+
5+
module.exports = {
6+
findBundle() {
7+
return ["bundle0.js"];
8+
},
9+
afterExecute(options) {
10+
expect(
11+
readMinifiedSections(options.output.path, "bundle0.css")
12+
).toMatchSnapshot();
13+
}
14+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
// Mirror a real production build: no per-module pathinfo banners.
12+
pathinfo: false
13+
},
14+
optimization: {
15+
minimize: true,
16+
// Same wiring as the production default (lib/config/defaults.js): one
17+
// minimizer plugin holding both minify functions, each routed by its filter.
18+
minimizer: [
19+
{
20+
apply: (compiler) => {
21+
new MinimizerPlugin({
22+
test: /\.(?:[cm]?js|css)(\?.*)?$/i,
23+
minify: [MinimizerPlugin.terserMinify, cssMinify],
24+
minimizerOptions: [{ compress: { passes: 2 } }, {}]
25+
}).apply(/** @type {EXPECTED_ANY} */ (compiler));
26+
}
27+
}
28+
]
29+
},
30+
experiments: {
31+
css: true
32+
}
33+
};

0 commit comments

Comments
 (0)