mirror of
https://github.com/swc-project/swc.git
synced 2024-11-28 11:13:43 +03:00
feat(html/minifier): Add remove_redundant_attributes
(#5023)
This commit is contained in:
parent
c7c1f635c2
commit
03d8a3a0dd
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -0,0 +1,3 @@
|
||||
{
|
||||
"removeRedundantAttributes": false
|
||||
}
|
@ -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>
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user