@@ -162,6 +162,7 @@ const {
162162 _appendChildTexts,
163163 _atKeyword,
164164 _blockSpans,
165+ _canonicalSelectorList,
165166 _collapseBox,
166167 _composeBlockBody,
167168 _customMedia,
@@ -1932,6 +1933,10 @@ const _resolveNestedPrelude = (prelude, parent) => {
19321933 return written . join ( "," ) ;
19331934} ;
19341935
1936+ // Whether the run `_writeNested` built took in the rule the hoist emptied,
1937+ // which is the caller's to write and so the caller's to leave out.
1938+ let _nestedTookParent = false ;
1939+
19351940/**
19361941 * Whether a block's declarations all stand before its rules, so taking the
19371942 * rules out of it leaves the cascade as it was.
@@ -1992,13 +1997,45 @@ const _writeNested = (path, node, prelude, rest, hoistable) => {
19921997 }
19931998 return null ;
19941999 }
2000+ // Hoisting writes these out as text, so the sibling merge never reads them —
2001+ // two nested rules printing one block would stay two until a second pass.
2002+ // They are neighbors here and nowhere else, so the run is joined here.
19952003 let out = "" ;
2004+ /** @type {{ prelude: string, rest: string, joined: boolean } | null } */
2005+ let held = null ;
2006+ // The rule the hoist emptied stands right in front of the run and may print
2007+ // the same block, so it is offered the join first. Read out of band, the
2008+ // caller writing that rule rather than this.
2009+ _nestedTookParent =
2010+ rest !== null && taken . length !== 0 && taken [ 0 ] . rest === rest ;
2011+ if ( rest !== null && _nestedTookParent ) {
2012+ held = { prelude, rest, joined : false } ;
2013+ }
19962014 for ( let i = 0 ; i < taken . length ; i ++ ) {
1997- out += taken [ i ] . prelude + taken [ i ] . rest ;
2015+ const one = taken [ i ] ;
2016+ if ( held !== null && held . rest === one . rest ) {
2017+ // Concatenated, like every other join: the list is ordered once, below,
2018+ // where the run that grew it ends.
2019+ held . prelude += `,${ one . prelude } ` ;
2020+ held . joined = true ;
2021+ continue ;
2022+ }
2023+ if ( held !== null ) out += _settledPrelude ( held ) + held . rest ;
2024+ held = { prelude : one . prelude , rest : one . rest , joined : false } ;
19982025 }
2026+ if ( held !== null ) out += _settledPrelude ( held ) + held . rest ;
19992027 return out ;
20002028} ;
20012029
2030+ /**
2031+ * A joined run's selector list, canonical. A run that joined nothing is written
2032+ * as it stands, so a rule the hoist only moved pays no read of its own list.
2033+ * @param {{ prelude: string, rest: string, joined: boolean } } run what the join left
2034+ * @returns {string } the list to write
2035+ */
2036+ const _settledPrelude = ( run ) =>
2037+ run . joined ? _canonicalSelectorList ( run . prelude ) : run . prelude ;
2038+
20022039/**
20032040 * Whether a rule's nested rules are taken out of it and written on their own.
20042041 * @param {CssPath } path the walk's accessors
@@ -4508,7 +4545,13 @@ const printer = (path, writer) => {
45084545 ) ;
45094546 if ( nested === null ) return "" ;
45104547 if ( out !== text ) return out + nested ;
4511- if ( nested . length !== 0 ) return text + nested ;
4548+ if ( nested . length !== 0 ) {
4549+ // The run wrote this rule's selectors into itself, and only where
4550+ // nothing but them and its block stood here.
4551+ return _nestedTookParent && text === prelude + rest
4552+ ? nested
4553+ : text + nested ;
4554+ }
45124555 // Where the block is what a sibling is compared against, remember the rule
45134556 // so the parent parts it without re-scanning for the `{`. An at-rule
45144557 // remembers its block's entries too, so a sibling's rules can join them.
0 commit comments