Skip to content

Commit

Permalink
feat(html/minifier): Add collapse_boolean_attributes option (#4960)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 14, 2022
1 parent 5c60464 commit 3957bc6
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crates/swc_html_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ struct Minifier {
collapse_whitespaces: Option<CollapseWhitespaces>,

remove_empty_attributes: bool,
collapse_boolean_attributes: bool,
}

impl Minifier {
Expand Down Expand Up @@ -764,7 +765,10 @@ impl VisitMut for Minifier {

let is_element_html_namespace = self.current_element_namespace == Some(Namespace::HTML);

if is_element_html_namespace && self.is_boolean_attribute(&n.name) {
if self.collapse_boolean_attributes
&& is_element_html_namespace
&& self.is_boolean_attribute(&n.name)
{
n.value = None;

return;
Expand Down Expand Up @@ -880,5 +884,6 @@ pub fn minify(document: &mut Document, options: &MinifyOptions) {
collapse_whitespaces: options.collapse_whitespaces.clone(),

remove_empty_attributes: options.remove_empty_attributes,
collapse_boolean_attributes: options.collapse_boolean_attributes,
});
}
2 changes: 2 additions & 0 deletions crates/swc_html_minifier/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct MinifyOptions {
/// libraries
#[serde(default = "true_by_default")]
pub remove_empty_attributes: bool,
#[serde(default = "true_by_default")]
pub collapse_boolean_attributes: bool,
}

/// Implement default using serde.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"collapseBooleanAttributes": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>

<p><button autofocus value="next">Button</button></p>
<p><button autofocus="autofocus" value="next">Button</button></p>
<p><button autofocus=" autofocus " value="next">Button</button></p>
<p><button autofocus="true" value="next">Button</button></p>
<p><button autofocus="false" value="next">Button</button></p>

<img draggable="false">
<img draggable="true">

<img data-readonly="test">

<input disabled="disabled">
<input CHECKED = "checked" readonly="readonly">
<option name="blah" selected="selected">moo</option>
<input autofocus="autofocus">
<input required="required">
<input multiple="multiple">
<div Allowfullscreen=foo Async=foo Autofocus=foo Autoplay=foo Checked=foo Compact=foo Controls=foo Declare=foo Default=foo Defaultchecked=foo Defaultmuted=foo Defaultselected=foo Defer=foo Disabled=foo Enabled=foo Formnovalidate=foo Hidden=foo Indeterminate=foo Inert=foo Ismap=foo Itemscope=foo Loop=foo Multiple=foo Muted=foo Nohref=foo Noresize=foo Noshade=foo Novalidate=foo Nowrap=foo Open=foo Pauseonexit=foo Readonly=foo Required=foo Reversed=foo Scoped=foo Seamless=foo Selected=foo Sortable=foo Truespeed=foo Typemustmatch=foo Visible=foo></div>
<div Allowfullscreen Async Autofocus Autoplay Checked Compact Controls Declare Default Defaultchecked Defaultmuted Defaultselected Defer Disabled Enabled Formnovalidate Hidden Indeterminate Inert Ismap Itemscope Loop Multiple Muted Nohref Noresize Noshade Novalidate Nowrap Open Pauseonexit Readonly Required Reversed Scoped Seamless Selected Sortable Truespeed Typemustmatch Visible></div>

<div draggable="auto"></div>
<div draggable="true"></div>
<div draggable="false"></div>
<div draggable="foo"></div>
<div draggable></div>
<div Draggable="auto"></div>
<div Draggable="true"></div>
<div Draggable="false"></div>
<div Draggable="foo"></div>
<div Draggable></div>
<div draggable="Auto"></div>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!doctype html><html lang=en><title>Document</title><body>

<p><button autofocus value=next>Button</button></p>
<p><button autofocus=autofocus value=next>Button</button></p>
<p><button autofocus=" autofocus " value=next>Button</button></p>
<p><button autofocus=true value=next>Button</button></p>
<p><button autofocus=false value=next>Button</button></p>

<img draggable=false>
<img draggable=true>

<img data-readonly=test>

<input disabled=disabled>
<input checked=checked readonly=readonly>
<option name=blah selected=selected>moo</option>
<input autofocus=autofocus>
<input required=required>
<input multiple=multiple>
<div allowfullscreen=foo async=foo autofocus=foo autoplay=foo checked=foo compact=foo controls=foo declare=foo default=foo defaultchecked=foo defaultmuted=foo defaultselected=foo defer=foo disabled=foo enabled=foo formnovalidate=foo hidden=foo indeterminate=foo inert=foo ismap=foo itemscope=foo loop=foo multiple=foo muted=foo nohref=foo noresize=foo noshade=foo novalidate=foo nowrap=foo open=foo pauseonexit=foo readonly=foo required=foo reversed=foo scoped=foo seamless=foo selected=foo sortable=foo truespeed=foo typemustmatch=foo visible=foo></div>
<div allowfullscreen async autofocus autoplay checked compact controls declare default defaultchecked defaultmuted defaultselected defer disabled enabled formnovalidate hidden indeterminate inert ismap itemscope loop multiple muted nohref noresize noshade novalidate nowrap open pauseonexit readonly required reversed scoped seamless selected sortable truespeed typemustmatch visible></div>

<div draggable=auto></div>
<div draggable=true></div>
<div draggable=false></div>
<div draggable=foo></div>
<div draggable></div>
<div draggable=auto></div>
<div draggable=true></div>
<div draggable=false></div>
<div draggable=foo></div>
<div draggable></div>
<div draggable=Auto></div>


Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,26 @@

<img data-readonly="test">

<input disabled="disabled">
<input CHECKED = "checked" readonly="readonly">
<option name="blah" selected="selected">moo</option>
<input autofocus="autofocus">
<input required="required">
<input multiple="multiple">
<div Allowfullscreen=foo Async=foo Autofocus=foo Autoplay=foo Checked=foo Compact=foo Controls=foo Declare=foo Default=foo Defaultchecked=foo Defaultmuted=foo Defaultselected=foo Defer=foo Disabled=foo Enabled=foo Formnovalidate=foo Hidden=foo Indeterminate=foo Inert=foo Ismap=foo Itemscope=foo Loop=foo Multiple=foo Muted=foo Nohref=foo Noresize=foo Noshade=foo Novalidate=foo Nowrap=foo Open=foo Pauseonexit=foo Readonly=foo Required=foo Reversed=foo Scoped=foo Seamless=foo Selected=foo Sortable=foo Truespeed=foo Typemustmatch=foo Visible=foo></div>
<div Allowfullscreen Async Autofocus Autoplay Checked Compact Controls Declare Default Defaultchecked Defaultmuted Defaultselected Defer Disabled Enabled Formnovalidate Hidden Indeterminate Inert Ismap Itemscope Loop Multiple Muted Nohref Noresize Noshade Novalidate Nowrap Open Pauseonexit Readonly Required Reversed Scoped Seamless Selected Sortable Truespeed Typemustmatch Visible></div>

<div draggable="auto"></div>
<div draggable="true"></div>
<div draggable="false"></div>
<div draggable="foo"></div>
<div draggable></div>
<div Draggable="auto"></div>
<div Draggable="true"></div>
<div Draggable="false"></div>
<div Draggable="foo"></div>
<div Draggable></div>
<div draggable="Auto"></div>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,25 @@

<img data-readonly=test>

<input disabled>
<input checked readonly>
<option name=blah selected>moo</option>
<input autofocus>
<input required>
<input multiple>
<div allowfullscreen async autofocus autoplay checked compact controls declare default defaultchecked defaultmuted defaultselected defer disabled enabled formnovalidate hidden indeterminate inert ismap itemscope loop multiple muted nohref noresize noshade novalidate nowrap open pauseonexit readonly required reversed scoped seamless selected sortable truespeed typemustmatch visible></div>
<div allowfullscreen async autofocus autoplay checked compact controls declare default defaultchecked defaultmuted defaultselected defer disabled enabled formnovalidate hidden indeterminate inert ismap itemscope loop multiple muted nohref noresize noshade novalidate nowrap open pauseonexit readonly required reversed scoped seamless selected sortable truespeed typemustmatch visible></div>

<div draggable=auto></div>
<div draggable=true></div>
<div draggable=false></div>
<div draggable=foo></div>
<div draggable></div>
<div draggable=auto></div>
<div draggable=true></div>
<div draggable=false></div>
<div draggable=foo></div>
<div draggable></div>
<div draggable=Auto></div>


0 comments on commit 3957bc6

Please sign in to comment.