diff --git a/crates/swc_html_minifier/src/lib.rs b/crates/swc_html_minifier/src/lib.rs
index adfb1cdf522..fa92292bdc2 100644
--- a/crates/swc_html_minifier/src/lib.rs
+++ b/crates/swc_html_minifier/src/lib.rs
@@ -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, name: &str) -> Option {
@@ -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;
}
diff --git a/crates/swc_html_minifier/tests/fixture/text/collapse-whitespace-disabled/output.min.html b/crates/swc_html_minifier/tests/fixture/text/collapse-whitespace-disabled/output.min.html
index ce968e91f42..adffc3ff018 100644
--- a/crates/swc_html_minifier/tests/fixture/text/collapse-whitespace-disabled/output.min.html
+++ b/crates/swc_html_minifier/tests/fixture/text/collapse-whitespace-disabled/output.min.html
@@ -1,6 +1,7 @@
Document
+
test
@@ -271,4 +272,6 @@
text
text
- text
\ No newline at end of file
+ text
+
+
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-19/config.json b/crates/swc_html_minifier/tests/fixture/text/inline-19/config.json
new file mode 100644
index 00000000000..ebe99c53ed1
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-19/config.json
@@ -0,0 +1,3 @@
+{
+ "collapseWhitespaces": "smart"
+}
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-19/input.html b/crates/swc_html_minifier/tests/fixture/text/inline-19/input.html
new file mode 100644
index 00000000000..e8460667741
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-19/input.html
@@ -0,0 +1,3 @@
+
+
+
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-19/output.min.html b/crates/swc_html_minifier/tests/fixture/text/inline-19/output.min.html
new file mode 100644
index 00000000000..2961bd77eb1
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-19/output.min.html
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-20/config.json b/crates/swc_html_minifier/tests/fixture/text/inline-20/config.json
new file mode 100644
index 00000000000..ebe99c53ed1
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-20/config.json
@@ -0,0 +1,3 @@
+{
+ "collapseWhitespaces": "smart"
+}
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-20/input.html b/crates/swc_html_minifier/tests/fixture/text/inline-20/input.html
new file mode 100644
index 00000000000..f47c7e1ce49
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-20/input.html
@@ -0,0 +1,3 @@
+
+
+ text
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-20/output.min.html b/crates/swc_html_minifier/tests/fixture/text/inline-20/output.min.html
new file mode 100644
index 00000000000..69a8fe5c90f
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-20/output.min.html
@@ -0,0 +1 @@
+text
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-21/config.json b/crates/swc_html_minifier/tests/fixture/text/inline-21/config.json
new file mode 100644
index 00000000000..ebe99c53ed1
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-21/config.json
@@ -0,0 +1,3 @@
+{
+ "collapseWhitespaces": "smart"
+}
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-21/input.html b/crates/swc_html_minifier/tests/fixture/text/inline-21/input.html
new file mode 100644
index 00000000000..a592c97db9f
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-21/input.html
@@ -0,0 +1,3 @@
+
+
+
text
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-21/output.min.html b/crates/swc_html_minifier/tests/fixture/text/inline-21/output.min.html
new file mode 100644
index 00000000000..d23e73ec2df
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-21/output.min.html
@@ -0,0 +1 @@
+
text
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-22/config.json b/crates/swc_html_minifier/tests/fixture/text/inline-22/config.json
new file mode 100644
index 00000000000..ebe99c53ed1
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-22/config.json
@@ -0,0 +1,3 @@
+{
+ "collapseWhitespaces": "smart"
+}
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-22/input.html b/crates/swc_html_minifier/tests/fixture/text/inline-22/input.html
new file mode 100644
index 00000000000..ddd6e808635
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-22/input.html
@@ -0,0 +1,3 @@
+
+
+ text text
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-22/output.min.html b/crates/swc_html_minifier/tests/fixture/text/inline-22/output.min.html
new file mode 100644
index 00000000000..a2f15f06738
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-22/output.min.html
@@ -0,0 +1 @@
+texttext
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-23/config.json b/crates/swc_html_minifier/tests/fixture/text/inline-23/config.json
new file mode 100644
index 00000000000..ebe99c53ed1
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-23/config.json
@@ -0,0 +1,3 @@
+{
+ "collapseWhitespaces": "smart"
+}
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-23/input.html b/crates/swc_html_minifier/tests/fixture/text/inline-23/input.html
new file mode 100644
index 00000000000..6cafaff1931
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-23/input.html
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-23/output.min.html b/crates/swc_html_minifier/tests/fixture/text/inline-23/output.min.html
new file mode 100644
index 00000000000..cfb034a13aa
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-23/output.min.html
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-24/config.json b/crates/swc_html_minifier/tests/fixture/text/inline-24/config.json
new file mode 100644
index 00000000000..ebe99c53ed1
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-24/config.json
@@ -0,0 +1,3 @@
+{
+ "collapseWhitespaces": "smart"
+}
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-24/input.html b/crates/swc_html_minifier/tests/fixture/text/inline-24/input.html
new file mode 100644
index 00000000000..dd1692729bc
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-24/input.html
@@ -0,0 +1,2 @@
+
+a
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-24/output.min.html b/crates/swc_html_minifier/tests/fixture/text/inline-24/output.min.html
new file mode 100644
index 00000000000..79c1752d9bd
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-24/output.min.html
@@ -0,0 +1 @@
+a
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-25/config.json b/crates/swc_html_minifier/tests/fixture/text/inline-25/config.json
new file mode 100644
index 00000000000..ebe99c53ed1
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-25/config.json
@@ -0,0 +1,3 @@
+{
+ "collapseWhitespaces": "smart"
+}
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-25/input.html b/crates/swc_html_minifier/tests/fixture/text/inline-25/input.html
new file mode 100644
index 00000000000..8957b27b610
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-25/input.html
@@ -0,0 +1,2 @@
+
+ a
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-25/output.min.html b/crates/swc_html_minifier/tests/fixture/text/inline-25/output.min.html
new file mode 100644
index 00000000000..41eed84be40
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-25/output.min.html
@@ -0,0 +1 @@
+a
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-26/config.json b/crates/swc_html_minifier/tests/fixture/text/inline-26/config.json
new file mode 100644
index 00000000000..ebe99c53ed1
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-26/config.json
@@ -0,0 +1,3 @@
+{
+ "collapseWhitespaces": "smart"
+}
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-26/input.html b/crates/swc_html_minifier/tests/fixture/text/inline-26/input.html
new file mode 100644
index 00000000000..50eaf6c577a
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-26/input.html
@@ -0,0 +1,2 @@
+
+aa
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/inline-26/output.min.html b/crates/swc_html_minifier/tests/fixture/text/inline-26/output.min.html
new file mode 100644
index 00000000000..078273692dd
--- /dev/null
+++ b/crates/swc_html_minifier/tests/fixture/text/inline-26/output.min.html
@@ -0,0 +1 @@
+aa
\ No newline at end of file
diff --git a/crates/swc_html_minifier/tests/fixture/text/noscript-1/output.min.html b/crates/swc_html_minifier/tests/fixture/text/noscript-1/output.min.html
index 9c443f4094f..c63e79e54c0 100644
--- a/crates/swc_html_minifier/tests/fixture/text/noscript-1/output.min.html
+++ b/crates/swc_html_minifier/tests/fixture/text/noscript-1/output.min.html
@@ -1 +1 @@
-Document