Skip to content

Commit 316bf2a

Browse files
committed
HTML: add KeepQuotes option, fixes #276
1 parent d60571a commit 316bf2a

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ Options:
172172
- `KeepDefaultAttrVals` preserve default attribute values such as `<script type="application/javascript">`
173173
- `KeepDocumentTags` preserve `html`, `head` and `body` tags
174174
- `KeepEndTags` preserve all end tags
175+
- `KeepQuotes` preserve quotes around attribute values
175176
- `KeepWhitespace` preserve whitespace between inline tags but still collapse multiple whitespace characters into one
176177

177178
After recent benchmarking and profiling it became really fast and minifies pages in the 10ms range, making it viable for on-the-fly minification.

cmd/minify/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ You can enable bash tab completion by using
2828
--html-keep-default-attrvals Preserve default attribute values
2929
--html-keep-document-tags Preserve html, head and body tags
3030
--html-keep-end-tags Preserve all end tags
31+
--html-keep-quotes Preserve quotes around attribute values
3132
--html-keep-whitespace Preserve whitespace characters but still collapse multiple into one
3233
-l, --list List all accepted filetypes
3334
--match string Filename pattern matching using regular expressions

cmd/minify/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func main() {
106106
flag.BoolVar(&htmlMinifier.KeepDocumentTags, "html-keep-document-tags", false, "Preserve html, head and body tags")
107107
flag.BoolVar(&htmlMinifier.KeepEndTags, "html-keep-end-tags", false, "Preserve all end tags")
108108
flag.BoolVar(&htmlMinifier.KeepWhitespace, "html-keep-whitespace", false, "Preserve whitespace characters but still collapse multiple into one")
109+
flag.BoolVar(&htmlMinifier.KeepQuotes, "html-keep-quotes", false, "Preserve quotes around attribute values")
109110
flag.IntVar(&svgMinifier.Decimals, "svg-decimals", -1, "Number of decimals to preserve in numbers, -1 is all")
110111
flag.BoolVar(&xmlMinifier.KeepWhitespace, "xml-keep-whitespace", false, "Preserve whitespace characters but still collapse multiple into one")
111112
if err := flag.Parse(os.Args[1:]); err != nil {

html/html.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Minifier struct {
3838
KeepDefaultAttrVals bool
3939
KeepDocumentTags bool
4040
KeepEndTags bool
41+
KeepQuotes bool
4142
KeepWhitespace bool
4243
}
4344

@@ -459,7 +460,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
459460
isXML := attr.Hash == html.Vocab || attr.Hash == html.Typeof || attr.Hash == html.Property || attr.Hash == html.Resource || attr.Hash == html.Prefix || attr.Hash == html.Content || attr.Hash == html.About || attr.Hash == html.Rev || attr.Hash == html.Datatype || attr.Hash == html.Inlist
460461

461462
// no quotes if possible, else prefer single or double depending on which occurs more often in value
462-
val = html.EscapeAttrVal(&attrByteBuffer, attr.AttrVal, val, isXML)
463+
val = html.EscapeAttrVal(&attrByteBuffer, attr.AttrVal, val, o.KeepQuotes || isXML)
463464
if _, err := w.Write(val); err != nil {
464465
return err
465466
}

html/html_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,26 @@ func TestHTMLKeepWhitespace(t *testing.T) {
271271
}
272272
}
273273

274+
func TestHTMLKeepQuotes(t *testing.T) {
275+
htmlTests := []struct {
276+
html string
277+
expected string
278+
}{
279+
{`<p attr="test">`, `<p attr="test">`},
280+
}
281+
282+
m := minify.New()
283+
htmlMinifier := &Minifier{KeepQuotes: true}
284+
for _, tt := range htmlTests {
285+
t.Run(tt.html, func(t *testing.T) {
286+
r := bytes.NewBufferString(tt.html)
287+
w := &bytes.Buffer{}
288+
err := htmlMinifier.Minify(m, w, r, nil)
289+
test.Minify(t, tt.html, err, w.String(), tt.expected)
290+
})
291+
}
292+
}
293+
274294
func TestHTMLURL(t *testing.T) {
275295
htmlTests := []struct {
276296
url string

0 commit comments

Comments
 (0)