From 8e63b0bbd528b48122a3cef1164a512fbd4952e8 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Fri, 17 Jun 2022 22:11:27 +0300 Subject: [PATCH] fix(html/minifier): Fix removal of comments (#4974) --- crates/swc_html_minifier/src/lib.rs | 14 ++++++++++++++ .../tests/fixture/comment/html/input.html | 12 ++++++++++++ .../tests/fixture/comment/html/output.min.html | 6 ++++++ .../tests/fixture/element/template/input.html | 4 ++++ .../tests/fixture/element/template/output.min.html | 4 ++++ 5 files changed, 40 insertions(+) create mode 100644 crates/swc_html_minifier/tests/fixture/comment/html/input.html create mode 100644 crates/swc_html_minifier/tests/fixture/comment/html/output.min.html diff --git a/crates/swc_html_minifier/src/lib.rs b/crates/swc_html_minifier/src/lib.rs index 7836025bcaa..d916f211510 100644 --- a/crates/swc_html_minifier/src/lib.rs +++ b/crates/swc_html_minifier/src/lib.rs @@ -598,6 +598,20 @@ impl Minifier { } impl VisitMut for Minifier { + fn visit_mut_document(&mut self, n: &mut Document) { + n.visit_mut_children_with(self); + + n.children + .retain(|child| !matches!(child, Child::Comment(_))); + } + + fn visit_mut_document_fragment(&mut self, n: &mut DocumentFragment) { + n.visit_mut_children_with(self); + + n.children + .retain(|child| !matches!(child, Child::Comment(_))); + } + fn visit_mut_document_type(&mut self, n: &mut DocumentType) { n.visit_mut_children_with(self); diff --git a/crates/swc_html_minifier/tests/fixture/comment/html/input.html b/crates/swc_html_minifier/tests/fixture/comment/html/input.html new file mode 100644 index 00000000000..02aaf684967 --- /dev/null +++ b/crates/swc_html_minifier/tests/fixture/comment/html/input.html @@ -0,0 +1,12 @@ + + +
+