Skip to content

Commit

Permalink
HTML: don't minify in style/script if it contains templates, see #621
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Oct 31, 2023
1 parent 652758d commit 47e6152
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
github.com/tdewolff/argp v0.0.0-20231030173501-fa6c54897951
github.com/tdewolff/parse/v2 v2.7.3-0.20231031130755-de6a78ee073c
github.com/tdewolff/parse/v2 v2.7.3-0.20231031132452-e7c20a5d77ab
github.com/tdewolff/test v1.0.10
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ github.com/tdewolff/argp v0.0.0-20231030173501-fa6c54897951 h1:QiakK8TMHgZfKZ2en
github.com/tdewolff/argp v0.0.0-20231030173501-fa6c54897951/go.mod h1:fF+gnKbmf3iMG+ErLiF+orMU/InyZIEnKVVigUjfriw=
github.com/tdewolff/parse/v2 v2.7.2 h1:9NdxF0nk/+lPI0YADDonSlpiY15hGcVUhXRj9hnK8sM=
github.com/tdewolff/parse/v2 v2.7.2/go.mod h1:9p2qMIHpjRSTr1qnFxQr+igogyTUTlwvf9awHSm84h8=
github.com/tdewolff/parse/v2 v2.7.3-0.20231031130755-de6a78ee073c h1:i84Tgo9fHJFtEEZ+8qJDhbA5TPInhd6aMLZIui72ivc=
github.com/tdewolff/parse/v2 v2.7.3-0.20231031130755-de6a78ee073c/go.mod h1:9p2qMIHpjRSTr1qnFxQr+igogyTUTlwvf9awHSm84h8=
github.com/tdewolff/parse/v2 v2.7.3-0.20231031132452-e7c20a5d77ab h1:4zj+h84OrVW4pljmp+LABknN7VS1IMAbeHj+eckO6Ao=
github.com/tdewolff/parse/v2 v2.7.3-0.20231031132452-e7c20a5d77ab/go.mod h1:9p2qMIHpjRSTr1qnFxQr+igogyTUTlwvf9awHSm84h8=
github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
github.com/tdewolff/test v1.0.10 h1:uWiheaLgLcNFqHcdWveum7PQfMnIUTf9Kl3bFxrIoew=
github.com/tdewolff/test v1.0.10/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
Expand Down
1 change: 0 additions & 1 deletion html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
w.Write(t.Data)
}
case html.TextToken:
// CSS and JS minifiers for inline code
if t.HasTemplate {
w.Write(t.Data)
} else if rawTagHash != 0 {
Expand Down
10 changes: 8 additions & 2 deletions html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,17 @@ func TestHTMLTemplates(t *testing.T) {
{`a<span> {{ printf " ! " }} </span>b`, `a<span> {{ printf " ! " }} </span>b`},
{`<a href={{ .Link }} />`, `<a href={{ .Link }}>`},
{`<input type="file" accept="{{ .Accept }}, image/jpeg">`, `<input type=file accept="{{ .Accept }}, image/jpeg">`},
{`<option value="0" {{ if eq .Type 0 }}selected{{ end }}>Foo</option>`, `<option value=0 {{ if eq .Type 0 }}selected{{ end }}>Foo</option>`},
{`<option value="0" {{ if eq .Type 0 }}selected{{ end }}>Foo</option>`, `<option value=0 {{ if eq .Type 0 }}selected{{ end }}>Foo`},
{`<style>a { color: {{.Color}} }</style>`, `<style>a { color: {{.Color}} }</style>`},
{`<div style=" color: {{.Color}} ">`, `<div style=" color: {{.Color}} ">`},
{`<script>alert( {{.Alert}} )</script>`, `<script>alert( {{.Alert}} )</script>`},
{`<button onclick=" alert( {{.Alert}} ) ">`, `<button onclick=" alert( {{.Alert}} ) ">`},
}

m := minify.New()
htmlMinifier := &Minifier{KeepEndTags: true, TemplateDelims: GoTemplateDelims}
m.AddFunc("text/css", css.Minify)
m.AddFunc("application/javascript", js.Minify)
htmlMinifier := &Minifier{TemplateDelims: GoTemplateDelims}
for _, tt := range htmlTests {
t.Run(tt.html, func(t *testing.T) {
r := bytes.NewBufferString(tt.html)
Expand Down

0 comments on commit 47e6152

Please sign in to comment.