Skip to content

Commit

Permalink
fix(html/minifier): Small fix (#5079)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jul 3, 2022
1 parent 279d4a3 commit 5ac5ae2
Show file tree
Hide file tree
Showing 27 changed files with 65 additions and 7 deletions.
13 changes: 8 additions & 5 deletions crates/swc_html_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,11 +1227,6 @@ impl Minifier {

result
});

// Remove all leading and trailing whitespaces for the `body` element
if namespace == Namespace::HTML && tag_name == "body" {
self.remove_leading_and_trailing_whitespaces(children);
}
}

fn get_attribute_value(&self, attributes: &Vec<Attribute>, name: &str) -> Option<JsWord> {
Expand Down Expand Up @@ -1714,6 +1709,14 @@ impl VisitMut for Minifier {

n.visit_mut_children_with(self);

// Remove all leading and trailing whitespaces for the `body` element
if n.namespace == Namespace::HTML
&& &*n.tag_name == "body"
&& self.need_collapse_whitespace()
{
self.remove_leading_and_trailing_whitespaces(&mut n.children);
}

if self.need_collapse_whitespace() {
self.descendant_of_pre = old_descendant_of_pre;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!doctype html><html lang=en><head>
<title>Document</title>
</head>
<body>
<div> test </div>

<div>
Expand Down Expand Up @@ -271,4 +272,6 @@
<div> text </div>

<span> text </span>
<span> text </span>
<span> text </span>


Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"collapseWhitespaces": "smart"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!doctype html>
<html lang="en">
<div> <a href="#"> <span> <b> foo </b> z <i> bar </i> </span> </a> </div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html lang=en><div><a href=#><span><b>foo</b>z <i>bar</i></span></a></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"collapseWhitespaces": "smart"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!doctype html>
<html lang="en">
<span> <!-- test --> text <!-- test --></span>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html lang=en><span>text</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"collapseWhitespaces": "smart"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!doctype html>
<html lang="en">
<div><span> <!-- test --> text <!-- test --></span></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html lang=en><div><span>text</span></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"collapseWhitespaces": "smart"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!doctype html>
<html lang="en">
<span> <!-- test --> text <!-- test --></span> <span> <!-- test --> text <!-- test --></span>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html lang=en><span>text</span> <span>text</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"collapseWhitespaces": "smart"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<svg> <linearGradient id=gradient /> </svg>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><svg><linearGradient id=gradient /></svg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"collapseWhitespaces": "smart"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<span>a</span> <svg> <linearGradient id=gradient /> </svg>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><span>a</span> <svg><linearGradient id=gradient /></svg>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"collapseWhitespaces": "smart"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<svg> <linearGradient id=gradient /> </svg> <span>a</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><svg><linearGradient id=gradient /></svg><span>a</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"collapseWhitespaces": "smart"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<span>a</span> <svg> <linearGradient id=gradient /> </svg> <span>a</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><span>a</span> <svg><linearGradient id=gradient /> </svg><span>a</span>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!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><div><span>test</span><noscript><a href=#>External Link</a></noscript></div><div><span>test</span><noscript> <a href=#>External Link</a></noscript></div>
<!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><div><span>test</span><noscript><a href=#>External Link</a></noscript></div><div><span>test</span><noscript><a href=#>External Link</a></noscript></div>

1 comment on commit 5ac5ae2

@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: 5ac5ae2 Previous: 52bfb18 Ratio
es/full/minify/libraries/antd 1769038572 ns/iter (± 154365477) 1596493295 ns/iter (± 20183245) 1.11
es/full/minify/libraries/d3 451734827 ns/iter (± 78228681) 388741766 ns/iter (± 4922363) 1.16
es/full/minify/libraries/echarts 1712791871 ns/iter (± 40805657) 1582451201 ns/iter (± 21494229) 1.08
es/full/minify/libraries/jquery 102132990 ns/iter (± 3887183) 84495686 ns/iter (± 1237417) 1.21
es/full/minify/libraries/lodash 136368920 ns/iter (± 2583670) 111786470 ns/iter (± 1821325) 1.22
es/full/minify/libraries/moment 56868943 ns/iter (± 3929842) 49728739 ns/iter (± 997595) 1.14
es/full/minify/libraries/react 17689803 ns/iter (± 325544) 17163497 ns/iter (± 676713) 1.03
es/full/minify/libraries/terser 619596760 ns/iter (± 16669718) 574415604 ns/iter (± 8079556) 1.08
es/full/minify/libraries/three 551641565 ns/iter (± 3661553) 528615615 ns/iter (± 3963862) 1.04
es/full/minify/libraries/typescript 3574033978 ns/iter (± 77681008) 3370250648 ns/iter (± 41105178) 1.06
es/full/minify/libraries/victory 753237471 ns/iter (± 16067347) 708786419 ns/iter (± 4416866) 1.06
es/full/minify/libraries/vue 139166993 ns/iter (± 4074559) 127487716 ns/iter (± 1950422) 1.09
es/full/codegen/es3 32938 ns/iter (± 745) 31927 ns/iter (± 973) 1.03
es/full/codegen/es5 32863 ns/iter (± 645) 31923 ns/iter (± 737) 1.03
es/full/codegen/es2015 32849 ns/iter (± 1784) 32031 ns/iter (± 623) 1.03
es/full/codegen/es2016 32991 ns/iter (± 1431) 31881 ns/iter (± 1201) 1.03
es/full/codegen/es2017 32845 ns/iter (± 429) 31974 ns/iter (± 607) 1.03
es/full/codegen/es2018 32917 ns/iter (± 575) 31886 ns/iter (± 799) 1.03
es/full/codegen/es2019 32952 ns/iter (± 848) 31909 ns/iter (± 760) 1.03
es/full/codegen/es2020 32915 ns/iter (± 1155) 31958 ns/iter (± 904) 1.03
es/full/all/es3 186835607 ns/iter (± 12908837) 179292118 ns/iter (± 2182919) 1.04
es/full/all/es5 171483888 ns/iter (± 6768086) 167118917 ns/iter (± 3267526) 1.03
es/full/all/es2015 138435531 ns/iter (± 3712654) 136186840 ns/iter (± 2313729) 1.02
es/full/all/es2016 142686853 ns/iter (± 5360513) 135109568 ns/iter (± 2714290) 1.06
es/full/all/es2017 140380803 ns/iter (± 5097830) 134834033 ns/iter (± 2788975) 1.04
es/full/all/es2018 139172698 ns/iter (± 5430193) 133287256 ns/iter (± 2197643) 1.04
es/full/all/es2019 137487564 ns/iter (± 3575824) 132731681 ns/iter (± 1841805) 1.04
es/full/all/es2020 133735339 ns/iter (± 4995920) 128057929 ns/iter (± 2266822) 1.04
es/full/parser 703426 ns/iter (± 22657) 679565 ns/iter (± 19092) 1.04
es/full/base/fixer 29068 ns/iter (± 723) 27929 ns/iter (± 844) 1.04
es/full/base/resolver_and_hygiene 88468 ns/iter (± 4495) 84437 ns/iter (± 4819) 1.05
serialization of ast node 214 ns/iter (± 1) 211 ns/iter (± 5) 1.01
serialization of serde 227 ns/iter (± 3) 223 ns/iter (± 5) 1.02

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

Please sign in to comment.