mirror of
https://github.com/swc-project/swc.git
synced 2024-12-22 21:21:31 +03:00
feat(html/minifier): Remove duplicate attributes (#4474)
This commit is contained in:
parent
f891c83f85
commit
750d6551fc
@ -1,5 +1,7 @@
|
||||
#![deny(clippy::all)]
|
||||
|
||||
use swc_atoms::JsWord;
|
||||
use swc_common::collections::AHashSet;
|
||||
use swc_html_ast::*;
|
||||
use swc_html_visit::{VisitMut, VisitMutWith};
|
||||
|
||||
@ -280,28 +282,32 @@ impl VisitMut for Minifier {
|
||||
n.visit_mut_children_with(self);
|
||||
|
||||
n.children.retain(|child| !matches!(child, Child::Comment(comment) if !self.is_conditional_comment(&comment.data)));
|
||||
|
||||
let mut already_seen: AHashSet<JsWord> = Default::default();
|
||||
|
||||
n.attributes.retain(|attribute| {
|
||||
if already_seen.contains(&attribute.name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
already_seen.insert(attribute.name.clone());
|
||||
|
||||
if attribute.value.is_none() {
|
||||
return true;
|
||||
}
|
||||
|
||||
match &*attribute.name {
|
||||
_ if self.is_default_attribute_value(
|
||||
n.namespace,
|
||||
&n.tag_name,
|
||||
&attribute.name,
|
||||
attribute.value.as_ref().unwrap(),
|
||||
) =>
|
||||
{
|
||||
false
|
||||
}
|
||||
_ if matches!(&*attribute.name, "id" | "class" | "style")
|
||||
&& (&*attribute.value.as_ref().unwrap()).trim().is_empty() =>
|
||||
{
|
||||
false
|
||||
}
|
||||
_ => true,
|
||||
if self.is_default_attribute_value(
|
||||
n.namespace,
|
||||
&n.tag_name,
|
||||
&attribute.name,
|
||||
attribute.value.as_ref().unwrap(),
|
||||
) || (matches!(&*attribute.name, "id" | "class" | "style")
|
||||
&& (&*attribute.value.as_ref().unwrap()).trim().is_empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -14,5 +14,8 @@
|
||||
></div>
|
||||
|
||||
<img src="test.png" alt="one" alt="two">
|
||||
<img src="test.png" alt="one" alt="two" alt="three">
|
||||
<img src="test.png" alt="one" alt="two" alt="three" src="test2.png">
|
||||
|
||||
</body>
|
||||
</html>
|
@ -5,8 +5,11 @@
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<div data-test=one data-test=two></div>
|
||||
<div data-test=one></div>
|
||||
|
||||
<img src=test.png alt=one>
|
||||
<img src=test.png alt=one>
|
||||
<img src=test.png alt=one>
|
||||
|
||||
<img src=test.png alt=one alt=two>
|
||||
|
||||
</body></html>
|
||||
|
Loading…
Reference in New Issue
Block a user