Skip to content

Commit

Permalink
Merge 7542379 into df39447
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-bacart committed Jul 4, 2020
2 parents df39447 + 7542379 commit 9026eeb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -173,6 +173,7 @@ Options:
- `KeepEndTags` preserve all end tags
- `KeepQuotes` preserve quotes around attribute values
- `KeepWhitespace` preserve whitespace between inline tags but still collapse multiple whitespace characters into one
- `DontLowercaseAttributes` prevent attributes lower case

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

Expand Down
1 change: 1 addition & 0 deletions cmd/minify/main.go
Expand Up @@ -126,6 +126,7 @@ func run() int {
flag.BoolVar(&htmlMinifier.KeepEndTags, "html-keep-end-tags", false, "Preserve all end tags")
flag.BoolVar(&htmlMinifier.KeepWhitespace, "html-keep-whitespace", false, "Preserve whitespace characters but still collapse multiple into one")
flag.BoolVar(&htmlMinifier.KeepQuotes, "html-keep-quotes", false, "Preserve quotes around attribute values")
flag.BoolVar(&htmlMinifier.DontLowercaseAttributes, "html-dont-lowercase", false, "Prevent attributes lower case")
flag.IntVar(&jsonMinifier.Precision, "json-precision", 0, "Number of significant digits to preserve in numbers, 0 is all")
flag.IntVar(&svgMinifier.Decimals, "svg-decimals", 0, "Number of significant digits to preserve in numbers, 0 is all (DEPRECATED")
flag.IntVar(&svgMinifier.Precision, "svg-precision", 0, "Number of significant digits to preserve in numbers, 0 is all")
Expand Down
5 changes: 4 additions & 1 deletion html/html.go
Expand Up @@ -40,6 +40,7 @@ type Minifier struct {
KeepEndTags bool
KeepQuotes bool
KeepWhitespace bool
DontLowercaseAttributes bool
}

// Minify minifies HTML data, it reads from r and writes to w.
Expand Down Expand Up @@ -394,7 +395,9 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
continue // omit empty attribute values
}
if attr.Traits&caselessAttr != 0 {
val = parse.ToLower(val)
if !o.DontLowercaseAttributes {
val = parse.ToLower(val)
}
if attr.Hash == Enctype || attr.Hash == Codetype || attr.Hash == Accept || attr.Hash == Type && (t.Hash == A || t.Hash == Link || t.Hash == Embed || t.Hash == Object || t.Hash == Source || t.Hash == Script || t.Hash == Style) {
val = minify.Mediatype(val)
}
Expand Down
21 changes: 21 additions & 0 deletions html/html_test.go
Expand Up @@ -299,6 +299,27 @@ func TestHTMLKeepQuotes(t *testing.T) {
}
}

func TestHTMLDontLowercaseAttributes(t *testing.T) {
htmlTests := []struct {
html string
expected string
}{
{`<p attr="Test">`, `<p attr=Test>`},
{`<html lang="{{.Lang}}">`, `<html lang={{.Lang}}>`},
}

m := minify.New()
htmlMinifier := &Minifier{DontLowercaseAttributes: true}
for _, tt := range htmlTests {
t.Run(tt.html, func(t *testing.T) {
r := bytes.NewBufferString(tt.html)
w := &bytes.Buffer{}
err := htmlMinifier.Minify(m, w, r, nil)
test.Minify(t, tt.html, err, w.String(), tt.expected)
})
}
}

func TestHTMLURL(t *testing.T) {
htmlTests := []struct {
url string
Expand Down

0 comments on commit 9026eeb

Please sign in to comment.