Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 26, 2022
1 parent ad5953c commit c22f1cc
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 50 deletions.
15 changes: 5 additions & 10 deletions crates/swc_html_minifier/src/lib.rs
Expand Up @@ -323,6 +323,8 @@ struct Minifier {
minify_json: bool,
minify_js: bool,
minify_css: bool,

sort_space_separated_attribute_values: bool,
}

fn get_white_space(namespace: Namespace, tag_name: &str) -> WhiteSpace {
Expand All @@ -333,9 +335,6 @@ fn get_white_space(namespace: Namespace, tag_name: &str) -> WhiteSpace {
},
_ => WhiteSpace::Normal,
}
preserve_comments: Option<Vec<CachedRegex>>,
minify_conditional_comments: bool,
sort_unordered_attribute_values: bool,
}

impl Minifier {
Expand Down Expand Up @@ -1373,9 +1372,7 @@ impl Minifier {
minify_js: self.minify_js,
minify_json: self.minify_json,
minify_css: self.minify_css,
preserve_comments: self.preserve_comments.clone(),
minify_conditional_comments: self.minify_conditional_comments,
sort_unordered_attribute_values: self.sort_unordered_attribute_values,
sort_space_separated_attribute_values: self.sort_space_separated_attribute_values,
},
);

Expand Down Expand Up @@ -1544,7 +1541,7 @@ impl VisitMut for Minifier {
{
let mut values = value.split_whitespace().collect::<Vec<_>>();

if self.sort_unordered_attribute_values
if self.sort_space_separated_attribute_values
&& self.is_attribute_value_unordered_set(
self.current_element.as_ref().unwrap(),
&n.name,
Expand Down Expand Up @@ -1843,9 +1840,7 @@ fn create_minifier(context_element: Option<&Element>, options: &MinifyOptions) -
minify_json: options.minify_json,
minify_css: options.minify_css,

preserve_comments: options.preserve_comments.clone(),
minify_conditional_comments: options.minify_conditional_comments,
sort_unordered_attribute_values: options.sort_unordered_attribute_values,
sort_space_separated_attribute_values: options.sort_space_separated_attribute_values,
}
}

Expand Down
6 changes: 1 addition & 5 deletions crates/swc_html_minifier/src/option.rs
Expand Up @@ -33,13 +33,9 @@ pub struct MinifyOptions {
pub minify_json: bool,
#[serde(default = "true_by_default")]
pub minify_css: bool,
#[serde(default = "default_preserve_comments")]
pub preserve_comments: Option<Vec<CachedRegex>>,
#[serde(default = "true_by_default")]
pub minify_conditional_comments: bool,
/// Sorting the values of `class`, `rel`, etc. of attributes
#[serde(default = "true_by_default")]
pub sort_unordered_attribute_values: bool,
pub sort_space_separated_attribute_values: bool,
}

/// Implement default using serde.
Expand Down
@@ -1,3 +1,3 @@
{
"sortUnorderedAttributeValues": false
"sortSpaceSeparatedAttributeValues": false
}
@@ -1,3 +1 @@
<!doctype html><html lang=en><meta charset=UTF-8><title>Document</title><body>
<div class="b a d"></div>

<!doctype html><html lang=en><meta charset=UTF-8><title>Document</title><div class="b a d"></div>
Expand Up @@ -11,12 +11,10 @@
<td headers=phone>+45342323</td>
<td headers=addr>Rosevn 56,4300 Sandnes,Norway</td>
</tr>
</table>
<tr>
<td headers="a name">John Doe</td>
<td headers="a email">someone@example.com</td>
<td headers="a phone">+45342323</td>
<td headers="addr b">Rosevn 56,4300 Sandnes,Norway</td>
</tr>
</table>

</table>
@@ -1,6 +1 @@
<!doctype html><html lang=en><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><div>test</div>
<!doctype html><html lang=en><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><link href=default.css rel=stylesheet title="Default Style"><link href=fancy.css rel="alternate stylesheet" title=Fancy><link href=basic.css rel="alternate stylesheet" title=Basic><link rel="bar foo stylesheet" href=d.css><body>
<!doctype html><html lang=en><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><link href=default.css rel=stylesheet title="Default Style"><link href=fancy.css rel="alternate stylesheet" title=Fancy><link href=basic.css rel="alternate stylesheet" title=Basic><link rel="bar foo stylesheet" href=d.css><link rel="bar foo stylesheet" href=d.css blocking="a render"><body>
<!doctype html><html lang=en><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><link href=default.css rel=stylesheet title="Default Style"><link href=fancy.css rel="alternate stylesheet" title=Fancy><link href=basic.css rel="alternate stylesheet" title=Basic><link rel="bar foo stylesheet" href=d.css><link rel="bar foo stylesheet" href=d.css blocking="a render"><link rel=icon href=demo_icon.gif type=image/gif sizes="16x16 32x32"><link rel=apple-touch-icon-precomposed sizes="114x114 512x512" href=apple-icon-114.png type=image/png><link rel=apple-touch-icon sizes="114x114 512x512" href=apple-icon-114.png type=image/png><link rel=icon href=/favicon.ico sizes=any><link rel=icon href=/icon.svg type=image/svg+xml><link rel=apple-touch-icon href=/apple-touch-icon.png><link rel=manifest href=/manifest.webmanifest><body>
<div>test</div>

<!doctype html><html lang=en><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><link href=default.css rel=stylesheet title="Default Style"><link href=fancy.css rel="alternate stylesheet" title=Fancy><link href=basic.css rel="alternate stylesheet" title=Basic><link rel="bar foo stylesheet" href=d.css><link rel="bar foo stylesheet" href=d.css blocking="a render"><link rel=icon href=demo_icon.gif type=image/gif sizes="16x16 32x32"><link rel=apple-touch-icon-precomposed sizes="114x114 512x512" href=apple-icon-114.png type=image/png><link rel=apple-touch-icon sizes="114x114 512x512" href=apple-icon-114.png type=image/png><link rel=icon href=/favicon.ico sizes=any><link rel=icon href=/icon.svg type=image/svg+xml><link rel=apple-touch-icon href=/apple-touch-icon.png><link rel=manifest href=/manifest.webmanifest><div>test</div>
@@ -1,12 +1,7 @@
<!doctype html><html lang=en><title>Document</title><a href=# rel="value1 nofollow">Link</a>
<a href=# rel="value1 nofollow">Link</a>
<a href=# rel=nofollow>Link</a>
<!doctype html><html lang=en><title>Document</title><body>
<a href=# rel="nofollow value1">Link</a>
<!doctype html><html lang=en><title>Document</title><a href=# rel="nofollow value1">Link</a>
<a href=# rel="nofollow value1">Link</a>
<a href=# rel=nofollow>Link</a>
<form rel="nofollow noreferrer" action=mypage.php>
<input type=search placeholder="search here">
<input type=button value=search>
</form>

</form>
@@ -1,4 +1 @@
<!doctype html><html lang=en><meta charset=UTF-8><meta name=viewport content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"><meta http-equiv=X-UA-Compatible content="ie=edge"><title>Document</title><iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms" src=https://platform.twitter.com/widgets/tweet_button.html style=border:0;width:130px;height:20px></iframe>
<!doctype html><html lang=en><meta charset=UTF-8><meta name=viewport content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"><meta http-equiv=X-UA-Compatible content="ie=edge"><title>Document</title><body>
<iframe sandbox="allow-forms allow-popups allow-same-origin allow-scripts" src=https://platform.twitter.com/widgets/tweet_button.html style=border:0;width:130px;height:20px></iframe>

<!doctype html><html lang=en><meta charset=UTF-8><meta name=viewport content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"><meta http-equiv=X-UA-Compatible content="ie=edge"><title>Document</title><iframe sandbox="allow-forms allow-popups allow-same-origin allow-scripts" src=https://platform.twitter.com/widgets/tweet_button.html style=border:0;width:130px;height:20px></iframe>
@@ -1,7 +1,5 @@
<!doctype html><html lang=en><meta charset=UTF-8><meta name=viewport content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"><title>Document</title><body>
<form oninput="result.value=parseInt(a.value)+parseInt(b.value)">
<!doctype html><html lang=en><meta charset=UTF-8><meta name=viewport content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"><title>Document</title><form oninput="result.value=parseInt(a.value)+parseInt(b.value)">
<input type=range id=b name=b value=50> +
<input type=number id=a name=a value=10> =
<output name=result for="a b">60</output>
</form>

</form>
Expand Up @@ -54,7 +54,4 @@
alert('test')
</script>
</math>
</math>
<script blocking="a render">console.log("block")</script>


<script blocking="a render">console.log("block")</script>
Expand Up @@ -24,6 +24,4 @@
}
</style>
</math>
</math>
<style blocking="a render">a{color:red}</style>

<style blocking="a render">a{color:red}</style>

0 comments on commit c22f1cc

Please sign in to comment.