@@ -2606,6 +2606,14 @@ const namesParent = (selector) => { |
2606 | 2606 | return false; |
2607 | 2607 | }; |
2608 | 2608 |
|
| 2609 | +/** |
| 2610 | + * Whether a selector list holds an empty selector, which makes it invalid. |
| 2611 | + * @param {string} prelude a printed selector list |
| 2612 | + * @returns {boolean} true when one of its selectors is empty |
| 2613 | + */ |
| 2614 | +const holdsEmptySelector = (prelude) => |
| 2615 | + splitSelectorList(prelude).some((one) => trimCssWhiteSpace(one).length === 0); |
| 2616 | + |
2609 | 2617 | /** |
2610 | 2618 | * The prelude a rule nested in one written `parent` says on its own, with every |
2611 | 2619 | * `&` written out and a selector naming none made the descendant it means. |
@@ -2785,8 +2793,17 @@ const writeNested = ( |
2785 | 2793 | // Read only once a rule written in this one asks. |
2786 | 2794 | /** @type {string | undefined} */ |
2787 | 2795 | let against; |
| 2796 | + // An empty selector makes a list invalid, and with it every rule written in |
| 2797 | + // it, which no hoisted copy may bring back to life. |
| 2798 | + /** @type {boolean | undefined} */ |
| 2799 | + let invalid; |
2788 | 2800 | for (let i = S._pendingNested.length - 1; i >= 0; i--) { |
2789 | 2801 | if (S._pendingNested[i].owner !== node) continue; |
| 2802 | + if (invalid === undefined) invalid = holdsEmptySelector(prelude); |
| 2803 | + if (invalid || holdsEmptySelector(S._pendingNested[i].prelude)) { |
| 2804 | + S._pendingNested.splice(i, 1); |
| 2805 | + continue; |
| 2806 | + } |
2790 | 2807 | if (against === undefined) { |
2791 | 2808 | against = |
2792 | 2809 | path.parent !== null && path.type(path.parent) !== NodeType.Stylesheet |
@@ -4522,6 +4539,40 @@ const positionPercentage = (written, axis) => { |
4522 | 4539 | return lower.endsWith("%") ? lower : null; |
4523 | 4540 | }; |
4524 | 4541 |
|
| 4542 | +/** |
| 4543 | + * `dropLayerInitials` over each layer a comma parts, whose position and boxes say |
| 4544 | + * nothing of the next one's. A layer it would empty is kept as written. |
| 4545 | + * @param {string} property the declaration's lowercased property name |
| 4546 | + * @param {string[]} components the value's top-level components |
| 4547 | + * @returns {string[] | null} the fragments to join, or `null` to keep the value |
| 4548 | + */ |
| 4549 | +const dropEachLayerInitials = (property, components) => { |
| 4550 | + const layers = valueLayers(components); |
| 4551 | + if (layers.length === 1) return dropLayerInitials(property, components); |
| 4552 | + // A quoted string could carry a comma the layer split would part. |
| 4553 | + for (const component of components) { |
| 4554 | + if (component.includes('"') || component.includes("'")) return null; |
| 4555 | + } |
| 4556 | + /** @type {string[]} */ |
| 4557 | + const out = []; |
| 4558 | + let changed = false; |
| 4559 | + for (let i = 0; i < layers.length; i++) { |
| 4560 | + let layer = layers[i]; |
| 4561 | + // An empty layer makes the value invalid, which no rewrite may make valid. |
| 4562 | + if (layer.length === 0) return null; |
| 4563 | + const dropped = dropLayerInitials(property, layer); |
| 4564 | + if (dropped !== null && dropped.length !== 0) { |
| 4565 | + layer = dropped; |
| 4566 | + changed = true; |
| 4567 | + } |
| 4568 | + for (let j = 0; j < layer.length; j++) { |
| 4569 | + const last = j === layer.length - 1 && i !== layers.length - 1; |
| 4570 | + out.push(last ? `${layer[j]},` : layer[j]); |
| 4571 | + } |
| 4572 | + } |
| 4573 | + return changed ? out : null; |
| 4574 | +}; |
| 4575 | + |
4525 | 4576 | // The spellings of the transparent black a `background` color starts as. |
4526 | 4577 | const TRANSPARENT_SPELLINGS = new Set(["transparent", "#0000", "#00000000"]); |
4527 | 4578 |
|
@@ -4860,7 +4911,7 @@ const collapseShorthand = (path, property, node, writer) => { |
4860 | 4911 | // A family's slots are read by what each takes rather than by an enumerated |
4861 | 4912 | // list, answering for a color and a length where the keyword table cannot. A |
4862 | 4913 | // layered shorthand reads position, size and boxes positionally. |
4863 | | - const layered = dropLayerInitials(property, written); |
| 4914 | + const layered = dropEachLayerInitials(property, written); |
4864 | 4915 | let dropped = readable |
4865 | 4916 | ? dropFamilyInitials(property, written) || |
4866 | 4917 | dropInitialKeywords(property, layered || written) || |
@@ -5396,6 +5447,28 @@ const printAttributeSelector = (path, children, writer) => { |
5396 | 5447 | return parts; |
5397 | 5448 | }; |
5398 | 5449 |
|
| 5450 | +/** |
| 5451 | + * A block or function as written, its comments' queued copies claimed, where its |
| 5452 | + * source closes it; one the input ran out in prints as usual, so the walk closes it. |
| 5453 | + * @param {CssPath} path the accessor positioned on the node |
| 5454 | + * @param {PrintContext} writer the print context |
| 5455 | + * @param {string} close the token that closes it |
| 5456 | + * @returns {string | null} the source, or null where the input left it open |
| 5457 | + */ |
| 5458 | +const writtenWhole = (path, writer, close) => { |
| 5459 | + // A node holding a visitor's replacement prints as its children printed. |
| 5460 | + if (S._transforms === S._plainTransforms) return null; |
| 5461 | + const source = printedSource(path.node); |
| 5462 | + if (!source.endsWith(close)) return null; |
| 5463 | + // A last child ending where the node does holds the closing token that is there. |
| 5464 | + const count = path.childCount(); |
| 5465 | + if (count !== 0 && path.end(path.child(count - 1)) === path.end()) { |
| 5466 | + return null; |
| 5467 | + } |
| 5468 | + writer.takeInserts(path.start(), path.end()); |
| 5469 | + return source; |
| 5470 | +}; |
| 5471 | + |
5399 | 5472 | /** |
5400 | 5473 | * The `printer` case for a function. |
5401 | 5474 | * @param {CssPath} path the accessor positioned on the finished node |
@@ -5408,24 +5481,17 @@ const printFunctionNode = (path, writer, minify) => { |
5408 | 5481 | // An escape spells the same name (§4.3.7): `c\61 lc(` is `calc()`, so |
5409 | 5482 | // every table below reads what it spells. |
5410 | 5483 | const name = written.includes("\\") ? unescapeIdentifier(written) : written; |
5411 | | - // A container style query asks whether a custom property's value is the one |
5412 | | - // written here, comparing token streams as written (CSS Conditional 5 §5), |
5413 | | - // so a squeezed whitespace run asks another question. Nothing is rewritten. |
| 5484 | + // A style query compares token streams as written (CSS Conditional 5 §5), and a |
| 5485 | + // beautified at-rule prelude keeps each function as written, as its blocks. |
5414 | 5486 | if ( |
5415 | | - minify && |
5416 | | - !path.within(NODE_TYPE_DECLARATION) && |
5417 | | - S._inMediaConditionPrelude && |
5418 | | - asciiCaseInsensitiveMatch(name, "style") |
| 5487 | + minify |
| 5488 | + ? !path.within(NODE_TYPE_DECLARATION) && |
| 5489 | + S._inMediaConditionPrelude && |
| 5490 | + asciiCaseInsensitiveMatch(name, "style") |
| 5491 | + : S._inAtRulePrelude && !S._inSupportsSelector |
5419 | 5492 | ) { |
5420 | | - const source = printedSource(path.node); |
5421 | | - // Unclosed at EOF, the source carries no `)` for the walk to have |
5422 | | - // closed - printed as usual so the parenthesis is written back. |
5423 | | - if (source.endsWith(")")) { |
5424 | | - // Written whole, comments included, so the queued copies are claimed |
5425 | | - // here rather than flushed a second time after the rule. |
5426 | | - writer.takeInserts(path.start(), path.end()); |
5427 | | - return source; |
5428 | | - } |
| 5493 | + const whole = writtenWhole(path, writer, ")"); |
| 5494 | + if (whole !== null) return whole; |
5429 | 5495 | } |
5430 | 5496 | let inner; |
5431 | 5497 | if (path.childCount() === 1) { |
@@ -5463,7 +5529,13 @@ const printFunctionNode = (path, writer, minify) => { |
5463 | 5529 | } |
5464 | 5530 | dropImpliedUniversalSelector(parts); |
5465 | 5531 | } |
5466 | | - inner = join(parts, !minify, minify ? trim : TRIM_NOTHING); |
| 5532 | + // Beautified, a selector's function keeps its tokens as its blocks do. |
| 5533 | + inner = join( |
| 5534 | + parts, |
| 5535 | + !minify, |
| 5536 | + minify ? trim : TRIM_NOTHING, |
| 5537 | + !minify && !path.within(NODE_TYPE_DECLARATION) |
| 5538 | + ); |
5467 | 5539 | // A leading `+` in An+B is a sign, not a combinator: `+ n` is no |
5468 | 5540 | // argument an engine reads, and `+n` would be one. |
5469 | 5541 | if (minify && NTH_PSEUDO_FUNCTIONS.has(lower)) { |
@@ -5921,13 +5993,23 @@ const printIdentNode = (path, minify) => { |
5921 | 5993 | const printSimpleBlockNode = (path, writer, minify) => { |
5922 | 5994 | const open = blockTokenOf(path.node); |
5923 | 5995 | const close = open === "(" ? ")" : open === "[" ? "]" : "}"; |
| 5996 | + // Beautified, an at-rule prelude's block prints as written: an engine keeps the |
| 5997 | + // text of a condition it does not know, whitespace and comments included. |
| 5998 | + if (!minify && S._inAtRulePrelude && !S._inSupportsSelector) { |
| 5999 | + const whole = writtenWhole(path, writer, close); |
| 6000 | + if (whole !== null) return whole; |
| 6001 | + } |
5924 | 6002 | // Outside a declaration value a `[...]` is an attribute selector and a `(...)` |
5925 | 6003 | // a query condition. Inside one they are a grid line-name list and a |
5926 | 6004 | // `calc()` sub-expression, where neither of those rewrites holds. |
5927 | 6005 | const structural = minify && !path.within(NODE_TYPE_DECLARATION); |
5928 | 6006 | /** @type {string[]} */ |
5929 | 6007 | let parts; |
5930 | | - if (structural && open === "[") { |
| 6008 | + if ( |
| 6009 | + structural && |
| 6010 | + open === "[" && |
| 6011 | + (!S._inAtRulePrelude || S._inSupportsSelector) |
| 6012 | + ) { |
5931 | 6013 | parts = printAttributeSelector(path, path.children(), writer); |
5932 | 6014 | } else { |
5933 | 6015 | // One array, rather than a child list and a mapped copy of it. |
@@ -5954,7 +6036,13 @@ const printSimpleBlockNode = (path, writer, minify) => { |
5954 | 6036 | trim = inScopePrelude(path) ? TRIM_COMBINATORS : TRIM_CONDITIONS; |
5955 | 6037 | if (trim === TRIM_COMBINATORS) foldPseudoNames(parts); |
5956 | 6038 | } |
5957 | | - const inner = join(parts, !minify, trim); |
| 6039 | + // Beautified, a selector's block keeps its tokens: no space where a comment was. |
| 6040 | + const inner = join( |
| 6041 | + parts, |
| 6042 | + !minify, |
| 6043 | + trim, |
| 6044 | + !minify && !path.within(NODE_TYPE_DECLARATION) |
| 6045 | + ); |
5958 | 6046 | return `${open}${inner}${close}`; |
5959 | 6047 | }; |
5960 | 6048 |
|
@@ -6629,6 +6717,8 @@ const printer = (path, writer) => { |
6629 | 6717 | if (minify) S._blockSpans.push(NO_BLOCK_ENTRY); |
6630 | 6718 | { |
6631 | 6719 | const raw = printedSource(path.node); |
| 6720 | + // Written whole, comments included, so the queued copies are claimed. |
| 6721 | + writer.takeInserts(path.start(), path.end()); |
6632 | 6722 | let rest = path.end(); |
6633 | 6723 | while ( |
6634 | 6724 | rest < S._input.length && |
|