@@ -1288,6 +1288,10 @@ const _COLOR_SCHEME_RE = /(?:^|[;{}])color-scheme:([^;{}]*)/i;
12881288// defers to.
12891289const _PREFERS_DARK = "@media (prefers-color-scheme:dark)" ;
12901290
1291+ // Stands for the rule's own prelude in a block the hoist holds, filled in once
1292+ // the run settles it: a NUL is never printed, the tokenizer replacing it.
1293+ const _OWN_PRELUDE = "\0" ;
1294+
12911295/**
12921296 * Whether a value names a color function no target reads, wherever it stands.
12931297 * @param {string } value the value, as printed
@@ -2017,10 +2021,19 @@ const _holdsNested = (node) => {
20172021 * @param {string } prelude the rule's printed prelude
20182022 * @param {string | null } rest the rule's own block, or null where it writes none
20192023 * @param {boolean } hoistable false where the rule is several rules with preludes of their own, which stay where they were written
2020- * @param {readonly string[]= } copies the preludes a prefix copy wrote it as, each carrying this rule's own block
2024+ * @param {readonly string[]= } copies the preludes a prefix copy wrote it as
2025+ * @param {string= } copyRest the block each of those carries, as a prefix copy writes it
20212026 * @returns {string | null } the text to write after the rule, or null when the rule goes on the list itself
20222027 */
2023- const _writeNested = ( path , node , prelude , rest , hoistable , copies ) => {
2028+ const _writeNested = (
2029+ path ,
2030+ node ,
2031+ prelude ,
2032+ rest ,
2033+ hoistable ,
2034+ copies ,
2035+ copyRest
2036+ ) => {
20242037 // Only what was written in this rule: the walk is post-order, so an earlier
20252038 // sibling's rules are on the list too, waiting for the parent they share.
20262039 /** @type {{ prelude: string, rest: string, owner: Node | null, apart?: boolean }[] } */
@@ -2040,7 +2053,12 @@ const _writeNested = (path, node, prelude, rest, hoistable, copies) => {
20402053 // written, rather than the whole group staying nested.
20412054 if ( rest !== null && copies !== undefined ) {
20422055 for ( const copy of copies ) {
2043- _pendingNested . push ( { prelude : copy , rest, owner, apart : true } ) ;
2056+ _pendingNested . push ( {
2057+ prelude : copy ,
2058+ rest : /** @type {string } */ ( copyRest ) ,
2059+ owner,
2060+ apart : true
2061+ } ) ;
20442062 }
20452063 }
20462064 if ( rest !== null ) {
@@ -2105,7 +2123,12 @@ const _writeNested = (path, node, prelude, rest, hoistable, copies) => {
21052123 */
21062124const _flushRun = ( run ) => {
21072125 const prelude = _settledPrelude ( run ) ;
2108- const text = prelude + run . rest ;
2126+ const at = run . rest . indexOf ( _OWN_PRELUDE ) ;
2127+ const text =
2128+ prelude +
2129+ ( at === - 1
2130+ ? run . rest
2131+ : run . rest . slice ( 0 , at ) + prelude + run . rest . slice ( at + 1 ) ) ;
21092132 _nestedRuns . push ( { text, prelude : prelude . length } ) ;
21102133 return text ;
21112134} ;
@@ -4628,11 +4651,17 @@ const printer = (path, writer) => {
46284651 // toggle's last is the one this block now ends on.
46294652 scheme = `${ body } ;${ toggle . toggle . slice ( 0 , - 1 ) } ` ;
46304653 if ( toggle . flip ) {
4631- schemeCopy = `${ _PREFERS_DARK } ${ soft } {${ prelude } ${ soft } {${ LIGHT_PROPERTY } :;${ DARK_PROPERTY } :initial}}` ;
4654+ schemeCopy = `${ _PREFERS_DARK } ${ soft } {${ _OWN_PRELUDE } ${ soft } {${ LIGHT_PROPERTY } :;${ DARK_PROPERTY } :initial}}` ;
46324655 }
46334656 }
46344657 }
4635- const rest = `${ soft } {${ scheme } ${ nl } }${ schemeCopy } ` ;
4658+ // The hoist's copy names the prelude as the run settles it, since one
4659+ // taken out is resolved against its parent and may be joined into a list.
4660+ const ownRest = `${ soft } {${ scheme } ${ nl } }${ schemeCopy } ` ;
4661+ const rest =
4662+ schemeCopy === ""
4663+ ? ownRest
4664+ : ownRest . replace ( _OWN_PRELUDE , ( ) => prelude ) ;
46364665 const text = `${ prelude } ${ rest } ` ;
46374666 // Prefix copies turn one rule into several, so a rewritten rule is not
46384667 // offered for joining: its text no longer describes a single block. Each
@@ -4691,9 +4720,10 @@ const printer = (path, writer) => {
46914720 path ,
46924721 /** @type {Node } */ ( _S . _currentNode ) ,
46934722 prelude ,
4694- rest ,
4723+ ownRest ,
46954724 out === text || copies !== undefined ,
4696- copies
4725+ copies ,
4726+ `${ soft } {${ body } }`
46974727 ) ;
46984728 if ( nested === null ) return "" ;
46994729 if ( out !== text ) return out + nested ;
0 commit comments