@@ -81,101 +81,109 @@ const htmlMinify = (input, sourceMap, minimizerOptions = {}) => {
8181 sortTokenLists,
8282 removeImpliedTags
8383 } ;
84- // An `<iframe srcdoc>` holds a whole document, and like the conditional
85- // comments below it cannot be minified where it is found. It is put back into
86- // the source rather than into the output, because the printer respells an
87- // attribute value — the recorded text is not what the output ends up holding.
88- // Only a document that names the attribute at all pays the extra parse.
84+ // Both nested languages a document can hold are minified as the documents they
85+ // are, so this is what each of them is put back through — an `<iframe srcdoc>`
86+ // carrying a conditional comment gets both, in that order.
8987 //
9088 // Recursion goes through this local name, never the module's own: the plugin
9189 // ships this function to its workers as source, where that binding is absent.
9290 /**
93- * @param {string } text markup that may hold `<iframe srcdoc>` documents
94- * @returns {string } the same markup, each of those minified in place
91+ * @param {string } text a whole document
92+ * @returns {string } the same document, minified
9593 */
96- const minifySrcdocIn = ( text ) => {
94+ const minifyDocument = ( text ) => {
95+ // An `<iframe srcdoc>` holds a whole document, and like the conditional
96+ // comments below it cannot be minified where it is found. It is put back into
97+ // the source rather than into the output, because the printer respells an
98+ // attribute value — the recorded text is not what the output ends up holding.
99+ // Only a document naming the attribute at all pays the extra parse.
100+ let source = text ;
97101 // Spelt as an attribute with a value: one written bare carries no document.
98- if ( ! / s r c d o c \s * = / i. test ( text ) ) return text ;
99- /** @type {{ start: number, end: number, value: string }[] } */
100- const found = [ ] ;
101- new SourceProcessor ( )
102- . use ( {
103- [ NodeType . Element ] : ( path ) => {
104- // An `<iframe>` in foreign content is an SVG or MathML element that
105- // happens to share the name, and its `srcdoc` is not this attribute.
106- if ( path . tagName ( ) !== "iframe" || path . namespace ( ) !== NS_HTML ) {
107- return ;
102+ if ( minifySrcdoc && / s r c d o c \s * = / i. test ( source ) ) {
103+ /** @type {{ start: number, end: number, value: string }[] } */
104+ const found = [ ] ;
105+ new SourceProcessor ( )
106+ . use ( {
107+ [ NodeType . Element ] : ( path ) => {
108+ // An `<iframe>` in foreign content is an SVG or MathML element that
109+ // happens to share the name, and its `srcdoc` is not this attribute.
110+ if ( path . tagName ( ) !== "iframe" || path . namespace ( ) !== NS_HTML ) {
111+ return ;
112+ }
113+ for ( const attribute of path . attributes ( ) ) {
114+ if ( attribute . name !== "srcdoc" || attribute . value === "" ) {
115+ continue ;
116+ }
117+ found . push ( {
118+ start : attribute . valueStart ,
119+ end : attribute . valueEnd ,
120+ value : attribute . value
121+ } ) ;
122+ }
108123 }
109- for ( const attribute of path . attributes ( ) ) {
110- if ( attribute . name !== "srcdoc" || attribute . value === "" ) continue ;
111- found . push ( {
112- start : attribute . valueStart ,
113- end : attribute . valueEnd ,
114- value : attribute . value
115- } ) ;
124+ } )
125+ . process ( source , { } ) ;
126+ // Back to front, so an earlier splice does not move a later range.
127+ for ( let i = found . length - 1 ; i >= 0 ; i -- ) {
128+ const { start, end, value } = found [ i ] ;
129+ const inner = minifyDocument ( decodeEntities ( value , true ) ) ;
130+ // A document is not something an unquoted value can hold — a space or a
131+ // `>` in it would end the attribute — so one written bare gets quotes.
132+ const delimiter = source . charCodeAt ( start - 1 ) ;
133+ const quoted = delimiter === 34 || delimiter === 39 ;
134+ const written = escapeAttribute ( inner , quoted ? delimiter : 34 , true ) ;
135+ source = `${ source . slice ( 0 , start ) } ${ quoted ? written : `"${ written } "` } ${ source . slice ( end ) } ` ;
136+ }
137+ }
138+ // A downlevel-hidden conditional comment holds markup, and it cannot be
139+ // minified where it is found: the visitor that finds it runs inside the
140+ // document's own parse, and the walk state is module-scoped, so `grammar`
141+ // refuses to start a second one. The bodies are therefore recorded as the
142+ // document prints — a visitor rides the walk that prints, so this costs no
143+ // extra parse — and each is minified afterwards and spliced in by exact
144+ // source text, never by a pattern over the output, which would find a
145+ // `<!--[if` written inside a script.
146+ /** @type {Map<string, string> } */
147+ const conditionals = new Map ( ) ;
148+ // One processor for both passes. It stops recording once the document is
149+ // printed, so a body carrying something that looks like another conditional
150+ // comment cannot write into the map being read below.
151+ let recording = true ;
152+ const processor = new SourceProcessor ( ) ;
153+ if ( minifyConditionalComments ) {
154+ processor . use ( {
155+ [ NodeType . Comment ] : ( path ) => {
156+ if ( ! recording ) return ;
157+ const comment = path . source ( ) ;
158+ const opened = comment . indexOf ( "]>" ) ;
159+ const closes = comment . lastIndexOf ( "<![endif]" ) ;
160+ if (
161+ ! / ^ < ! - - \[ i f \s / i. test ( comment ) ||
162+ opened === - 1 ||
163+ closes <= opened
164+ ) {
165+ return ;
116166 }
167+ conditionals . set ( comment , comment . slice ( opened + 2 , closes ) ) ;
117168 }
118- } )
119- . process ( text , { } ) ;
120- let out = text ;
121- // Back to front, so an earlier splice does not move a later range.
122- for ( let i = found . length - 1 ; i >= 0 ; i -- ) {
123- const { start, end, value } = found [ i ] ;
124- const body = minifySrcdocIn ( decodeEntities ( value , true ) ) ;
125- const { code : inner } = new SourceProcessor ( ) . process ( body , printOptions ) ;
126- // A document is not something an unquoted value can hold — a space or a
127- // `>` in it would end the attribute — so one written bare gets quotes.
128- const delimiter = text . charCodeAt ( start - 1 ) ;
129- const quoted = delimiter === 34 || delimiter === 39 ;
130- const written = escapeAttribute ( inner , quoted ? delimiter : 34 , true ) ;
131- out = `${ out . slice ( 0 , start ) } ${ quoted ? written : `"${ written } "` } ${ out . slice ( end ) } ` ;
169+ } ) ;
132170 }
133- return out ;
171+ // `process` parses once, and with `mode: "minify"` its walk also prints the
172+ // safely minified serialization — no second parse. No `source` is named, so no
173+ // map is built: the HTML serialize is token-coarse and only the code is used.
174+ const { code : out } = processor . process ( source , printOptions ) ;
175+ recording = false ;
176+ if ( conditionals . size === 0 ) return out ;
177+ let spliced = out ;
178+ for ( const [ comment , body ] of conditionals ) {
179+ if ( ! spliced . includes ( comment ) ) continue ;
180+ const { code : minified } = processor . process ( body , printOptions ) ;
181+ if ( minified === body ) continue ;
182+ spliced = spliced . split ( comment ) . join ( comment . replace ( body , minified ) ) ;
183+ }
184+ return spliced ;
134185 } ;
135- const source = minifySrcdoc ? minifySrcdocIn ( code ) : code ;
136- // A downlevel-hidden conditional comment holds markup, and it cannot be
137- // minified where it is found: the visitor that finds it runs inside the
138- // document's own parse, and the walk state is module-scoped, so `grammar`
139- // refuses to start a second one. The bodies are therefore recorded as the
140- // document prints — a visitor rides the walk that prints, so this costs no
141- // extra parse — and each is minified afterwards and spliced in by exact
142- // source text, never by a pattern over the output, which would find a
143- // `<!--[if` written inside a script.
144- /** @type {Map<string, string> } */
145- const conditionals = new Map ( ) ;
146- // One processor for both passes. It stops recording once the document is
147- // printed, so a body carrying something that looks like another conditional
148- // comment cannot write into the map being read below.
149- let recording = true ;
150- const processor = new SourceProcessor ( ) ;
151- if ( minifyConditionalComments ) {
152- processor . use ( {
153- [ NodeType . Comment ] : ( path ) => {
154- if ( ! recording ) return ;
155- const source = path . source ( ) ;
156- const opened = source . indexOf ( "]>" ) ;
157- const closes = source . lastIndexOf ( "<![endif]" ) ;
158- if ( ! / ^ < ! - - \[ i f \s / i. test ( source ) || opened === - 1 || closes <= opened ) {
159- return ;
160- }
161- conditionals . set ( source , source . slice ( opened + 2 , closes ) ) ;
162- }
163- } ) ;
164- }
165- // `process` parses once, and with `mode: "minify"` its walk also prints the
166- // safely minified serialization — no second parse. No `source` is named, so no
167- // map is built: the HTML serialize is token-coarse and only the code is used.
168- const { code : out } = processor . process ( source , printOptions ) ;
169- recording = false ;
170- if ( conditionals . size === 0 ) return { code : out } ;
171- let spliced = out ;
172- for ( const [ source , body ] of conditionals ) {
173- if ( ! spliced . includes ( source ) ) continue ;
174- const { code : minified } = processor . process ( body , printOptions ) ;
175- if ( minified === body ) continue ;
176- spliced = spliced . split ( source ) . join ( source . replace ( body , minified ) ) ;
177- }
178- return { code : spliced } ;
186+ return { code : minifyDocument ( code ) } ;
179187} ;
180188
181189// Worker-safe (see the body's in-worker `require`), so it may run in the shared
0 commit comments