feat(html/minifier): Compress type for style and link tags (#4424)

This commit is contained in:
Alexander Akait 2022-04-25 16:44:53 +03:00 committed by GitHub
parent 65ced1d524
commit 25a87f0000
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 127 additions and 17 deletions

View File

@ -80,23 +80,41 @@ impl VisitMut for Minifier {
n.visit_mut_children_with(self); n.visit_mut_children_with(self);
n.children.retain(|child| !matches!(child, Child::Comment(comment) if !self.is_conditional_comment(&comment.data))); n.children.retain(|child| !matches!(child, Child::Comment(comment) if !self.is_conditional_comment(&comment.data)));
n.attributes.retain(|attribute| match &*attribute.name {
if n.tag_name.eq_str_ignore_ascii_case("script") { "type"
n.attributes.retain(|attribute| { if n.namespace == Namespace::HTML
!(attribute.namespace.is_none() && matches!(n.tag_name.as_ref(), "script")
&& attribute.prefix.is_none() && (attribute.value.is_some()
&& attribute.name.eq_str_ignore_ascii_case("type") && attribute
&& attribute .value
.value .as_ref()
.as_ref() .map(|v| {
.map(|v| { EXECUTABLE_SCRIPTS_MIME_TYPES
EXECUTABLE_SCRIPTS_MIME_TYPES .iter()
.iter() .any(|mime| v.eq_str_ignore_ascii_case(mime))
.any(|mime| v.eq_str_ignore_ascii_case(mime)) })
}) .unwrap_or_default()) =>
.unwrap_or_default()) {
}) false
} }
"type"
if n.namespace == Namespace::HTML
&& matches!(n.tag_name.as_ref(), "style" | "link")
&& (attribute.value.is_some()
&& matches!(
attribute
.value
.as_ref()
.unwrap()
.to_ascii_lowercase()
.trim(),
"text/css"
)) =>
{
false
}
_ => true,
});
} }
fn visit_mut_attribute(&mut self, n: &mut Attribute) { fn visit_mut_attribute(&mut self, n: &mut Attribute) {

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,13 @@
<!DOCTYPE html><html lang=en><head>
<title>Document</title>
<link rel=stylesheet href=a.css>
<link rel=stylesheet href=b.css>
<link rel=stylesheet href=b.css>
<link rel=stylesheet href=c.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,33 @@
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
<style>
h1 {color:red;}
p {color:blue;}
</style>
<style type="text/css">
h1 {color:red;}
p {color:blue;}
</style>
<style type="TEXT/CSS">
h1 {color:red;}
p {color:blue;}
</style>
<style type=" ">
h1 {color:red;}
p {color:blue;}
</style>
<style type=" text/css ">
h1 {color:red;}
p {color:blue;}
</style>
<style type="unknown/unknown">
h1 {color:red;}
p {color:blue;}
</style>
</head>
<body>
</body>
</html>

View File

@ -0,0 +1,31 @@
<!DOCTYPE html><html lang=en><head>
<title>Document</title>
<style>
h1 {color:red;}
p {color:blue;}
</style>
<style>
h1 {color:red;}
p {color:blue;}
</style>
<style>
h1 {color:red;}
p {color:blue;}
</style>
<style type=" ">
h1 {color:red;}
p {color:blue;}
</style>
<style>
h1 {color:red;}
p {color:blue;}
</style>
<style type=unknown/unknown>
h1 {color:red;}
p {color:blue;}
</style>
</head>
<body>
</body></html>