diff --git a/crates/swc_html_minifier/src/lib.rs b/crates/swc_html_minifier/src/lib.rs index e415928e6ae..63b73f72151 100644 --- a/crates/swc_html_minifier/src/lib.rs +++ b/crates/swc_html_minifier/src/lib.rs @@ -368,8 +368,6 @@ impl VisitMut for Minifier { return true; } - let is_empty_value = (&*attribute.value.as_ref().unwrap()).trim().is_empty(); - if self.is_default_attribute_value( n.namespace, &n.tag_name, @@ -386,12 +384,16 @@ impl VisitMut for Minifier { } _ => attribute.value.as_ref().unwrap(), }, - ) || (matches!(&*attribute.name, "id" | "class" | "style") && is_empty_value) - { + ) { return false; } - if self.is_event_handler_attribute(&attribute.name) && is_empty_value { + let value = &*attribute.value.as_ref().unwrap(); + + if (matches!(&*attribute.name, "id") && value.is_empty()) + || (matches!(&*attribute.name, "class" | "style") && value.trim().is_empty()) + || self.is_event_handler_attribute(&attribute.name) && value.trim().is_empty() + { return false; } diff --git a/crates/swc_html_minifier/tests/fixture/attribute/empty-attributes/input.html b/crates/swc_html_minifier/tests/fixture/attribute/empty-attributes/input.html new file mode 100644 index 00000000000..6fef2003842 --- /dev/null +++ b/crates/swc_html_minifier/tests/fixture/attribute/empty-attributes/input.html @@ -0,0 +1,20 @@ + + + + Document + + +
test
+
test
+ +
test
+
test
+ +
test
+
test
+
test
+
test
+
test
+ + + \ No newline at end of file diff --git a/crates/swc_html_minifier/tests/fixture/attribute/empty-attributes/output.min.html b/crates/swc_html_minifier/tests/fixture/attribute/empty-attributes/output.min.html new file mode 100644 index 00000000000..c941e4e9405 --- /dev/null +++ b/crates/swc_html_minifier/tests/fixture/attribute/empty-attributes/output.min.html @@ -0,0 +1,15 @@ +Document + +
test
+
test
+ +
test
+
test
+ +
test
+
test
+
test
+
test
+
test
+ +