Skip to content

Commit ba0b471

Browse files
committed
fix(html): scope the srcdoc minifier and quote what it writes back
An `<iframe>` in foreign content is an SVG or MathML element sharing the name, so its `srcdoc` is not this attribute and its value is left alone. A value written without quotes gains them: a document holds spaces and `>`, either of which would end a bare value and swallow the attribute after it.
1 parent 281c581 commit ba0b471

5 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎lib/html/htmlMinify.js‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@ const htmlMinify = (input, sourceMap, minimizerOptions = {}) => {
4040
// parses but change the attribute string a script reads back, so a consumer
4141
// that compares those bytes wants a way to turn each off. Room to widen them
4242
// too: any boolean value rather than only the canonical spellings.
43-
const { SourceProcessor, NodeType, decodeEntities, escapeAttribute } =
44-
webpack.html.syntax;
43+
const {
44+
SourceProcessor,
45+
NodeType,
46+
NS_HTML,
47+
decodeEntities,
48+
escapeAttribute
49+
} = webpack.html.syntax;
4550
const [[, code]] = Object.entries(input);
4651
// The nested CSS minifier gets the abilities and options a `.css` asset
4752
// gets, or the inline copy of a declaration would disagree with it.
@@ -89,13 +94,18 @@ const htmlMinify = (input, sourceMap, minimizerOptions = {}) => {
8994
* @returns {string} the same markup, each of those minified in place
9095
*/
9196
const minifySrcdocIn = (text) => {
92-
if (!/srcdoc/i.test(text)) return text;
97+
// Spelt as an attribute with a value: one written bare carries no document.
98+
if (!/srcdoc\s*=/i.test(text)) return text;
9399
/** @type {{ start: number, end: number, value: string }[]} */
94100
const found = [];
95101
new SourceProcessor()
96102
.use({
97103
[NodeType.Element]: (path) => {
98-
if (path.tagName() !== "iframe") return;
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;
108+
}
99109
for (const attribute of path.attributes()) {
100110
if (attribute.name !== "srcdoc" || attribute.value === "") continue;
101111
found.push({
@@ -113,7 +123,12 @@ const htmlMinify = (input, sourceMap, minimizerOptions = {}) => {
113123
const { start, end, value } = found[i];
114124
const body = minifySrcdocIn(decodeEntities(value, true));
115125
const { code: inner } = new SourceProcessor().process(body, printOptions);
116-
out = `${out.slice(0, start)}${escapeAttribute(inner, text.charCodeAt(start - 1), true)}${out.slice(end)}`;
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)}`;
117132
}
118133
return out;
119134
};

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ 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=bare srcdoc=\\"<p>bare\\" title=after></iframe>
1314
<iframe id=empty srcdoc></iframe>
1415
<iframe id=none src=child.html></iframe>
16+
<div id=not-an-iframe srcdoc=\\"<!-- kept --><p >div</p>\\"></div>
17+
<svg><iframe id=foreign srcdoc=\\"<!-- kept --><p >svg</p>\\"></iframe></svg>
1518
<p>the word srcdoc in text is not an attribute</p>
1619
1720

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ 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=bare srcdoc=\\"<p>bare\\" title=after></iframe>
1314
<iframe id=empty srcdoc></iframe>
1415
<iframe id=none src=child.html></iframe>
16+
<div id=not-an-iframe srcdoc=\\"<!-- kept --><p >div</p>\\"></div>
17+
<svg><iframe id=foreign srcdoc=\\"<!-- kept --><p >svg</p>\\"></iframe></svg>
1518
<p>the word srcdoc in text is not an attribute</p>
1619
1720

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
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="bare" srcdoc=&lt;!--dropped--&gt;&lt;p&gt;bare&lt;/p&gt; title="after"></iframe>
1516
<iframe id="empty" srcdoc=""></iframe>
1617
<iframe id="none" src="child.html"></iframe>
18+
<div id="not-an-iframe" srcdoc="&lt;!-- kept --&gt;&lt;p &gt;div&lt;/p&gt;"></div>
19+
<svg><iframe id="foreign" srcdoc="&lt;!-- kept --&gt;&lt;p &gt;svg&lt;/p&gt;"></iframe></svg>
1720
<p>the word srcdoc in text is not an attribute</p>
1821
</body>
1922
</html>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@ 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+
// A document cannot sit in an unquoted value — a space or a `>` would end
40+
// the attribute, taking the one after it with it — so it gains quotes.
41+
expect(page).toContain('<iframe id=bare srcdoc="<p>bare" title=after>');
42+
3943
// Nothing to minify, and nothing invented.
4044
expect(page).toContain("<iframe id=empty srcdoc>");
4145
expect(page).toContain("<iframe id=none src=child.html>");
4246
expect(page).toContain("the word srcdoc in text is not an attribute");
47+
48+
// The attribute belongs to an HTML `<iframe>`. On anything else the name
49+
// means nothing, so the value is left as the string it is.
50+
expect(page).toContain("<!-- kept --><p >div</p>");
51+
expect(page).toContain("<!-- kept --><p >svg</p>");
4352
}
4453
};

0 commit comments

Comments
 (0)