Skip to content

Commit 6b66852

Browse files
fix(css): minify a second time to the same output
- note every qualified rule for the same-selector drop, flat or not, and read the earlier one as flat only when it is rewritten - drop only after every copy in the piece is taken back - leave a named layer's rules alone, as the gathered blocks are - write background:transparent none as none too
1 parent 0a59be3 commit 6b66852

5 files changed

Lines changed: 57 additions & 23 deletions

File tree

‎lib/css/syntax-parser.js‎

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5996,8 +5996,12 @@ const _noteSpans = (writer, taken, spans, chain) => {
59965996
const before = rules.get(span.key);
59975997
if (before !== undefined) _cutSpan(writer, before.taken, before.span);
59985998
rules.set(span.key, { taken, span });
5999-
// A distant merge moves a block up past others, out of the order read here.
6000-
if (_S._transforms.removeDeadRules && !_mergingDistantRules) {
5999+
}
6000+
// After every copy is taken back, so an earlier rule a cut here left flat is
6001+
// read as flat. A distant merge moves a block up past others, out of this order.
6002+
if (!_S._transforms.removeDeadRules || _mergingDistantRules) return;
6003+
for (const span of spans) {
6004+
if (span.key !== UNKEYED && span.len !== 0) {
60016005
_shadowEarlierRule(writer, taken, span);
60026006
}
60036007
}
@@ -6007,11 +6011,13 @@ const _noteSpans = (writer, taken, spans, chain) => {
60076011
const _ROLLBACK_RE = /^revert(?:-[a-z]+)?$/i;
60086012

60096013
// A keyframe's selector is an offset, and two blocks at one offset are not read
6010-
// as a cascade by every engine, so no keyframe is shadowed.
6011-
const _KEYFRAMES_SCOPE_RE = /@(?:-[a-z]+-)?keyframes/i;
6014+
// as a cascade by every engine, so no keyframe is shadowed. A named layer's
6015+
// blocks are gathered into one without their rules noted, so none is there.
6016+
const _NO_SHADOW_SCOPE_RE = /@(?:-[a-z]+-)?keyframes|@layer/i;
60126017

60136018
/**
6014-
* The declarations of a printed block body, split on the `;` between them.
6019+
* The declarations of a printed block body, split on the `;` between them; a
6020+
* nested block is a piece of its own.
60156021
* @param {string} body the text between a rule's braces
60166022
* @returns {string[]} each declaration, without its `;`
60176023
*/
@@ -6029,10 +6035,23 @@ const _splitPrintedDeclarations = (body) => {
60296035
if (code === quote) quote = 0;
60306036
} else if (code === CC_QUOTATION_MARK || code === CC_APOSTROPHE) {
60316037
quote = code;
6032-
} else if (code === CC_LEFT_PARENTHESIS || code === CC_LEFT_SQUARE) {
6038+
} else if (
6039+
code === CC_LEFT_PARENTHESIS ||
6040+
code === CC_LEFT_SQUARE ||
6041+
code === CC_LEFT_CURLY
6042+
) {
60336043
depth++;
6034-
} else if (code === CC_RIGHT_PARENTHESIS || code === CC_RIGHT_SQUARE) {
6044+
} else if (
6045+
code === CC_RIGHT_PARENTHESIS ||
6046+
code === CC_RIGHT_SQUARE ||
6047+
code === CC_RIGHT_CURLY
6048+
) {
60356049
depth--;
6050+
// A nested block ends a piece of its own, which no `;` follows.
6051+
if (code === CC_RIGHT_CURLY && depth === 0) {
6052+
out.push(body.slice(start, i + 1));
6053+
start = i + 1;
6054+
}
60366055
} else if (code === CC_SEMICOLON && depth === 0) {
60376056
out.push(body.slice(start, i));
60386057
start = i + 1;
@@ -6080,13 +6099,12 @@ const _readWherever = (values, earlier) => {
60806099
const _shadowEarlierRule = (writer, taken, span) => {
60816100
const text = taken.text.slice(span.at, span.at + span.len);
60826101
const open = text.indexOf("{");
6083-
// Only a flat qualified rule: a nested block or an at-rule is no one cascade.
6102+
// Only a qualified rule: an at-rule is no one cascade. One a later cut
6103+
// leaves flat is read as one, so every rule is noted, flat or not.
60846104
if (
60856105
open <= 0 ||
60866106
text.charCodeAt(0) === CC_AT_SIGN ||
6087-
text.includes("{", open + 1) ||
6088-
text.indexOf("}") !== text.length - 1 ||
6089-
_KEYFRAMES_SCOPE_RE.test(span.scope.prefix)
6107+
_NO_SHADOW_SCOPE_RE.test(span.scope.prefix)
60906108
) {
60916109
return;
60926110
}
@@ -6101,9 +6119,11 @@ const _shadowEarlierRule = (writer, taken, span) => {
61016119
// them, and whether one of those is `!important`.
61026120
/** @type {Map<string, { values: Set<string>, read: boolean, important: boolean }>} */
61036121
const later = new Map();
6122+
// Its own declarations: a rule nested in it matches other elements.
61046123
for (const declaration of _splitPrintedDeclarations(
61056124
text.slice(open + 1, -1)
61066125
)) {
6126+
if (declaration.includes("{")) continue;
61076127
const colon = declaration.indexOf(":");
61086128
if (colon <= 0) return;
61096129
const name = _cascadeName(declaration);
@@ -6137,6 +6157,14 @@ const _shadowEarlierRule = (writer, taken, span) => {
61376157
) {
61386158
continue;
61396159
}
6160+
// A rule still holding a nested one is left whole: only a flat one is
6161+
// written again declaration by declaration.
6162+
if (
6163+
before.includes("{", open + 1) ||
6164+
before.indexOf("}") !== before.length - 1
6165+
) {
6166+
continue;
6167+
}
61406168
/** @type {string[]} */
61416169
const kept = [];
61426170
let dropped = false;

‎lib/css/syntax-printer.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4274,15 +4274,6 @@ const _collapseShorthand = (path, property, node, writer) => {
42744274
if (property === "animation") {
42754275
return _collapseAnimationLayers(written);
42764276
}
4277-
// A transparent color alone is every slot at its initial, as `none` is, and
4278-
// that spelling is shorter than `#0000`.
4279-
if (
4280-
property === "background" &&
4281-
written.length === 1 &&
4282-
_TRANSPARENT_SPELLINGS.has(toLowerCaseIfNeeded(written[0]))
4283-
) {
4284-
return ["none"];
4285-
}
42864277
// A slot holding its own initial goes first, so what follows reads the
42874278
// components that are left rather than the ones the author wrote.
42884279
const readable = _shorthandSlotsReadable(property);
@@ -4296,6 +4287,15 @@ const _collapseShorthand = (path, property, node, writer) => {
42964287
layered
42974288
: layered;
42984289
let components = dropped === null ? written : dropped;
4290+
// A transparent color alone, as written or once the initials around it went,
4291+
// is every slot at its initial, as `none` is — shorter than `#0000`.
4292+
if (
4293+
property === "background" &&
4294+
components.length === 1 &&
4295+
_TRANSPARENT_SPELLINGS.has(toLowerCaseIfNeeded(components[0]))
4296+
) {
4297+
return ["none"];
4298+
}
42994299
// A component spelling the property's own initial says nothing beside another:
43004300
// omitting the group it belongs to leaves exactly that keyword.
43014301
const omittable = OMITTABLE_INITIAL_KEYWORDS.get(property);

‎test/configCases/css/minify-chrome-100/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ exports[`ConfigCacheTestCases css minify-chrome-100 minify-chrome-100 should com
88

99
exports[`ConfigCacheTestCases css minify-chrome-100 minify-chrome-100 should compile: minify-nested-combinator 1`] = `".a>.child{top:0}.b+.next{top:1px}.c~.later{top:2px}.d>.child{top:3px}.e || .col{top:4px}.f .inside{top:5px}:is(.g,.h)>.child{top:6px}.i>.child{top:7px}.j .z,.j+.y,.j>.x{left:0}"`;
1010

11-
exports[`ConfigCacheTestCases css minify-chrome-100 minify-chrome-100 should compile: minify-nested-idempotent 1`] = `".a{width:1px}.mid{color:teal}.a{height:2px}.a .x{color:red}.p,.q .r{color:blue}.q .s{color:green}.t{--c:1;color:navy}.t .u{color:navy}.v,.v .w{top:0}.z{top:1px}.y{--c:1;color:olive}.y{.y{color:olive}--c:1;color:olive}.k .l,.l .m{left:0}@media print{.k .l{left:0}}.e,.e f: (1) [2]{g:h}.e .i{top:0}"`;
11+
exports[`ConfigCacheTestCases css minify-chrome-100 minify-chrome-100 should compile: minify-nested-idempotent 1`] = `".a{width:1px}.mid{color:teal}.a{height:2px}.a .x{color:red}.p,.q .r{color:blue}.q .s{color:green}.t{--c:1;color:navy}.t .u{color:navy}.v,.v .w{top:0}.z{top:1px}.y{.y{color:olive}--c:1;color:olive}.k .l,.l .m{left:0}@media print{.k .l{left:0}}.e,.e f: (1) [2]{g:h}.e .i{top:0}"`;

‎test/configCases/css/minify-chrome-100/__snapshots__/ConfigTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ exports[`ConfigTestCases css minify-chrome-100 minify-chrome-100 should compile:
88

99
exports[`ConfigTestCases css minify-chrome-100 minify-chrome-100 should compile: minify-nested-combinator 1`] = `".a>.child{top:0}.b+.next{top:1px}.c~.later{top:2px}.d>.child{top:3px}.e || .col{top:4px}.f .inside{top:5px}:is(.g,.h)>.child{top:6px}.i>.child{top:7px}.j .z,.j+.y,.j>.x{left:0}"`;
1010

11-
exports[`ConfigTestCases css minify-chrome-100 minify-chrome-100 should compile: minify-nested-idempotent 1`] = `".a{width:1px}.mid{color:teal}.a{height:2px}.a .x{color:red}.p,.q .r{color:blue}.q .s{color:green}.t{--c:1;color:navy}.t .u{color:navy}.v,.v .w{top:0}.z{top:1px}.y{--c:1;color:olive}.y{.y{color:olive}--c:1;color:olive}.k .l,.l .m{left:0}@media print{.k .l{left:0}}.e,.e f: (1) [2]{g:h}.e .i{top:0}"`;
11+
exports[`ConfigTestCases css minify-chrome-100 minify-chrome-100 should compile: minify-nested-idempotent 1`] = `".a{width:1px}.mid{color:teal}.a{height:2px}.a .x{color:red}.p,.q .r{color:blue}.q .s{color:green}.t{--c:1;color:navy}.t .u{color:navy}.v,.v .w{top:0}.z{top:1px}.y{.y{color:olive}--c:1;color:olive}.k .l,.l .m{left:0}@media print{.k .l{left:0}}.e,.e f: (1) [2]{g:h}.e .i{top:0}"`;

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5273,6 +5273,10 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
52735273
expect(minify("a{color:red}/*! keep */b{x:1}a{color:blue}")).toBe(
52745274
"a{color:red}/*! keep */b{x:1}a{color:blue}"
52755275
);
5276+
// A later rule's own declarations shadow, whatever it nests.
5277+
expect(minify(".a{x:1;y:2}.b{c:d}.a{.c{z:3}x:4}")).toBe(
5278+
".a{y:2}.b{c:d}.a{.c{z:3}x:4}"
5279+
);
52765280
// Every earlier rule of the selector, not only the latest.
52775281
expect(
52785282
minify("a{color:red;top:2px}b{x:1}a{color:blue}c{y:1}a{top:1px}")
@@ -12718,7 +12722,7 @@ describe("CssSyntax minify — what a nesting lowering writes is minified once",
1271812722
browsers
1271912723
)
1272012724
).toBe(
12721-
".z{top:1px}.y{--c:1;color:olive}.y{.y{color:olive}--c:1;color:olive}"
12725+
".z{top:1px}.y{.y{color:olive}--c:1;color:olive}"
1272212726
);
1272312727
}
1272412728
);
@@ -15960,6 +15964,8 @@ describe("CssSyntax minify — layered and positional spellings", () => {
1596015964
["a{animation:x 1s,y 2s;animation-delay:.5s,1s}", "a{animation:x 1s.5s,y 2s 1s}"],
1596115965
["a{background:transparent}", "a{background:none}"],
1596215966
["a{background:#0000!important}", "a{background:none!important}"],
15967+
// The same once the initial image beside it goes.
15968+
["a{background:transparent none}", "a{background:none}"],
1596315969
// A type name leads its attribute variants.
1596415970
["[type=button],button{x:1}", "button,[type=button]{x:1}"],
1596515971
// An escaped `{` and a `[` meet at one place, and still sort apart.

0 commit comments

Comments
 (0)