Skip to content

Commit 09235a9

Browse files
fix(html): read a foreign style body as character data (#22258)
A `<style>` body is raw text in the HTML namespace, so its source is its text. The same element in a foreign subtree holds character data, where a character reference stands for what it names — so `<svg><style>&#x2e;a{…}` fed the CSS pipeline the literal `&#x2e;a`, an invalid selector, instead of the rule `.a` a browser reads there. The namespace now decides which of the two the body is, as it already does for `<script>` and on the printing side. Because the body is decoded going in, the write-back escapes it as text coming back out, keeping the round trip a fixed point where an `&` in the processed CSS would otherwise be re-read as a character reference.
1 parent b4c3707 commit 09235a9

9 files changed

Lines changed: 128 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Read a foreign `<script>` or `<style>` body as character data, not as raw text.

‎.changeset/014-html-foreign-script-body.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎lib/dependencies/HtmlInlineStyleDependency.js‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ const ATTR_ESCAPES = { "&": "&amp;", '"': "&quot;", "'": "&#39;" };
3131
*/
3232
const escapeAttrChar = (c) => ATTR_ESCAPES[c];
3333

34+
// Character data needs only the two that open markup: `&` starts a character
35+
// reference and `<` a tag. A CR is written as a reference because §13.2.3.5
36+
// rewrites a literal one to LF before the tokenizer reads it.
37+
const TEXT_ESCAPE_REGEXP = /[&<\r]/g;
38+
/** @type {Record<string, string>} */
39+
const TEXT_ESCAPES = { "&": "&amp;", "<": "&lt;", "\r": "&#13;" };
40+
/**
41+
* @param {string} c matched character
42+
* @returns {string} the HTML-text-escaped entity
43+
*/
44+
const escapeTextChar = (c) => TEXT_ESCAPES[c];
45+
3446
/**
3547
* Represents inline CSS in an HTML module — either a `<style>...</style>`
3648
* block (a stylesheet) or an element's `style="..."` attribute (a CSS
@@ -47,14 +59,23 @@ class HtmlInlineStyleDependency extends ModuleDependency {
4759
* @param {Range} range range of the inline CSS content (between `<style>` and `</style>`, or the `style` attribute value)
4860
* @param {boolean=} attribute true when the source is a `style="..."` attribute (a block's contents) rather than a `<style>` block (stylesheet)
4961
* @param {boolean=} inAttribute true when the range is an attribute value (decoded on parse, so the processed CSS must be re-escaped on write-back); defaults to `attribute` — differs only for attribute values parsed as full stylesheets
62+
* @param {boolean=} characterData true when the range is a `<style>` body in a foreign (SVG/MathML) subtree, where the content model is character data rather than raw text, so the processed CSS is escaped as text on write-back
5063
*/
51-
constructor(request, range, attribute = false, inAttribute = attribute) {
64+
constructor(
65+
request,
66+
range,
67+
attribute = false,
68+
inAttribute = attribute,
69+
characterData = false
70+
) {
5271
super(request);
5372
this.range = range;
5473
/** @type {boolean} */
5574
this.attribute = attribute;
5675
/** @type {boolean} */
5776
this.inAttribute = inAttribute;
77+
/** @type {boolean} */
78+
this.characterData = characterData;
5879
}
5980

6081
get type() {
@@ -87,6 +108,7 @@ class HtmlInlineStyleDependency extends ModuleDependency {
87108
super.serialize(context);
88109
context.write(this.attribute);
89110
context.write(this.inAttribute);
111+
context.write(this.characterData);
90112
}
91113

92114
/**
@@ -97,6 +119,7 @@ class HtmlInlineStyleDependency extends ModuleDependency {
97119
super.deserialize(context);
98120
this.attribute = context.read();
99121
this.inAttribute = context.read();
122+
this.characterData = context.read();
100123
}
101124
}
102125

@@ -134,6 +157,11 @@ HtmlInlineStyleDependency.Template = class HtmlInlineStyleDependencyTemplate ext
134157
cssText = cssText
135158
.replace(TRAILING_WHITESPACE_REGEXP, "")
136159
.replace(ATTR_ESCAPE_REGEXP, escapeAttrChar);
160+
} else if (dep.characterData) {
161+
// A foreign `<style>` body was entity-decoded before parsing, so the
162+
// processed CSS is escaped back as text — its newlines stay literal,
163+
// which an attribute value could not afford.
164+
cssText = cssText.replace(TEXT_ESCAPE_REGEXP, escapeTextChar);
137165
}
138166

139167
source.replace(dep.range[0], dep.range[1] - 1, cssText);

‎lib/html/HtmlParser.js‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,19 +1608,30 @@ class HtmlParser extends Parser {
16081608
// A resolved `type` selects the parse algorithm and the dependency
16091609
// kind — the only dispatch in the parser.
16101610
switch (type) {
1611-
// Inline CSS — a `<style>` body (raw, a full stylesheet) or an
1611+
// Inline CSS — a `<style>` body (a full stylesheet) or an
16121612
// attribute value (decoded, re-escaped on write-back; `*-attribute`
16131613
// is a declaration list). Needs `experiments.css`.
16141614
case "stylesheet-style":
16151615
case "stylesheet-style-attribute": {
16161616
if (!css) break;
1617-
const cssText = content ? value : decodeEntities(value, true);
1617+
// WHY: an HTML `<style>` body is raw text, so its source is its
1618+
// text. The same element in a foreign subtree holds character
1619+
// data, where `&#x2e;a` is the selector `.a` — so the sheet has
1620+
// to be decoded going in and escaped coming back out, which is
1621+
// what `characterData` tells the write-back.
1622+
const characterData = content && path.namespace() !== NS_HTML;
1623+
const cssText = content
1624+
? characterData
1625+
? decodeEntities(value, false)
1626+
: value
1627+
: decodeEntities(value, true);
16181628
if (cssText.trim() === "") break;
16191629
const dep = new HtmlInlineStyleDependency(
16201630
dataUri("text/css", cssText),
16211631
[start, end],
16221632
content ? false : type === "stylesheet-style-attribute",
1623-
!content
1633+
!content,
1634+
characterData
16241635
);
16251636
setLoc(dep, start, end);
16261637
module.addDependency(dep);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases html foreign-style-body exported tests should read a foreign style body as character data 1`] = `
4+
"<!DOCTYPE html>
5+
<title>Foreign style body</title>
6+
<style>/*!************************************************************************************************!*\\\\
7+
!*** css data:text/css;base64,Lmh0bWxuczo6YWZ0ZXJ7Y29udGVudDoiJmFtcDsifQ== (exportType: text) ***!
8+
\\\\************************************************************************************************/
9+
.htmlns::after{content:\\"&amp;\\"}
10+
</style>
11+
<svg><style>/*!********************************************************************************!*\\\\
12+
!*** css data:text/css;base64,LmZvcmVpZ257Y29sb3I6Z3JlZW59 (exportType: text) ***!
13+
\\\\********************************************************************************/
14+
.foreign{color:green}
15+
</style></svg>
16+
<svg><style>/*!************************************************************************************!*\\\\
17+
!*** css data:text/css;base64,LmVzYzo6YWZ0ZXJ7Y29udGVudDoiJiJ9 (exportType: text) ***!
18+
\\\\************************************************************************************/
19+
.esc::after{content:\\"&amp;\\"}
20+
</style></svg>
21+
"
22+
`;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases html foreign-style-body exported tests should read a foreign style body as character data 1`] = `
4+
"<!DOCTYPE html>
5+
<title>Foreign style body</title>
6+
<style>/*!************************************************************************************************!*\\\\
7+
!*** css data:text/css;base64,Lmh0bWxuczo6YWZ0ZXJ7Y29udGVudDoiJmFtcDsifQ== (exportType: text) ***!
8+
\\\\************************************************************************************************/
9+
.htmlns::after{content:\\"&amp;\\"}
10+
</style>
11+
<svg><style>/*!********************************************************************************!*\\\\
12+
!*** css data:text/css;base64,LmZvcmVpZ257Y29sb3I6Z3JlZW59 (exportType: text) ***!
13+
\\\\********************************************************************************/
14+
.foreign{color:green}
15+
</style></svg>
16+
<svg><style>/*!************************************************************************************!*\\\\
17+
!*** css data:text/css;base64,LmVzYzo6YWZ0ZXJ7Y29udGVudDoiJiJ9 (exportType: text) ***!
18+
\\\\************************************************************************************/
19+
.esc::after{content:\\"&amp;\\"}
20+
</style></svg>
21+
"
22+
`;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import page from "./page.html";
2+
3+
it("should read a foreign style body as character data", () => {
4+
expect(page).toMatchSnapshot();
5+
6+
// An HTML-namespace `<style>` body is raw text, so `&amp;` is the five
7+
// characters the author wrote and the declaration keeps them.
8+
expect(page).toContain('.htmlns::after{content:"&amp;"}');
9+
10+
// `<svg><style>` holds character data, so `&#x2e;` names the `.` that
11+
// starts the selector. Kept as its source the rule reads `&#x2e;foreign`,
12+
// which selects nothing.
13+
expect(page).toContain(".foreign");
14+
expect(page).not.toContain("&#x2e;foreign");
15+
16+
// Character data is decoded on the way in, so it has to be escaped on the
17+
// way out — otherwise re-reading the document turns `&amp;` into `&`, and
18+
// one more round trip would take `&amp;lt;` to `<`.
19+
expect(page).toContain('.esc::after{content:"&amp;"}');
20+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<title>Foreign style body</title>
3+
<style>.htmlns::after{content:"&amp;"}</style>
4+
<svg><style>&#x2e;foreign{color:green}</style></svg>
5+
<svg><style>.esc::after{content:"&amp;"}</style></svg>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
devtool: false,
6+
target: "web",
7+
experiments: {
8+
html: true,
9+
css: true
10+
}
11+
};

0 commit comments

Comments
 (0)