Skip to content

Commit

Permalink
fix(html/codegen): Handle optional tags (#4986)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 17, 2022
1 parent d722ee2 commit 41dc0a7
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 8 deletions.
55 changes: 48 additions & 7 deletions crates/swc_html_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,19 @@ where
}) if is_html_tag_name(*namespace, &**tag_name)
&& !matches!(
&**tag_name,
"a" | "audio" | "del" | "ins" | "map" | "noscript" | "video"
"a" | "audio"
| "acronym"
| "big"
| "del"
| "font"
| "ins"
| "tt"
| "strike"
| "map"
| "noscript"
| "video"
| "kbd"
| "rbc"
) =>
{
true
Expand All @@ -502,15 +514,29 @@ where
// An li element's end tag can be omitted if the li element is immediately
// followed by another li element or if there is no more content in the parent
// element.
"li" => match next {
Some(Child::Element(Element {
"li" if match parent {
Some(Element {
namespace,
tag_name,
..
})) if *namespace == Namespace::HTML && tag_name == "li" => true,
None => true,
}) if *namespace == Namespace::HTML
&& matches!(&**tag_name, "ul" | "ol" | "menu") =>
{
true
}
_ => false,
},
} =>
{
match next {
Some(Child::Element(Element {
namespace,
tag_name,
..
})) if *namespace == Namespace::HTML && tag_name == "li" => true,
None => true,
_ => false,
}
}
// A dt element's end tag can be omitted if the dt element is immediately
// followed by another dt element or a dd element.
"dt" => match next {
Expand Down Expand Up @@ -1079,6 +1105,7 @@ fn is_html_tag_name(namespace: Namespace, tag_name: &str) -> bool {
matches!(
tag_name,
"a" | "abbr"
| "acronym"
| "address"
| "applet"
| "area"
Expand All @@ -1087,8 +1114,10 @@ fn is_html_tag_name(namespace: Namespace, tag_name: &str) -> bool {
| "audio"
| "b"
| "base"
| "basefont"
| "bdi"
| "bdo"
| "big"
| "blockquote"
| "body"
| "br"
Expand Down Expand Up @@ -1116,8 +1145,11 @@ fn is_html_tag_name(namespace: Namespace, tag_name: &str) -> bool {
| "fieldset"
| "figcaption"
| "figure"
| "font"
| "footer"
| "form"
| "frame"
| "frameset"
| "h1"
| "h2"
| "h3"
Expand All @@ -1135,6 +1167,9 @@ fn is_html_tag_name(namespace: Namespace, tag_name: &str) -> bool {
| "img"
| "input"
| "ins"
| "isindex"
| "kbd"
| "keygen"
| "label"
| "legend"
| "li"
Expand All @@ -1145,12 +1180,14 @@ fn is_html_tag_name(namespace: Namespace, tag_name: &str) -> bool {
| "mark"
| "marquee"
| "menu"
| "menuitem"
// Removed from spec, but we keep here to track it
// | "menuitem"
| "meta"
| "meter"
| "nav"
| "nobr"
| "noembed"
| "noframes"
| "noscript"
| "object"
| "ol"
Expand All @@ -1160,10 +1197,12 @@ fn is_html_tag_name(namespace: Namespace, tag_name: &str) -> bool {
| "p"
| "param"
| "picture"
| "plaintext"
| "pre"
| "progress"
| "q"
| "rb"
| "rbc"
| "rp"
| "rt"
| "rtc"
Expand All @@ -1176,6 +1215,7 @@ fn is_html_tag_name(namespace: Namespace, tag_name: &str) -> bool {
| "small"
| "source"
| "span"
| "strike"
| "strong"
| "style"
| "sub"
Expand All @@ -1193,6 +1233,7 @@ fn is_html_tag_name(namespace: Namespace, tag_name: &str) -> bool {
| "title"
| "tr"
| "track"
| "tt"
| "u"
| "ul"
| "var"
Expand Down
17 changes: 17 additions & 0 deletions crates/swc_html_codegen/tests/fixture/menu/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<menu>
<li><button onclick="copy()"><img src="copy.svg" alt="Copy"></button></li>
<li><button onclick="cut()"><img src="cut.svg" alt="Cut"></button></li>
<li><button onclick="paste()"><img src="paste.svg" alt="Paste"></button></li>
</menu>
<menu><li><button onclick="copy()"><img src="copy.svg" alt="Copy"></button></li><li><button onclick="cut()"><img src="cut.svg" alt="Cut"></button></li><li><button onclick="paste()"><img src="paste.svg" alt="Paste"></button></li></menu>

<p>test</p><unknown><li>test</li></unknown>

</body>
</html>
17 changes: 17 additions & 0 deletions crates/swc_html_codegen/tests/fixture/menu/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<menu>
<li><button onclick="copy()"><img src="copy.svg" alt="Copy"></button></li>
<li><button onclick="cut()"><img src="cut.svg" alt="Cut"></button></li>
<li><button onclick="paste()"><img src="paste.svg" alt="Paste"></button></li>
</menu>
<menu><li><button onclick="copy()"><img src="copy.svg" alt="Copy"></button></li><li><button onclick="cut()"><img src="cut.svg" alt="Cut"></button></li><li><button onclick="paste()"><img src="paste.svg" alt="Paste"></button></li></menu>

<p>test</p><unknown><li>test</li></unknown>


</body></html>
14 changes: 14 additions & 0 deletions crates/swc_html_codegen/tests/fixture/menu/output.min.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html><html lang=en><head>
<title>Document</title>
</head>
<body>
<menu>
<li><button onclick=copy()><img src=copy.svg alt=Copy></button></li>
<li><button onclick=cut()><img src=cut.svg alt=Cut></button></li>
<li><button onclick=paste()><img src=paste.svg alt=Paste></button></li>
</menu>
<menu><li><button onclick=copy()><img src=copy.svg alt=Copy></button><li><button onclick=cut()><img src=cut.svg alt=Cut></button><li><button onclick=paste()><img src=paste.svg alt=Paste></button></menu>

<p>test</p><unknown><li>test</li></unknown>


35 changes: 35 additions & 0 deletions crates/swc_html_codegen/tests/fixture/optional/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
<ul><li>test</li><li>test</li><li>test</li></ul>
<ul><li>test</li><li>test</li> <li>test</li> </ul>

<menu><li>test</li></menu>
<menu> <li>test</li> </menu>
<menu> <li>test</li> <li>test</li> </menu>
<menu> <li>test</li> <li>test</li></menu>
<menu><li>test</li><li>test</li><li>test</li></menu>
<menu><li>test</li><li>test</li> <li>test</li> </menu>

<article>
<h1>FAQ</h1>
<dl><dt>What do we want?</dt><dd>Our data.</dd><dt>When do we want it?</dt><dd>Now.</dd><dt>Where is it?</dt><dd>We are not sure.</dd></dl>
Expand Down Expand Up @@ -408,6 +415,34 @@ <h1>FAQ</h1>
<table><thead><tr><td>test</td></tr></thead><tbody><tr><td>test</td></tr></tbody><tbody><tr><td>test</td></tr></tbody></table>
<table><tfoot><tr><td>test</td></tr></tfoot><tbody><tr><td>test</td></tr></tbody><tbody><tr><td>test</td></tr></tbody></table>

<p>test</p><rbc>test</rbc>
<rbc><p>test</p></rbc>
<p>test</p><keygen name="name" challenge="challenge string" keytype="type" keyparams="pqg-params">
<p>test</p><kbd>test</kbd>
<kbd><p>test</p></kbd>
<p>test</p><isindex>
<p>test</p><frameset></frameset>
<p>test</p><frame>
<p>test</p><font>test</font>
<font><p>test</p></font>
<p>test</p><big>test</big>
<big><p>test</p></big>
<p>test</p><basefont>
<p>test</p><acronym title="test">test</acronym>
<p>test</p><tt><p>test</p></tt>
<tt><p>test</p></tt>
<p>test</p><tt>test</tt>
<p>test</p><strike>test</strike>
<strike><p>test</p></strike>
<p>test</p><noframes>test</noframes>
<noframes><p>test</p></noframes>

<p>test</p><ul><li>test</li></ul>
<p>test</p><ol><li>test</li></ol>
<p>test</p><menu><li>test</li></menu>

<p>test</p><unknown>test</unknown>
<unknown><p>test</p></unknown>
</body>

</html>
37 changes: 36 additions & 1 deletion crates/swc_html_codegen/tests/fixture/optional/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
<ul><li>test</li><li>test</li><li>test</li></ul>
<ul><li>test</li><li>test</li> <li>test</li> </ul>

<menu><li>test</li></menu>
<menu> <li>test</li> </menu>
<menu> <li>test</li> <li>test</li> </menu>
<menu> <li>test</li> <li>test</li></menu>
<menu><li>test</li><li>test</li><li>test</li></menu>
<menu><li>test</li><li>test</li> <li>test</li> </menu>

<article>
<h1>FAQ</h1>
<dl><dt>What do we want?</dt><dd>Our data.</dd><dt>When do we want it?</dt><dd>Now.</dd><dt>Where is it?</dt><dd>We are not sure.</dd></dl>
Expand Down Expand Up @@ -408,6 +415,34 @@ <h1>FAQ</h1>
<table><thead><tr><td>test</td></tr></thead><tbody><tr><td>test</td></tr></tbody><tbody><tr><td>test</td></tr></tbody></table>
<table><tfoot><tr><td>test</td></tr></tfoot><tbody><tr><td>test</td></tr></tbody><tbody><tr><td>test</td></tr></tbody></table>

<p>test</p><rbc>test</rbc>
<rbc><p>test</p></rbc>
<p>test</p><keygen name="name" challenge="challenge string" keytype="type" keyparams="pqg-params">
<p>test</p><kbd>test</kbd>
<kbd><p>test</p></kbd>
<p>test</p><isindex>
<p>test</p>
<p>test</p>
<p>test</p><font>test</font>
<font><p>test</p></font>
<p>test</p><big>test</big>
<big><p>test</p></big>
<p>test</p><basefont>
<p>test</p><acronym title="test">test</acronym>
<p>test</p><tt><p>test</p></tt>
<tt><p>test</p></tt>
<p>test</p><tt>test</tt>
<p>test</p><strike>test</strike>
<strike><p>test</p></strike>
<p>test</p><noframes>test</noframes>
<noframes><p>test</p></noframes>

<p>test</p><ul><li>test</li></ul>
<p>test</p><ol><li>test</li></ol>
<p>test</p><menu><li>test</li></menu>

<p>test</p><unknown>test</unknown>
<unknown><p>test</p></unknown>


</body></html>
</isindex></body></html>
36 changes: 36 additions & 0 deletions crates/swc_html_codegen/tests/fixture/optional/output.min.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
<ul><li>test<li>test<li>test</ul>
<ul><li>test<li>test</li> <li>test</li> </ul>

<menu><li>test</menu>
<menu> <li>test</li> </menu>
<menu> <li>test</li> <li>test</li> </menu>
<menu> <li>test</li> <li>test</menu>
<menu><li>test<li>test<li>test</menu>
<menu><li>test<li>test</li> <li>test</li> </menu>

<article>
<h1>FAQ</h1>
<dl><dt>What do we want?<dd>Our data.<dt>When do we want it?<dd>Now.<dt>Where is it?<dd>We are not sure.</dl>
Expand Down Expand Up @@ -406,5 +413,34 @@ <h1>FAQ</h1>
<table><thead><tr><td>test<tbody><tr><td>test<tbody><tr><td>test</table>
<table><tfoot><tr><td>test</tfoot><tbody><tr><td>test<tbody><tr><td>test</table>

<p>test</p><rbc>test</rbc>
<rbc><p>test</p></rbc>
<p>test</p><keygen name=name challenge="challenge string" keytype=type keyparams=pqg-params>
<p>test</p><kbd>test</kbd>
<kbd><p>test</p></kbd>
<p>test</p><isindex>
<p>test</p>
<p>test</p>
<p>test</p><font>test</font>
<font><p>test</p></font>
<p>test</p><big>test</big>
<big><p>test</p></big>
<p>test</p><basefont>
<p>test</p><acronym title=test>test</acronym>
<p>test</p><tt><p>test</p></tt>
<tt><p>test</p></tt>
<p>test</p><tt>test</tt>
<p>test</p><strike>test</strike>
<strike><p>test</p></strike>
<p>test</p><noframes>test</noframes>
<noframes><p>test</p></noframes>

<p>test<ul><li>test</ul>
<p>test<ol><li>test</ol>
<p>test<menu><li>test</menu>

<p>test</p><unknown>test</unknown>
<unknown><p>test</p></unknown>


</isindex>

1 comment on commit 41dc0a7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 41dc0a7 Previous: fb74ace Ratio
es/full/minify/libraries/antd 1697303245 ns/iter (± 68660308) 1913756493 ns/iter (± 32222882) 0.89
es/full/minify/libraries/d3 414321976 ns/iter (± 12941243) 432659667 ns/iter (± 12983162) 0.96
es/full/minify/libraries/echarts 1637451361 ns/iter (± 29203576) 1932791912 ns/iter (± 36889865) 0.85
es/full/minify/libraries/jquery 95759382 ns/iter (± 4898003) 102641547 ns/iter (± 1863662) 0.93
es/full/minify/libraries/lodash 140256170 ns/iter (± 9420233) 139717114 ns/iter (± 1336052) 1.00
es/full/minify/libraries/moment 54907335 ns/iter (± 2065777) 62413668 ns/iter (± 2079298) 0.88
es/full/minify/libraries/react 17803192 ns/iter (± 397948) 21052119 ns/iter (± 484339) 0.85
es/full/minify/libraries/terser 641442206 ns/iter (± 20161950) 712001987 ns/iter (± 23749344) 0.90
es/full/minify/libraries/three 574637997 ns/iter (± 19749940) 586989030 ns/iter (± 30049785) 0.98
es/full/minify/libraries/typescript 3602930170 ns/iter (± 46482953) 4010379806 ns/iter (± 61830983) 0.90
es/full/minify/libraries/victory 762619488 ns/iter (± 10642337) 819381265 ns/iter (± 32606370) 0.93
es/full/minify/libraries/vue 153330603 ns/iter (± 2383886) 152153451 ns/iter (± 1667667) 1.01
es/full/codegen/es3 33488 ns/iter (± 2373) 37983 ns/iter (± 2104) 0.88
es/full/codegen/es5 33550 ns/iter (± 3514) 38193 ns/iter (± 672) 0.88
es/full/codegen/es2015 34318 ns/iter (± 2545) 38513 ns/iter (± 833) 0.89
es/full/codegen/es2016 33677 ns/iter (± 4111) 38191 ns/iter (± 946) 0.88
es/full/codegen/es2017 33000 ns/iter (± 3335) 39064 ns/iter (± 2309) 0.84
es/full/codegen/es2018 33445 ns/iter (± 2528) 38266 ns/iter (± 892) 0.87
es/full/codegen/es2019 33499 ns/iter (± 3155) 38386 ns/iter (± 1074) 0.87
es/full/codegen/es2020 34023 ns/iter (± 2645) 38529 ns/iter (± 1198) 0.88
es/full/all/es3 197159598 ns/iter (± 8935230) 223486143 ns/iter (± 4589446) 0.88
es/full/all/es5 182092447 ns/iter (± 7200398) 209708833 ns/iter (± 4283782) 0.87
es/full/all/es2015 146006772 ns/iter (± 5740629) 171219434 ns/iter (± 2923824) 0.85
es/full/all/es2016 146608106 ns/iter (± 5995390) 173485613 ns/iter (± 4670784) 0.85
es/full/all/es2017 147566397 ns/iter (± 6576103) 170286896 ns/iter (± 2231408) 0.87
es/full/all/es2018 140347083 ns/iter (± 5727911) 166385606 ns/iter (± 3107865) 0.84
es/full/all/es2019 138741820 ns/iter (± 5800474) 165544669 ns/iter (± 1770920) 0.84
es/full/all/es2020 134556057 ns/iter (± 6789073) 159331885 ns/iter (± 3063264) 0.84
es/full/parser 714874 ns/iter (± 47463) 847365 ns/iter (± 19842) 0.84
es/full/base/fixer 30047 ns/iter (± 2325) 34455 ns/iter (± 2497) 0.87
es/full/base/resolver_and_hygiene 88983 ns/iter (± 8073) 101256 ns/iter (± 4764) 0.88
serialization of ast node 220 ns/iter (± 22) 233 ns/iter (± 10) 0.94
serialization of serde 226 ns/iter (± 37) 242 ns/iter (± 11) 0.93

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.