Skip to content

Commit 95910a8

Browse files
feat(css): fold a one-axis skew, an alpha percentage and a ratio over one
`skewX(a)` is the `skew(a)` whose second component defaults to 0, an `<alpha-value>` percentage is the number a hundredth of it names, and a `<ratio>` denominator of 1 is the one an omitted denominator means.
1 parent a2a5170 commit 95910a8

9 files changed

Lines changed: 245 additions & 14 deletions

File tree

‎.changeset/070-css-html-minify-transforms.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": minor
33
---
44

5-
Minify CSS and HTML further where the document is unchanged: CSS rounds a number to 6 significant digits, rewrites a time into the shorter of `ms` / `s`, folds `calc()` and every math function the spec's grammars name over constants whose result is exact, rounding what it prints the way an authored number is rounded, reducing `calc-size()`'s size in place, down to the sum of unlike units it reduces to, takes the parentheses off a folded negative where the property accepts one, drops a zero's unit inside a call whose every number is a length, converts every polar and Lab color function to hex where the engine agrees, merges four box longhands with unrelated declarations between them — sides or corners — the two a pair shorthand sets, and the slots of an order-free one where each value parses back into its own, collapses an `and` of two one-sided media-feature ranges into the interval it describes, writes `2n+1` as the `odd` it names, a zero-step An+B as the child it selects, a `from` keyframe as `0%`, each `unicode-range` in its shortest equal spelling and each named color in the shortest spelling of its value where the property takes no identifier of the author's own, writes a two-keyword `display` as the one keyword naming the same box, a transform and a linear gradient in their shorter equal form, the `font` shorthand's weight as the number naming it and each layer of a `transition` in the order its grammar lists the slots, unquotes a font family the quotes carry nothing for, rewrites a `<position>` written as edge keywords — including the properties spelling both axes out — and a `font-stretch` keyword into the percentages they resolve to, drops a `<position>`'s second value where it names the centre an omitted one already means, drops a shorthand slot holding the value it already defaults to — including one every value of which its grammar spells out, such as `background`'s image — a `flex` shrink factor no basis can be read as, a shadow's trailing zero lengths and a filter function's argument that is the amount an omitted one means, writes the percent-escapes a data URI's payload does not need as the bytes they name, drops a gradient's last color stop position where the spec's own fix-up already puts it there and — where the target reads CSS Images 4's two-position stop — writes two stops of one color as the one stop naming both, collapses the values a property's own grammar already implies — two equal `<repeat-style>` keywords, an `initial` whose initial value is a shorter word and a `grid-template-areas` row's padding — drops the unit a zero angle does not need and the whitespace neither a combinator inside a selector function nor two calls in a value need, writes a rotation about one axis as the call naming it, reduces a transform until it stops getting shorter, drops a background size's second value where `auto` is what an omitted one already means, and keeps a `@supports` condition as written; HTML normalizes each attribute against the element it is on.
5+
Minify CSS and HTML further where the document is unchanged: CSS rounds a number to 6 significant digits, rewrites a time into the shorter of `ms` / `s`, folds `calc()` and every math function the spec's grammars name over constants whose result is exact, rounding what it prints the way an authored number is rounded, reducing `calc-size()`'s size in place, down to the sum of unlike units it reduces to, takes the parentheses off a folded negative where the property accepts one, drops a zero's unit inside a call whose every number is a length, converts every polar and Lab color function to hex where the engine agrees, merges four box longhands with unrelated declarations between them — sides or corners — the two a pair shorthand sets, and the slots of an order-free one where each value parses back into its own, collapses an `and` of two one-sided media-feature ranges into the interval it describes, writes `2n+1` as the `odd` it names, a zero-step An+B as the child it selects, a `from` keyframe as `0%`, each `unicode-range` in its shortest equal spelling and each named color in the shortest spelling of its value where the property takes no identifier of the author's own, writes a two-keyword `display` as the one keyword naming the same box, a transform and a linear gradient in their shorter equal form, the `font` shorthand's weight as the number naming it and each layer of a `transition` in the order its grammar lists the slots, unquotes a font family the quotes carry nothing for, rewrites a `<position>` written as edge keywords — including the properties spelling both axes out — and a `font-stretch` keyword into the percentages they resolve to, drops a `<position>`'s second value where it names the centre an omitted one already means, drops a shorthand slot holding the value it already defaults to — including one every value of which its grammar spells out, such as `background`'s image — a `flex` shrink factor no basis can be read as, a shadow's trailing zero lengths and a filter function's argument that is the amount an omitted one means, writes the percent-escapes a data URI's payload does not need as the bytes they name, drops a gradient's last color stop position where the spec's own fix-up already puts it there and — where the target reads CSS Images 4's two-position stop — writes two stops of one color as the one stop naming both, collapses the values a property's own grammar already implies — two equal `<repeat-style>` keywords, an `initial` whose initial value is a shorter word and a `grid-template-areas` row's padding — drops the unit a zero angle does not need and the whitespace neither a combinator inside a selector function nor two calls in a value need, writes a rotation about one axis as the call naming it, reduces a transform until it stops getting shorter, drops a background size's second value where `auto` is what an omitted one already means, writes a skew along one axis as the pair naming it, an alpha value's percentage as the number a hundredth of it names and a ratio without the denominator of `1` an omitted one already means, and keeps a `@supports` condition as written; HTML normalizes each attribute against the element it is on.

‎lib/css/data.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,6 +2245,14 @@ const INTEGER_PROPERTIES = new Set([
22452245
"z-index"
22462246
]);
22472247

2248+
// The properties whose value is one `<number> | <percentage>`, where the
2249+
// percentage is the number hundredfold and the two compute to the same thing.
2250+
const ALPHA_VALUE_PROPERTIES = new Set(["opacity", "shape-image-threshold"]);
2251+
2252+
// The properties taking a `<ratio>`, whose second number of `1` is the one an
2253+
// omitted denominator means.
2254+
const RATIO_PROPERTIES = new Set(["aspect-ratio"]);
2255+
22482256
// The keywords of every property a `css/module` reads a scoped name out of,
22492257
// each mapped to how many times it may be spelled before the next one is the
22502258
// name (`Infinity` — never the name). Derived from each property's grammar.
@@ -3032,6 +3040,7 @@ const RGB_TO_NAME = new Map([
30323040
]);
30333041

30343042
module.exports.ABSOLUTE_UNIT_SCALE = ABSOLUTE_UNIT_SCALE;
3043+
module.exports.ALPHA_VALUE_PROPERTIES = ALPHA_VALUE_PROPERTIES;
30353044
module.exports.ANGLE_UNITS = ANGLE_UNITS;
30363045
module.exports.ARC_COSINE_DEGREES = ARC_COSINE_DEGREES;
30373046
module.exports.ARC_SINE_DEGREES = ARC_SINE_DEGREES;
@@ -3080,6 +3089,7 @@ module.exports.POSITION_PROPERTIES = POSITION_PROPERTIES;
30803089
module.exports.POSITION_X_KEYWORDS = POSITION_X_KEYWORDS;
30813090
module.exports.POSITION_Y_KEYWORDS = POSITION_Y_KEYWORDS;
30823091
module.exports.QUARTER_TURN_ANGLE = QUARTER_TURN_ANGLE;
3092+
module.exports.RATIO_PROPERTIES = RATIO_PROPERTIES;
30833093
module.exports.REPEAT_STYLE_KEYWORDS = REPEAT_STYLE_KEYWORDS;
30843094
module.exports.REPEAT_STYLE_PROPERTIES = REPEAT_STYLE_PROPERTIES;
30853095
module.exports.RGB_TO_NAME = RGB_TO_NAME;

‎lib/css/syntax.js‎

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const GenericSourceProcessor = require("../util/SourceProcessor");
1010
const { makeCacheable } = require("../util/identifier");
1111
const {
1212
ABSOLUTE_UNIT_SCALE,
13+
ALPHA_VALUE_PROPERTIES,
1314
ANGLE_UNITS,
1415
BOX_FAMILY_PREFIX,
1516
BOX_LONGHANDS,
@@ -49,6 +50,7 @@ const {
4950
POSITION_PROPERTIES,
5051
POSITION_X_KEYWORDS,
5152
POSITION_Y_KEYWORDS,
53+
RATIO_PROPERTIES,
5254
REPEAT_STYLE_KEYWORDS,
5355
REPEAT_STYLE_PROPERTIES,
5456
RGB_TO_NAME,
@@ -6665,6 +6667,46 @@ const _numberFontShorthandWeight = (components) => {
66656667
// A component that is exactly zero, whatever unit the zero-unit drop left it in.
66666668
const _ZERO_COMPONENT_RE = /^[+-]?0(?:\.0*)?$/;
66676669

6670+
// A value that is one percentage and nothing else.
6671+
const _LONE_PERCENTAGE_RE = /^([+-]?)(\d*)(?:\.(\d*))?%$/;
6672+
6673+
/**
6674+
* Write an `<alpha-value>` percentage as the number naming the same quantity:
6675+
* the decimal point moved two places, which is exact where dividing is not.
6676+
* @param {string} property the lowercased property name
6677+
* @param {string} value the printed value
6678+
* @returns {string} the value, or the shorter number
6679+
*/
6680+
const _numberAlphaValue = (property, value) => {
6681+
if (!ALPHA_VALUE_PROPERTIES.has(property)) return value;
6682+
const parts = _LONE_PERCENTAGE_RE.exec(value);
6683+
if (parts === null) return value;
6684+
const digits = `00${parts[2]}`;
6685+
const shifted = _normalizeNumber(
6686+
`${parts[1]}${digits.slice(0, -2)}.${digits.slice(-2)}${parts[3] || ""}`
6687+
);
6688+
return shifted.length < value.length ? shifted : value;
6689+
};
6690+
6691+
// A ratio whose denominator is the `1` an omitted one means, taken as a whole
6692+
// component so the `1` of `2/10` is no match.
6693+
const _RATIO_OVER_ONE_RE = /(^|\s)((?:\d+\.?\d*|\.\d+))\s*\/\s*1(?=$|\s)/g;
6694+
6695+
/**
6696+
* Drop a `<ratio>`'s denominator where it is the 1 an omitted one means.
6697+
* @param {string} property the lowercased property name
6698+
* @param {string} value the printed value
6699+
* @returns {string} the value, its `/1` dropped
6700+
*/
6701+
const _dropRatioDenominator = (property, value) => {
6702+
// A substitution could expand to a number of its own, turning the `1` into
6703+
// the denominator of a ratio this does not see.
6704+
if (!RATIO_PROPERTIES.has(property) || _SUBSTITUTION_RE.test(value)) {
6705+
return value;
6706+
}
6707+
return value.replace(_RATIO_OVER_ONE_RE, "$1$2");
6708+
};
6709+
66686710
// A size whose omitted second value is `auto` (CSS Backgrounds 3 §3.9, CSS
66696711
// Masking 1 §4.5), not the first repeated as a box longhand's is.
66706712
const _AUTO_SECOND_VALUE_PROPERTIES = new Set(["background-size", "mask-size"]);
@@ -6700,6 +6742,13 @@ const _dropDefaultSecondValue = (property, value) => {
67006742
return changed ? layers.join(",") : value;
67016743
};
67026744

6745+
// A transform along x only -> the pair spelling whose second component is the
6746+
// 0 the one-axis call already means (CSS Transforms 1 §11).
6747+
const _X_AXIS_TRANSFORMS = new Map([
6748+
["translatex", "translate"],
6749+
["skewx", "skew"]
6750+
]);
6751+
67036752
const _TRANSFORM_CALL_RE = /^([a-z0-9]+)\((.*)\)$/i;
67046753

67056754
/**
@@ -7822,18 +7871,19 @@ const printer = (path, writer) => {
78227871
? _minifyEasingFunction(fn, inner)
78237872
: null;
78247873
if (easing !== null) return easing;
7825-
// `translateX(v)` is `translate(v)` — the second component defaults to 0.
7826-
// Only for one plain component: a substitution could expand to two, which
7827-
// `translateX` rejects and `translate` would accept, reviving a
7828-
// declaration the browser drops.
7874+
// `translateX(v)` is `translate(v)` and `skewX(a)` is `skew(a)` — the
7875+
// second component defaults to 0. Only for one plain component: a
7876+
// substitution could expand to two, which the one-axis spelling rejects
7877+
// and the pair would accept, reviving a declaration the browser drops.
7878+
const oneAxis = _X_AXIS_TRANSFORMS.get(fn);
78297879
if (
7880+
oneAxis !== undefined &&
78307881
path.inValue() &&
78317882
!_inSubstitutedValue &&
7832-
fn === "translatex" &&
78337883
!inner.includes(",") &&
78347884
!_SUBSTITUTION_RE.test(inner)
78357885
) {
7836-
return `translate(${inner})`;
7886+
return `${oneAxis}(${inner})`;
78377887
}
78387888
if (path.inValue() && !_inSubstitutedValue) {
78397889
// An argument that is the amount an omitted one means says nothing,
@@ -8038,7 +8088,11 @@ const printer = (path, writer) => {
80388088
fragments = fragments.map(_dropZeroLengthUnitInCall);
80398089
}
80408090
value = _join(fragments, !minify);
8041-
if (minify) value = _dropDefaultSecondValue(property, value);
8091+
if (minify) {
8092+
value = _dropDefaultSecondValue(property, value);
8093+
value = _numberAlphaValue(property, value);
8094+
value = _dropRatioDenominator(property, value);
8095+
}
80428096
}
80438097
}
80448098
const important = path.important() ? `${soft}!important` : "";

‎test/CssSyntax.unittest.js‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,7 +2827,10 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
28272827
["a{transform:scale3d(2,3,1)}", "a{transform:scale(2,3)}"],
28282828
// One reduction uncovers the next: the 2D call it leaves reduces too.
28292829
["a{transform:scale3d(1,1,1)}", "a{transform:scale(1)}"],
2830-
["a{transform:translate3d(-50%,0,0)}", "a{transform:translate(-50%)}"]
2830+
["a{transform:translate3d(-50%,0,0)}", "a{transform:translate(-50%)}"],
2831+
// `skewX(a)` is `skew(a)`: the second component defaults to 0.
2832+
["a{transform:skewX(10deg)}", "a{transform:skew(10deg)}"],
2833+
["a{transform:skewX(0)}", "a{transform:skew(0)}"]
28312834
])("%s", (css, expected) => {
28322835
expect(minify(css)).toBe(expected);
28332836
});
@@ -2842,7 +2845,9 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
28422845
["the axis is scaled", "a{transform:rotate3d(0,0,2,37deg)}"],
28432846
["the axis is negative", "a{transform:rotate3d(0,0,-1,37deg)}"],
28442847
["the vector names no axis", "a{transform:rotate3d(1,1,0,37deg)}"],
2845-
["the z factor scales", "a{transform:scale3d(2,3,4)}"]
2848+
["the z factor scales", "a{transform:scale3d(2,3,4)}"],
2849+
["a substitution could fill the second", "a{transform:skewX(var(--a))}"],
2850+
["the skew is along y", "a{transform:skewY(10deg)}"]
28462851
])("keeps it where %s", (_name, css) => {
28472852
expect(minify(css)).toBe(css);
28482853
});
@@ -2878,6 +2883,47 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
28782883
});
28792884
});
28802885

2886+
describe("an alpha value written as a percentage", () => {
2887+
it.each([
2888+
["a{opacity:100%}", "a{opacity:1}"],
2889+
["a{opacity:50%}", "a{opacity:.5}"],
2890+
["a{opacity:0%}", "a{opacity:0}"],
2891+
// The decimal point moves rather than dividing, so no digit is lost.
2892+
["a{opacity:33.33%}", "a{opacity:.3333}"],
2893+
["a{opacity:-50%}", "a{opacity:-.5}"],
2894+
["a{shape-image-threshold:12.5%}", "a{shape-image-threshold:.125}"]
2895+
])("%s", (css, expected) => {
2896+
expect(minify(css)).toBe(expected);
2897+
});
2898+
2899+
it.each([
2900+
["the number is no shorter", "a{opacity:5%}"],
2901+
["a substitution stands there", "a{opacity:var(--x)}"],
2902+
["the percentage means something else", "a{width:100%}"]
2903+
])("keeps the percentage where %s", (_name, css) => {
2904+
expect(minify(css)).toBe(css);
2905+
});
2906+
});
2907+
2908+
describe("a ratio's denominator of one", () => {
2909+
it.each([
2910+
["a{aspect-ratio:2/1}", "a{aspect-ratio:2}"],
2911+
["a{aspect-ratio:2 / 1}", "a{aspect-ratio:2}"],
2912+
["a{aspect-ratio:auto 3/1}", "a{aspect-ratio:auto 3}"],
2913+
["a{aspect-ratio:1/1}", "a{aspect-ratio:1}"]
2914+
])("%s", (css, expected) => {
2915+
expect(minify(css)).toBe(expected);
2916+
});
2917+
2918+
it.each([
2919+
["the denominator is another number", "a{aspect-ratio:2/10}"],
2920+
["a substitution stands there", "a{aspect-ratio:var(--x)/1}"],
2921+
["the property takes no ratio", "a{width:2/1}"]
2922+
])("keeps it where %s", (_name, css) => {
2923+
expect(minify(css)).toBe(css);
2924+
});
2925+
});
2926+
28812927
describe("a linear gradient's default direction", () => {
28822928
it.each([
28832929
[

‎test/CssValueSyntax.unittest.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ const {
66
acceptedValues,
77
assertClassesArePrintable,
88
checkStatedClassSpellings,
9+
collectAlphaValueProperties,
910
collectData,
1011
collectGradientFunctions,
1112
collectMergeableAtRules,
13+
collectRatioProperties,
1214
isSpelledSyntax,
1315
parseValueSyntax,
1416
shorthandSlots,
@@ -448,6 +450,39 @@ describe("CssValueSyntax", () => {
448450
});
449451
});
450452

453+
describe("collectAlphaValueProperties", () => {
454+
it("names the properties whose whole value is that alternation", () => {
455+
expect(collectAlphaValueProperties()).toEqual([
456+
"opacity",
457+
"shape-image-threshold"
458+
]);
459+
});
460+
461+
it("follows a name through to what states the alternation", () => {
462+
expect(
463+
collectAlphaValueProperties(
464+
{ a: { syntax: "<one>" } },
465+
{ one: { syntax: "<number> | <percentage>" } }
466+
)
467+
).toEqual(["a"]);
468+
});
469+
470+
it("names none where the chain of names has no end", () => {
471+
expect(
472+
collectAlphaValueProperties(
473+
{ a: { syntax: "<one>" }, b: { syntax: "<gone>" }, c: {} },
474+
{ one: { syntax: "<one>" } }
475+
)
476+
).toEqual([]);
477+
});
478+
});
479+
480+
describe("collectRatioProperties", () => {
481+
it("names the properties whose grammar reaches a `<ratio>`", () => {
482+
expect(collectRatioProperties()).toEqual(["aspect-ratio"]);
483+
});
484+
});
485+
451486
describe("the guards on what those datasets still publish", () => {
452487
it("accepts a `syntaxes.json` that states no class the SUPPLEMENT does", () => {
453488
expect(() => checkStatedClassSpellings({})).not.toThrow();

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Array [
1919
"/*! convert-values: should trim trailing zeros */.t10{width:109px}",
2020
"/*! convert-values: should trim trailing zeros (2) */.t11{width:0}",
2121
"/*! convert-values: should trim trailing zeros (3) */.t12{width:100%}",
22-
"/*! convert-values: should strip the unit from a zero percentage */.t13{opacity:0%}",
22+
"/*! convert-values: should strip the unit from a zero percentage */.t13{opacity:0}",
2323
"/*! convert-values: should normalize a bare decimal point */.t14{opacity:1.}.t15{opacity:0}",
2424
"/*! convert-values: should normalize a bare decimal point with a unit */.t16{width:10.px}.t17{width:0}",
2525
"/*! convert-values: should normalize inside calc */.t18{width:calc(10.px + 0px)}",
@@ -165,5 +165,11 @@ Array [
165165
"/*! declined: a substitution could make the \`auto\` a third value */.t74c{background-size:var(--w) auto}",
166166
"/*! one reduction uncovers the next, down to the shortest call */.t75{transform:translate(-50%)}.t75a{transform:scale(1)}",
167167
"/*! declined: a z factor that scales keeps the 3D call */.t75b{transform:scale3d(2,3,4)}",
168+
"/*! a skew along x is the pair whose second component is the 0 it means */.t76{transform:skew(10deg)}",
169+
"/*! declined: a substitution could fill the second component */.t76a{transform:skewX(var(--a))}",
170+
"/*! an alpha percentage is the number a hundredth of it names */.t77{opacity:1}.t77a{opacity:.3333}.t77b{shape-image-threshold:.125}",
171+
"/*! declined: the number is no shorter than the percentage */.t77c{opacity:5%}",
172+
"/*! a ratio's denominator of 1 is the one an omitted denominator means */.t78{aspect-ratio:2}.t78a{aspect-ratio:auto 3}",
173+
"/*! declined: another denominator is another ratio */.t78b{aspect-ratio:2/10}",
168174
]
169175
`;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Array [
1919
"/*! convert-values: should trim trailing zeros */.t10{width:109px}",
2020
"/*! convert-values: should trim trailing zeros (2) */.t11{width:0}",
2121
"/*! convert-values: should trim trailing zeros (3) */.t12{width:100%}",
22-
"/*! convert-values: should strip the unit from a zero percentage */.t13{opacity:0%}",
22+
"/*! convert-values: should strip the unit from a zero percentage */.t13{opacity:0}",
2323
"/*! convert-values: should normalize a bare decimal point */.t14{opacity:1.}.t15{opacity:0}",
2424
"/*! convert-values: should normalize a bare decimal point with a unit */.t16{width:10.px}.t17{width:0}",
2525
"/*! convert-values: should normalize inside calc */.t18{width:calc(10.px + 0px)}",
@@ -165,5 +165,11 @@ Array [
165165
"/*! declined: a substitution could make the \`auto\` a third value */.t74c{background-size:var(--w) auto}",
166166
"/*! one reduction uncovers the next, down to the shortest call */.t75{transform:translate(-50%)}.t75a{transform:scale(1)}",
167167
"/*! declined: a z factor that scales keeps the 3D call */.t75b{transform:scale3d(2,3,4)}",
168+
"/*! a skew along x is the pair whose second component is the 0 it means */.t76{transform:skew(10deg)}",
169+
"/*! declined: a substitution could fill the second component */.t76a{transform:skewX(var(--a))}",
170+
"/*! an alpha percentage is the number a hundredth of it names */.t77{opacity:1}.t77a{opacity:.3333}.t77b{shape-image-threshold:.125}",
171+
"/*! declined: the number is no shorter than the percentage */.t77c{opacity:5%}",
172+
"/*! a ratio's denominator of 1 is the one an omitted denominator means */.t78{aspect-ratio:2}.t78a{aspect-ratio:auto 3}",
173+
"/*! declined: another denominator is another ratio */.t78b{aspect-ratio:2/10}",
168174
]
169175
`;

0 commit comments

Comments
 (0)