From 03d8a3a0ddb843ded43134265973557dd48e14a7 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Wed, 22 Jun 2022 08:54:25 +0300 Subject: [PATCH] feat(html/minifier): Add `remove_redundant_attributes` (#5023) --- crates/swc_html_minifier/src/lib.rs | 35 +++++++++++-------- crates/swc_html_minifier/src/option.rs | 2 ++ .../config.json | 3 ++ .../input.html | 15 ++++++++ .../output.min.html | 3 ++ 5 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/config.json create mode 100644 crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/input.html create mode 100644 crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/output.min.html diff --git a/crates/swc_html_minifier/src/lib.rs b/crates/swc_html_minifier/src/lib.rs index 386e0e1ac19..b3639ffab78 100644 --- a/crates/swc_html_minifier/src/lib.rs +++ b/crates/swc_html_minifier/src/lib.rs @@ -278,6 +278,7 @@ struct Minifier { collapse_whitespaces: Option, remove_empty_attributes: bool, + remove_redundant_attributes: bool, collapse_boolean_attributes: bool, minify_json: bool, minify_js: bool, @@ -909,6 +910,7 @@ impl Minifier { force_set_html5_doctype: self.force_set_html5_doctype, collapse_whitespaces: self.collapse_whitespaces.clone(), remove_empty_attributes: self.remove_empty_attributes, + remove_redundant_attributes: self.remove_empty_attributes, collapse_boolean_attributes: self.collapse_boolean_attributes, minify_js: self.minify_js, minify_json: self.minify_json, @@ -1005,23 +1007,25 @@ impl VisitMut for Minifier { return true; } - if self.is_default_attribute_value( - n.namespace, - &n.tag_name, - &attribute.name, - match &*n.tag_name { - "script" if matches!(n.namespace, Namespace::HTML | Namespace::SVG) => { - let original_value = attribute.value.as_ref().unwrap(); + if self.remove_redundant_attributes + && self.is_default_attribute_value( + n.namespace, + &n.tag_name, + &attribute.name, + match &*n.tag_name { + "script" if matches!(n.namespace, Namespace::HTML | Namespace::SVG) => { + let original_value = attribute.value.as_ref().unwrap(); - if let Some(next) = original_value.split(';').next() { - next - } else { - original_value + if let Some(next) = original_value.split(';').next() { + next + } else { + original_value + } } - } - _ => attribute.value.as_ref().unwrap(), - }, - ) { + _ => attribute.value.as_ref().unwrap(), + }, + ) + { return false; } @@ -1321,6 +1325,7 @@ fn create_minifier(context_element: Option<&Element>, options: &MinifyOptions) - collapse_whitespaces: options.collapse_whitespaces.clone(), remove_empty_attributes: options.remove_empty_attributes, + remove_redundant_attributes: options.remove_redundant_attributes, collapse_boolean_attributes: options.collapse_boolean_attributes, minify_js: options.minify_js, diff --git a/crates/swc_html_minifier/src/option.rs b/crates/swc_html_minifier/src/option.rs index 50800a6f78a..1158e70a251 100644 --- a/crates/swc_html_minifier/src/option.rs +++ b/crates/swc_html_minifier/src/option.rs @@ -18,6 +18,8 @@ pub struct MinifyOptions { #[serde(default = "true_by_default")] pub remove_empty_attributes: bool, #[serde(default = "true_by_default")] + pub remove_redundant_attributes: bool, + #[serde(default = "true_by_default")] pub collapse_boolean_attributes: bool, #[serde(default = "true_by_default")] pub minify_js: bool, diff --git a/crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/config.json b/crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/config.json new file mode 100644 index 00000000000..fd42b4a55ac --- /dev/null +++ b/crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/config.json @@ -0,0 +1,3 @@ +{ + "removeRedundantAttributes": false +} \ No newline at end of file diff --git a/crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/input.html b/crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/input.html new file mode 100644 index 00000000000..4a045e0a326 --- /dev/null +++ b/crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/input.html @@ -0,0 +1,15 @@ + + + + Document + + + + + + + + +
test
+ + \ No newline at end of file diff --git a/crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/output.min.html b/crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/output.min.html new file mode 100644 index 00000000000..e39c53fcdf3 --- /dev/null +++ b/crates/swc_html_minifier/tests/fixture/attribute/disabled-remove_redundant_attributes/output.min.html @@ -0,0 +1,3 @@ +Document +
test
+