@@ -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) => {
60076011const _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) => {
60806099const _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;
0 commit comments