Skip to content

Commit

Permalink
Keep empty value attr on radio input, fixes #226
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Sep 13, 2018
1 parent 05bd281 commit a8ba821
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 12 additions & 3 deletions html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,17 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
if attrs[0] != nil && attrs[1] != nil {
attrs[1].Text = nil
}
}
} else if t.Hash == html.Input {
attrs := tb.Attributes(html.Type, html.Value)
if t, value := attrs[0], attrs[1]; t != nil && value != nil {
isRadio := parse.EqualFold(t.AttrVal, []byte("radio"))
if !isRadio && len(value.AttrVal) == 0 {
value.Text = nil
} else if isRadio && parse.EqualFold(value.AttrVal, []byte("on")) {
value.Text = nil
}
}
}

// write attributes
htmlEqualIdName := false
Expand Down Expand Up @@ -343,8 +353,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
attr.Hash == html.Lang ||
attr.Hash == html.Name ||
attr.Hash == html.Title ||
attr.Hash == html.Action && t.Hash == html.Form ||
attr.Hash == html.Value && t.Hash == html.Input) {
attr.Hash == html.Action && t.Hash == html.Form) {
continue // omit empty attribute values
}
if attr.Traits&caselessAttr != 0 {
Expand Down
2 changes: 2 additions & 0 deletions html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func TestHTML(t *testing.T) {
{`</span >`, `</span>`},
{`<meta name=viewport content="width=0.1, initial-scale=1.0 , maximum-scale=1000">`, `<meta name=viewport content="width=.1,initial-scale=1,maximum-scale=1e3">`},
{`<br/>`, `<br>`},
{`<input type="radio" value="">`, `<input type=radio value>`},
{`<input type="radio" value="on">`, `<input type=radio>`},

// increase coverage
{`<script style="css">js</script>`, `<script style=css>js</script>`},
Expand Down

0 comments on commit a8ba821

Please sign in to comment.