feat(html/minifier): Add remove_redundant_attributes (#5023)

This commit is contained in:
Alexander Akait 2022-06-22 08:54:25 +03:00 committed by GitHub
parent c7c1f635c2
commit 03d8a3a0dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 15 deletions

View File

@ -278,6 +278,7 @@ struct Minifier {
collapse_whitespaces: Option<CollapseWhitespaces>,
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,

View File

@ -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,

View File

@ -0,0 +1,3 @@
{
"removeRedundantAttributes": false
}

View File

@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
<link rel="stylesheet" href="a.css">
<link rel="stylesheet" href="b.css" type="text/css">
<link rel="stylesheet" href="b.css" type="TEXT/CSS">
<link rel="stylesheet" href="c.css" type=" text/css ">
<link rel="stylesheet" href="d.css" type="">
<link rel="stylesheet" href="d.css" type="unknown/unknown">
</head>
<body>
<div>test</div>
</body>
</html>

View File

@ -0,0 +1,3 @@
<!doctype html><html lang=en><title>Document</title><link rel=stylesheet href=a.css><link rel=stylesheet href=b.css type=text/css><link rel=stylesheet href=b.css type=TEXT/CSS><link rel=stylesheet href=c.css type=" text/css "><link rel=stylesheet href=d.css type=""><link rel=stylesheet href=d.css type=unknown/unknown><body>
<div>test</div>