Skip to content

Commit

Permalink
fix(html/codegen): Emit newline in textarea/pre (#4919)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 10, 2022
1 parent c58f6eb commit 4f1e046
Show file tree
Hide file tree
Showing 10 changed files with 763 additions and 10 deletions.
1 change: 0 additions & 1 deletion crates/swc_html_codegen/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ where
#[derive(Debug, Default, Clone, Copy)]
pub(crate) struct Ctx {
pub need_escape_text: bool,
pub need_extra_newline_in_text: bool,
}

pub(super) struct WithCtx<'w, I: 'w + HtmlWriter> {
Expand Down
20 changes: 13 additions & 7 deletions crates/swc_html_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,19 @@ where
} else if !n.children.is_empty() {
let ctx = self.create_context_for_element(n);

let need_extra_newline =
n.namespace == Namespace::HTML && matches!(&*n.tag_name, "textarea" | "pre");

if need_extra_newline {
if let Some(Child::Text(Text { data, .. })) = &n.children.first() {
if data.contains('\n') {
newline!(self);
} else {
formatting_newline!(self);
}
}
}

if self.config.minify {
self.with_ctx(ctx)
.emit_list_for_tag_omission(TagOmissionParent::Element(n))?;
Expand Down Expand Up @@ -706,10 +719,6 @@ where
if self.ctx.need_escape_text {
let mut data = String::with_capacity(n.data.len());

if self.ctx.need_extra_newline_in_text && n.data.contains('\n') {
data.push('\n');
}

if self.config.minify {
data.push_str(&minify_text(&n.data));
} else {
Expand Down Expand Up @@ -740,12 +749,9 @@ where
_ if self.is_plaintext => false,
_ => true,
};
let need_extra_newline_in_text =
n.namespace == Namespace::HTML && matches!(&*n.tag_name, "textarea" | "pre");

Ctx {
need_escape_text,
need_extra_newline_in_text,
..self.ctx
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
<textarea>

foo</textarea>
<textarea>foo</textarea>
<textarea>foo</textarea>
<textarea>
foo</textarea>
<textarea>
foo</textarea>

</body></html>
86 changes: 86 additions & 0 deletions crates/swc_html_codegen/tests/fixture/pre-tag/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,92 @@
Oh, bring back my Bonnie to me.
</pre>

<pre>



<code>



Text



</code>


</pre>

<pre>Text</pre>

<pre><code>



Text



</code></pre>

<pre>
<code>



Text



</code>
</pre>

<pre>Text
</pre>
<pre>
Text</pre>

<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>

<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>

<pre>
<pre>Text
</pre>
</pre>

<pre><pre>Text</pre></pre>

<pre>
<pre>

Text

</pre>
</pre>

<pre>


<pre>

Text


</pre>


</pre>

</body>
</html>

88 changes: 88 additions & 0 deletions crates/swc_html_codegen/tests/fixture/pre-tag/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,94 @@
Oh, bring back my Bonnie to me.
</pre>

<pre>



<code>



Text



</code>


</pre>

<pre>
Text</pre>

<pre><code>



Text



</code></pre>

<pre>
<code>



Text



</code>
</pre>

<pre>
Text
</pre>
<pre>
Text</pre>

<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>

<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>

<pre><pre>
Text
</pre>
</pre>

<pre><pre>
Text</pre></pre>

<pre><pre>

Text

</pre>
</pre>

<pre>


<pre>

Text


</pre>


</pre>




Expand Down
84 changes: 84 additions & 0 deletions crates/swc_html_codegen/tests/fixture/pre-tag/output.min.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,90 @@
Oh, bring back my Bonnie to me.
</pre>

<pre>



<code>



Text



</code>


</pre>

<pre>Text</pre>

<pre><code>



Text



</code></pre>

<pre> <code>



Text



</code>
</pre>

<pre>
Text
</pre>
<pre>Text</pre>

<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>

<pre><code>Label current;
// Load effective address of current instruction into rcx.
__ leaq(rcx, Operand(&amp;current));
__ bind(&amp;current);
</code></pre>

<pre><pre>
Text
</pre>
</pre>

<pre><pre>Text</pre></pre>

<pre><pre>

Text

</pre>
</pre>

<pre>


<pre>

Text


</pre>


</pre>




30 changes: 30 additions & 0 deletions crates/swc_html_parser/tests/fixture/element/pre-3/dom.rust-debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
| <!DOCTYPE html>
| <html>
| <head>
| <body>
| "

"
| <pre>
| "


"
| <code>
| "



Text



"
| "


"
| "


"
23 changes: 23 additions & 0 deletions crates/swc_html_parser/tests/fixture/element/pre-3/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!doctype html>
<html>
<body>

<pre>



<code>



Text



</code>


</pre>

</body>
</html>

1 comment on commit 4f1e046

@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: 4f1e046 Previous: 064e504 Ratio
es/full/minify/libraries/antd 2233382360 ns/iter (± 53380412) 2493804130 ns/iter (± 36532669) 0.90
es/full/minify/libraries/d3 549080545 ns/iter (± 14918325) 575342686 ns/iter (± 12537320) 0.95
es/full/minify/libraries/echarts 2582677458 ns/iter (± 14901607) 2748400617 ns/iter (± 20164063) 0.94
es/full/minify/libraries/jquery 108833457 ns/iter (± 1242132) 126721793 ns/iter (± 3241692) 0.86
es/full/minify/libraries/lodash 159541800 ns/iter (± 1323965) 178554566 ns/iter (± 2276458) 0.89
es/full/minify/libraries/moment 63738409 ns/iter (± 468360) 74389372 ns/iter (± 843435) 0.86
es/full/minify/libraries/react 20634564 ns/iter (± 105681) 24905094 ns/iter (± 421524) 0.83
es/full/minify/libraries/terser 544548641 ns/iter (± 12071967) 801326409 ns/iter (± 15952977) 0.68
es/full/minify/libraries/three 780971084 ns/iter (± 11720362) 787026715 ns/iter (± 16467079) 0.99
es/full/minify/libraries/typescript 5070068953 ns/iter (± 12975794) 5646260004 ns/iter (± 99068225) 0.90
es/full/minify/libraries/victory 1008490634 ns/iter (± 5786864) 1071395246 ns/iter (± 8308781) 0.94
es/full/minify/libraries/vue 171912304 ns/iter (± 2156552) 190305271 ns/iter (± 3550764) 0.90
es/full/codegen/es3 34324 ns/iter (± 359) 41411 ns/iter (± 2266) 0.83
es/full/codegen/es5 34206 ns/iter (± 153) 41143 ns/iter (± 2121) 0.83
es/full/codegen/es2015 34246 ns/iter (± 140) 41080 ns/iter (± 3316) 0.83
es/full/codegen/es2016 34253 ns/iter (± 134) 40796 ns/iter (± 1644) 0.84
es/full/codegen/es2017 34254 ns/iter (± 162) 40926 ns/iter (± 1887) 0.84
es/full/codegen/es2018 34258 ns/iter (± 145) 40890 ns/iter (± 1746) 0.84
es/full/codegen/es2019 34218 ns/iter (± 159) 41014 ns/iter (± 3468) 0.83
es/full/codegen/es2020 34211 ns/iter (± 147) 41379 ns/iter (± 1467) 0.83
es/full/all/es3 192526616 ns/iter (± 541731) 226631105 ns/iter (± 6726768) 0.85
es/full/all/es5 182362115 ns/iter (± 685792) 217896549 ns/iter (± 6066948) 0.84
es/full/all/es2015 144903030 ns/iter (± 502551) 171272775 ns/iter (± 4745120) 0.85
es/full/all/es2016 144180921 ns/iter (± 936204) 169716274 ns/iter (± 4370032) 0.85
es/full/all/es2017 143264998 ns/iter (± 545609) 169758324 ns/iter (± 5193042) 0.84
es/full/all/es2018 141098273 ns/iter (± 964207) 168257810 ns/iter (± 4693601) 0.84
es/full/all/es2019 140652596 ns/iter (± 543978) 165756890 ns/iter (± 5395821) 0.85
es/full/all/es2020 136178140 ns/iter (± 603649) 158433219 ns/iter (± 4608595) 0.86
es/full/parser 587917 ns/iter (± 55954) 766149 ns/iter (± 48242) 0.77
es/full/base/fixer 26928 ns/iter (± 183) 37023 ns/iter (± 1957) 0.73
es/full/base/resolver_and_hygiene 140449 ns/iter (± 2314) 184427 ns/iter (± 7925) 0.76
serialization of ast node 178 ns/iter (± 1) 205 ns/iter (± 6) 0.87
serialization of serde 180 ns/iter (± 0) 208 ns/iter (± 7) 0.87

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

Please sign in to comment.