@@ -291,6 +291,7 @@ const CC_LESS_THAN = 0x3c;
291291const CC_EQUALS = 0x3d ;
292292const CC_GREATER_THAN = 0x3e ;
293293const CC_QUESTION_MARK = 0x3f ;
294+ const CC_COMMERCIAL_AT = 0x40 ;
294295const CC_LEFT_SQUARE_BRACKET = 0x5b ;
295296const CC_REVERSE_SOLIDUS = 0x5c ;
296297const CC_RIGHT_SQUARE_BRACKET = 0x5d ;
@@ -9835,13 +9836,12 @@ const _takeRenderedSheet = (css) => {
98359836 */
98369837const _sheetDecides = ( css ) => {
98379838 const emptying = _removeEmptyElements ;
9838- const joining = _S . _mergeStyles ;
9839+ // A run is joined on its answer only where its text is left open.
9840+ const joining = _S . _mergeStyles && ! _sheetEndsAtRule ( css ) ;
98399841 if ( ! emptying && ! joining ) return undefined ;
9840- const clean = joining && _sheetEndsCleanly ( css ) ;
98419842 return ( answer ) =>
98429843 typeof answer === "string" &&
9843- ( ( emptying && answer === "" ) ||
9844- ( joining && _sheetEndsCleanly ( answer ) !== clean ) ) ;
9844+ ( ( emptying && answer === "" ) || ( joining && _sheetEndsAtRule ( answer ) ) ) ;
98459845} ;
98469846
98479847// An at-rule that only applies at the top of a sheet, so a second sheet carrying
@@ -9930,21 +9930,40 @@ const _mergeableStyleAfter = (path, element) => {
99309930} ;
99319931
99329932/**
9933- * Whether a sheet ends outside every string, comment and bracket, so a sheet
9934- * appended to it starts rules of its own rather than finishing this one's last.
9933+ * Whether a sheet ends where a rule does: outside every string, comment and
9934+ * block, after a `}` or an at-rule's `;`. A sheet appended to it then starts
9935+ * rules of its own (§5.4) rather than finishing this one's last.
99359936 * @param {string } css the sheet's text
9936- * @returns {boolean } true when another sheet may follow it
9937+ * @returns {boolean } true when another sheet may follow it as written
99379938 */
9938- const _sheetEndsCleanly = ( css ) => {
9939- let depth = 0 ;
9939+ const _sheetEndsAtRule = ( css ) => {
9940+ /** @type {number[] } */
9941+ const closers = [ ] ;
9942+ // Whether nothing has been read since a rule ended, and whether the
9943+ // statement being read is an at-rule, which a `;` ends.
9944+ let ended = true ;
9945+ let atRule = false ;
99409946 for ( let i = 0 ; i < css . length ; i ++ ) {
99419947 const c = css . charCodeAt ( i ) ;
9942- if ( c === CC_REVERSE_SOLIDUS ) {
9943- i ++ ;
9944- } else if ( c === CC_SOLIDUS && css . charCodeAt ( i + 1 ) === CC_ASTERISK ) {
9948+ if ( isAsciiWhitespace ( c ) ) continue ;
9949+ if ( c === CC_SOLIDUS && css . charCodeAt ( i + 1 ) === CC_ASTERISK ) {
99459950 const close = css . indexOf ( "*/" , i + 2 ) ;
99469951 if ( close === - 1 ) return false ;
99479952 i = close + 1 ;
9953+ continue ;
9954+ }
9955+ const top = closers . length === 0 ;
9956+ if ( top && ended ) {
9957+ // §5.4.1 skips a CDO or CDC where a rule may start.
9958+ if ( css . startsWith ( "<!--" , i ) || css . startsWith ( "-->" , i ) ) {
9959+ i += c === CC_LESS_THAN ? 3 : 2 ;
9960+ continue ;
9961+ }
9962+ atRule = c === CC_COMMERCIAL_AT ;
9963+ }
9964+ if ( top ) ended = false ;
9965+ if ( c === CC_REVERSE_SOLIDUS ) {
9966+ i ++ ;
99489967 } else if ( c === CC_QUOTATION_MARK || c === CC_APOSTROPHE ) {
99499968 // A string ends at its quote, or at a newline as a bad string does.
99509969 for ( i ++ ; i < css . length ; i ++ ) {
@@ -9953,22 +9972,57 @@ const _sheetEndsCleanly = (css) => {
99539972 else if ( d === c || d === CC_LF ) break ;
99549973 }
99559974 if ( i >= css . length ) return false ;
9956- } else if (
9957- c === CC_LEFT_CURLY_BRACKET ||
9958- c === CC_LEFT_PARENTHESIS ||
9959- c === CC_LEFT_SQUARE_BRACKET
9960- ) {
9961- depth ++ ;
9962- } else if (
9963- depth !== 0 &&
9964- ( c === CC_RIGHT_CURLY_BRACKET ||
9965- c === CC_RIGHT_PARENTHESIS ||
9966- c === CC_RIGHT_SQUARE_BRACKET )
9967- ) {
9968- depth -- ;
9975+ } else if ( c === CC_LEFT_CURLY_BRACKET ) {
9976+ closers . push ( CC_RIGHT_CURLY_BRACKET ) ;
9977+ } else if ( c === CC_LEFT_PARENTHESIS ) {
9978+ closers . push ( CC_RIGHT_PARENTHESIS ) ;
9979+ } else if ( c === CC_LEFT_SQUARE_BRACKET ) {
9980+ closers . push ( CC_RIGHT_SQUARE_BRACKET ) ;
9981+ } else if ( ! top && c === closers [ closers . length - 1 ] ) {
9982+ // §5.4.8 ends a block only on its own closer; any other is a token in it.
9983+ closers . pop ( ) ;
9984+ if ( closers . length === 0 && c === CC_RIGHT_CURLY_BRACKET ) ended = true ;
9985+ } else if ( top && atRule && c === CC_SEMICOLON ) {
9986+ ended = true ;
99699987 }
99709988 }
9971- return depth === 0 ;
9989+ return ended ;
9990+ } ;
9991+
9992+ /**
9993+ * The text a `<style>` run prints as one sheet, minified once. Each sheet's
9994+ * source is appended while the text before it ends at a rule; where it is left
9995+ * open, the renderer's answer for it is appended to instead, if that ends at one.
9996+ * @param {HtmlPath } path the accessor
9997+ * @param {HtmlElement } element the run's first `<style>`
9998+ * @param {string } css its own text
9999+ * @param {boolean } absorb whether to mark the rest of the run as printed here
10000+ * @returns {string } the run's text
10001+ */
10002+ const _styleRunText = ( path , element , css , absorb ) => {
10003+ let text = css ;
10004+ let ends = _sheetEndsAtRule ( css ) ;
10005+ for (
10006+ let next = _mergeableStyleAfter ( path , element ) ;
10007+ next !== 0 ;
10008+ next = _mergeableStyleAfter ( path , next )
10009+ ) {
10010+ const source = _nodeStrings [ _onlyTextChild ( next ) ] ;
10011+ if ( _SHEET_LEADING_AT_RULE_REGEXP . test ( source ) ) break ;
10012+ if ( ! ends ) {
10013+ // Without an answer yet, `_sheetDecides` has the print run again.
10014+ if ( ! _rendersNow ( ) ) break ;
10015+ text = _renderedSheet ( text ) ;
10016+ if ( ! _sheetEndsAtRule ( text ) ) break ;
10017+ }
10018+ if ( absorb ) {
10019+ if ( _S . _absorbed === null ) _S . _absorbed = new Set ( ) ;
10020+ _S . _absorbed . add ( next ) ;
10021+ }
10022+ text += source ;
10023+ ends = _sheetEndsAtRule ( source ) ;
10024+ }
10025+ return text ;
997210026} ;
997310027
997410028/**
@@ -9984,21 +10038,8 @@ const _sheetPrintsNothing = (path, element) => {
998410038 return false ;
998510039 }
998610040 const css = _nodeStrings [ first ] ;
9987- if ( css . trim ( ) === "" || _renderedSheet ( css ) !== "" ) return false ;
9988- if ( ! _S . _mergeStyles ) return true ;
9989- // The run `_mergedStyleRun` builds, read with the same two breaks.
9990- for (
9991- let next = _mergeableStyleAfter ( path , element ) ;
9992- next !== 0 ;
9993- next = _mergeableStyleAfter ( path , next )
9994- ) {
9995- const text = _nodeStrings [ _onlyTextChild ( next ) ] ;
9996- if ( _SHEET_LEADING_AT_RULE_REGEXP . test ( text ) ) break ;
9997- const printed = text . trim ( ) === "" ? text : _renderedSheet ( text ) ;
9998- if ( ! _sheetEndsCleanly ( printed ) ) break ;
9999- if ( printed !== "" ) return false ;
10000- }
10001- return true ;
10041+ const text = _S . _mergeStyles ? _styleRunText ( path , element , css , false ) : css ;
10042+ return text . trim ( ) !== "" && _renderedSheet ( text ) === "" ;
1000210043} ;
1000310044
1000410045/**
@@ -13611,7 +13652,8 @@ module.exports._sheetDecides = _sheetDecides;
1361113652module . exports . _SHEET_LEADING_AT_RULE_REGEXP = _SHEET_LEADING_AT_RULE_REGEXP ;
1361213653module . exports . _mergeableStyleAfter = _mergeableStyleAfter ;
1361313654module . exports . _onlyTextChild = _onlyTextChild ;
13614- module . exports . _sheetEndsCleanly = _sheetEndsCleanly ;
13655+ module . exports . _sheetEndsAtRule = _sheetEndsAtRule ;
13656+ module . exports . _styleRunText = _styleRunText ;
1361513657module . exports . baseTag = baseTag ;
1361613658module . exports . buildHeadTags = buildHeadTags ;
1361713659module . exports . collapseWhitespaceRuns = collapseWhitespaceRuns ;
0 commit comments