@@ -9927,26 +9927,28 @@ const _mergeableStyleAfter = (path, element) => {
99279927} ;
99289928
99299929/**
9930- * Whether a sheet ends where a rule does: outside every string, comment and
9931- * block, after a `}` or an at-rule's `;`. A sheet appended to it then starts
9932- * rules of its own (§5.4) rather than finishing this one's last.
9930+ * What a sheet needs appended to end where a rule does, as §5.4 ends it at the
9931+ * end of input: the quote, comment end and closers it left open, then `{}` or `;`
9932+ * for a statement it did not finish. Null where it ends on a lone backslash,
9933+ * which would escape whatever followed.
99339934 * @param {string } css the sheet's text
9934- * @returns {boolean } true when another sheet may follow it as written
9935+ * @returns {string | null } the text to append before another sheet, empty where it already ends at a rule
99359936 */
9936- const _sheetEndsAtRule = ( css ) => {
9937+ const _sheetClosing = ( css ) => {
99379938 /** @type {number[] } */
99389939 const closers = [ ] ;
99399940 // Whether nothing has been read since a rule ended, and whether the
99409941 // statement being read is an at-rule, which a `;` ends.
99419942 let ended = true ;
99429943 let atRule = false ;
9943- for ( let i = 0 ; i < css . length ; i ++ ) {
9944+ let open = "" ;
9945+ for ( let i = 0 ; i < css . length && open . length === 0 ; i ++ ) {
99449946 const c = css . charCodeAt ( i ) ;
99459947 if ( isAsciiWhitespace ( c ) ) continue ;
99469948 if ( c === CC_SOLIDUS && css . charCodeAt ( i + 1 ) === CC_ASTERISK ) {
99479949 const close = css . indexOf ( "*/" , i + 2 ) ;
9948- if ( close === - 1 ) return false ;
9949- i = close + 1 ;
9950+ if ( close === - 1 ) open = "*/" ;
9951+ else i = close + 1 ;
99509952 continue ;
99519953 }
99529954 const top = closers . length === 0 ;
@@ -9960,15 +9962,18 @@ const _sheetEndsAtRule = (css) => {
99609962 }
99619963 if ( top ) ended = false ;
99629964 if ( c === CC_REVERSE_SOLIDUS ) {
9963- i ++ ;
9965+ if ( ++ i >= css . length ) return null ;
99649966 } else if ( c === CC_QUOTATION_MARK || c === CC_APOSTROPHE ) {
99659967 // A string ends at its quote, or at a newline as a bad string does.
99669968 for ( i ++ ; i < css . length ; i ++ ) {
99679969 const d = css . charCodeAt ( i ) ;
9968- if ( d === CC_REVERSE_SOLIDUS ) i ++ ;
9969- else if ( d === c || d === CC_LF ) break ;
9970+ if ( d === CC_REVERSE_SOLIDUS ) {
9971+ if ( ++ i >= css . length ) return null ;
9972+ } else if ( d === c || d === CC_LF ) {
9973+ break ;
9974+ }
99709975 }
9971- if ( i >= css . length ) return false ;
9976+ if ( i >= css . length ) open = String . fromCharCode ( c ) ;
99729977 } else if ( c === CC_LEFT_CURLY_BRACKET ) {
99739978 closers . push ( CC_RIGHT_CURLY_BRACKET ) ;
99749979 } else if ( c === CC_LEFT_PARENTHESIS ) {
@@ -9983,25 +9988,34 @@ const _sheetEndsAtRule = (css) => {
99839988 ended = true ;
99849989 }
99859990 }
9986- return ended ;
9991+ for ( let i = closers . length - 1 ; i >= 0 ; i -- ) {
9992+ open += String . fromCharCode ( closers [ i ] ) ;
9993+ }
9994+ if ( closers . length !== 0 ) ended = closers [ 0 ] === CC_RIGHT_CURLY_BRACKET ;
9995+ return ended ? open : `${ open } ${ atRule ? ";" : "{}" } ` ;
99879996} ;
99889997
99899998/**
9990- * The text a `<style>` run prints as one sheet, minified once. Each sheet's
9991- * source is appended while the one before it ends at a rule, so the run stops
9992- * after a sheet the input left open.
9999+ * The text a `<style>` run prints as one sheet, minified once: each sheet's
10000+ * source, the one before it closed first as its own end of input closed it.
999310001 * @param {HtmlPath } path the accessor
999410002 * @param {HtmlElement } element the run's first `<style>`
999510003 * @param {string } css its own text
999610004 * @param {boolean } absorb whether to mark the rest of the run as printed here
999710005 * @returns {string } the run's text
999810006 */
999910007const _styleRunText = ( path , element , css , absorb ) => {
10008+ // Printed as written, a closing would stay in the value a custom property
10009+ // keeps as authored (`--x: f(` reads back `f()`); minified, it is closed anyway.
10010+ const asWritten =
10011+ _S . _renderEmbeddedSource === undefined &&
10012+ _S . _deferEmbeddedSource === undefined ;
1000010013 let text = css ;
10001- let ends = _sheetEndsAtRule ( css ) ;
10014+ let closing = _sheetClosing ( css ) ;
10015+ if ( asWritten && closing !== "" ) closing = null ;
1000210016 for (
1000310017 let next = _mergeableStyleAfter ( path , element ) ;
10004- ends && next !== 0 ;
10018+ closing !== null && next !== 0 ;
1000510019 next = _mergeableStyleAfter ( path , next )
1000610020 ) {
1000710021 const source = _nodeStrings [ _onlyTextChild ( next ) ] ;
@@ -10010,8 +10024,9 @@ const _styleRunText = (path, element, css, absorb) => {
1001010024 if ( _S . _absorbed === null ) _S . _absorbed = new Set ( ) ;
1001110025 _S . _absorbed . add ( next ) ;
1001210026 }
10013- text += source ;
10014- ends = _sheetEndsAtRule ( source ) ;
10027+ text += closing + source ;
10028+ closing = _sheetClosing ( source ) ;
10029+ if ( asWritten && closing !== "" ) closing = null ;
1001510030 }
1001610031 return text ;
1001710032} ;
@@ -13643,7 +13658,7 @@ module.exports._sheetDecides = _sheetDecides;
1364313658module . exports . _SHEET_LEADING_AT_RULE_REGEXP = _SHEET_LEADING_AT_RULE_REGEXP ;
1364413659module . exports . _mergeableStyleAfter = _mergeableStyleAfter ;
1364513660module . exports . _onlyTextChild = _onlyTextChild ;
13646- module . exports . _sheetEndsAtRule = _sheetEndsAtRule ;
13661+ module . exports . _sheetClosing = _sheetClosing ;
1364713662module . exports . _styleRunText = _styleRunText ;
1364813663module . exports . baseTag = baseTag ;
1364913664module . exports . buildHeadTags = buildHeadTags ;
0 commit comments