Skip to content

Commit

Permalink
fix(html/codegen): Expand elements before which body isn’t elided (#8877
Browse files Browse the repository at this point in the history
)

**Description:**

For example, transforming `<body><noscript>` to `<noscript>` would
incorrectly change the meaning so `<noscript>` is parsed as a child of
`<head>`.

Reference: [§13.2.6.4.4 The "in head" insertion
mode](https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead),
[13.2.6.4.6 The "after head" insertion
mode](https://html.spec.whatwg.org/multipage/parsing.html#the-after-head-insertion-mode).

**Related issue:**

- Closes #8876.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Loading branch information
andersk committed Apr 19, 2024
1 parent a4d3b26 commit 5419a94
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
14 changes: 8 additions & 6 deletions crates/swc_html_codegen/src/lib.rs
Expand Up @@ -234,8 +234,7 @@ where
}
// A body element's start tag can be omitted if the element is empty, or if the
// first thing inside the body element is not ASCII whitespace or a comment, except
// if the first thing inside the body element is a meta, link, script, style, or
// template element.
// if the first thing inside the body element would be parsed differently outside.
"body"
if n.children.is_empty()
|| (match n.children.first() {
Expand All @@ -253,14 +252,17 @@ where
})) if *namespace == Namespace::HTML
&& matches!(
&**tag_name,
"meta"
"base"
| "basefont"
| "bgsound"
| "frameset"
| "link"
| "meta"
| "noframes"
| "noscript"
| "script"
| "style"
| "template"
| "bgsound"
| "basefont"
| "base"
| "title"
) =>
{
Expand Down
@@ -0,0 +1,8 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Test</title>
</head>
<body><noscript>This app requires JavaScript.</noscript></body>
</html>
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Test</title>
</head>
<body><noscript>This app requires JavaScript.</noscript>

</body></html>
@@ -0,0 +1,6 @@
<!doctype html><html lang=en><head>
<meta charset=utf-8>
<title>Test</title>
</head>
<body><noscript>This app requires JavaScript.</noscript>

@@ -1,4 +1,4 @@
<!doctype html><html lang=en><meta charset=UTF-8><meta name=viewport content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"><meta http-equiv=X-UA-Compatible content="ie=edge"><title>Document</title><noscript>
<!doctype html><html lang=en><meta charset=UTF-8><meta name=viewport content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"><meta http-equiv=X-UA-Compatible content="ie=edge"><title>Document</title><body><noscript>

<a href=https://www.mozilla.com/>External Link</a>
</noscript>
Expand Down

0 comments on commit 5419a94

Please sign in to comment.