Skip to content

Commit 2a8f547

Browse files
committed
fix(html): minify a conditional comment inside a srcdoc too
The nested body went through a bare processor, so a downlevel-hidden conditional comment in there kept its unminified markup when both options were on. Both nested languages now go through one whole-document function, which the srcdoc pass recurses into.
1 parent ba0b471 commit 2a8f547

6 files changed

Lines changed: 104 additions & 85 deletions

File tree

‎lib/html/htmlMinify.js‎

Lines changed: 92 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (!/srcdoc\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 && /srcdoc\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+
!/^<!--\[if\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 (!/^<!--\[if\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

‎test/configCases/html/minimize-srcdoc/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exports[`ConfigCacheTestCases html minimize-srcdoc minimize-srcdoc should compil
1010
<iframe id=entities srcdoc=\\"<p>a & b &amp;lt; c\\"></iframe>
1111
<iframe id=quote-in-body srcdoc=\\"<p title=q>quoted\\"></iframe>
1212
<iframe id=upper srcdoc=\\"<em>up</em>\\"></iframe>
13+
<iframe id=conditional srcdoc=\\"<!--[if IE]><p class=ie>downlevel<![endif]-->\\"></iframe>
1314
<iframe id=bare srcdoc=\\"<p>bare\\" title=after></iframe>
1415
<iframe id=empty srcdoc></iframe>
1516
<iframe id=none src=child.html></iframe>

‎test/configCases/html/minimize-srcdoc/__snapshots__/ConfigTest.snap‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exports[`ConfigTestCases html minimize-srcdoc minimize-srcdoc should compile 1`]
1010
<iframe id=entities srcdoc=\\"<p>a & b &amp;lt; c\\"></iframe>
1111
<iframe id=quote-in-body srcdoc=\\"<p title=q>quoted\\"></iframe>
1212
<iframe id=upper srcdoc=\\"<em>up</em>\\"></iframe>
13+
<iframe id=conditional srcdoc=\\"<!--[if IE]><p class=ie>downlevel<![endif]-->\\"></iframe>
1314
<iframe id=bare srcdoc=\\"<p>bare\\" title=after></iframe>
1415
<iframe id=empty srcdoc></iframe>
1516
<iframe id=none src=child.html></iframe>

‎test/configCases/html/minimize-srcdoc/page.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<iframe id="entities" srcdoc="&lt;p&gt;a &amp;amp; b &amp;lt; c&lt;/p&gt;"></iframe>
1313
<iframe id="quote-in-body" srcdoc="&lt;p title=&quot;q&quot; &gt;quoted&lt;/p&gt;"></iframe>
1414
<iframe id="upper" SRCDOC="&lt;!-- dropped --&gt;&lt;em &gt;up&lt;/em&gt;"></iframe>
15+
<iframe id="conditional" srcdoc="&lt;!--[if IE]&gt;&lt;p class=&quot;ie&quot;&gt;downlevel&lt;/p&gt;&lt;![endif]--&gt;"></iframe>
1516
<iframe id="bare" srcdoc=&lt;!--dropped--&gt;&lt;p&gt;bare&lt;/p&gt; title="after"></iframe>
1617
<iframe id="empty" srcdoc=""></iframe>
1718
<iframe id="none" src="child.html"></iframe>

‎test/configCases/html/minimize-srcdoc/test.config.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ module.exports = {
3636
// reference needs none, and renders as itself either way.
3737
expect(page).toContain('srcdoc="<p>a & b &amp;lt; c"');
3838

39+
// Both nested languages reach a srcdoc: a conditional comment in there is
40+
// minified as one, not left as the text of an attribute.
41+
expect(page).toContain(
42+
'srcdoc="<!--[if IE]><p class=ie>downlevel<![endif]-->"'
43+
);
44+
3945
// A document cannot sit in an unquoted value — a space or a `>` would end
4046
// the attribute, taking the one after it with it — so it gains quotes.
4147
expect(page).toContain('<iframe id=bare srcdoc="<p>bare" title=after>');

‎test/configCases/html/minimize-srcdoc/webpack.config.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ module.exports = {
1010
parser: { html: { sources: false } }
1111
},
1212
optimization: {
13-
minimize: { html: { minifySrcdoc: true } },
13+
minimize: {
14+
html: { minifySrcdoc: true, minifyConditionalComments: true }
15+
},
1416
minimizer: ["..."]
1517
},
1618
experiments: { html: true }

0 commit comments

Comments
 (0)