Skip to content

Commit 573513d

Browse files
committed
HTML: minify whitespace around template tokens in text
1 parent 263f6d8 commit 573513d

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/fsnotify/fsnotify v1.8.0
1010
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
1111
github.com/tdewolff/argp v0.0.0-20250209172303-079abae893fb
12-
github.com/tdewolff/parse/v2 v2.7.21
12+
github.com/tdewolff/parse/v2 v2.7.22
1313
github.com/tdewolff/test v1.0.11
1414
)
1515

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
2525
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
2626
github.com/tdewolff/argp v0.0.0-20250209172303-079abae893fb h1:Id2aj2q74MC+yHBGQNeTVuQp/gqJd3vZjMmdoVPPfVc=
2727
github.com/tdewolff/argp v0.0.0-20250209172303-079abae893fb/go.mod h1:PKhwRVvnrI2gye5NRF3c4VWbE+3E9mGyRUsNWGcJlDY=
28-
github.com/tdewolff/parse/v2 v2.7.21 h1:OCuPFtGr4mXdnfKikQlUb0n654ROJANhBqCk+wioJ/A=
29-
github.com/tdewolff/parse/v2 v2.7.21/go.mod h1:I7TXO37t3aSG9SlPUBefAhgIF8nt7yYUwVGgETIoBcA=
28+
github.com/tdewolff/parse/v2 v2.7.22 h1:ROVbrjtp5RoXi22YSZaOks5DaOcXBJ3PZO5hyyQ9Bbs=
29+
github.com/tdewolff/parse/v2 v2.7.22/go.mod h1:I7TXO37t3aSG9SlPUBefAhgIF8nt7yYUwVGgETIoBcA=
3030
github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
3131
github.com/tdewolff/test v1.0.11 h1:FdLbwQVHxqG16SlkGveC0JVyrJN62COWTRyUFzfbtBE=
3232
github.com/tdewolff/test v1.0.11/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=

html/html.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
145145
w.Write(t.Data)
146146
}
147147
omitSpace = false
148+
case html.TemplateToken:
149+
w.Write(t.Data)
150+
omitSpace = false
148151
case html.TextToken:
149-
if t.HasTemplate {
150-
w.Write(t.Data)
151-
omitSpace = parse.IsWhitespace(t.Data[len(t.Data)-1])
152-
} else if rawTagHash != 0 {
152+
if rawTagHash != 0 && !t.HasTemplate {
153153
if rawTagHash == Style || rawTagHash == Script || rawTagHash == Iframe {
154154
var mimetype []byte
155155
var params map[string]string
@@ -196,7 +196,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
196196
t.Data = t.Data[:len(t.Data)-1]
197197
omitSpace = false
198198
break
199-
} else if next.TokenType == html.TextToken && !parse.IsAllWhitespace(next.Data) {
199+
} else if next.TokenType == html.TextToken && !parse.IsAllWhitespace(next.Data) || next.TokenType == html.TemplateToken {
200200
// stop looking when text encountered
201201
break
202202
} else if next.TokenType == html.StartTagToken || next.TokenType == html.EndTagToken || next.TokenType == html.SvgToken || next.TokenType == html.MathToken {
@@ -215,7 +215,6 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
215215
i++
216216
}
217217
}
218-
219218
w.Write(t.Data)
220219
}
221220
case html.StartTagToken, html.EndTagToken:

html/html_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func TestHTML(t *testing.T) {
116116
{`a <strike> b </strike> c`, `a <strike>b </strike>c`},
117117
{`a <svg>`, `a <svg>`},
118118
{"<h5>\n Lorem & ipsum\n dolor sid amet\n</h5>", "<h5>Lorem & ipsum\ndolor sid amet</h5>"},
119+
{`cats <br> dogs `, `cats<br>dogs`},
119120

120121
// from HTML Minifier
121122
{`<DIV TITLE="blah">boo</DIV>`, `<div title=blah>boo</div>`},
@@ -369,8 +370,6 @@ func TestHTMLTemplates(t *testing.T) {
369370
html string
370371
expected string
371372
}{
372-
{`a<p> {{ printf " ! " }} </p>b`, `a<p> {{ printf " ! " }} </p>b`},
373-
{`a<span> {{ printf " ! " }} </span>b`, `a<span> {{ printf " ! " }} </span>b`},
374373
{`<a href={{ .Link }} />`, `<a href={{ .Link }}>`},
375374
{`<input type="file" accept="{{ .Accept }}, image/jpeg">`, `<input type=file accept="{{ .Accept }}, image/jpeg">`},
376375
{`<option value="0" {{ if eq .Type 0 }}selected{{ end }}>Foo</option>`, `<option value=0 {{ if eq .Type 0 }}selected{{ end }}>Foo`},
@@ -382,6 +381,13 @@ func TestHTMLTemplates(t *testing.T) {
382381
{`<p>Hello <code>{{""}}</code> there</p>`, `<p>Hello <code>{{""}}</code> there`},
383382
{`<select><option>Default</option>{{range $i, $lang := .Languages}}<option>{{$lang}}</option>{{end}}</select>`, `<select><option>Default{{range $i, $lang := .Languages}}<option>{{$lang}}{{end}}</select>`},
384383
{`<tr{{if .Deleted}} class="is-disabled"{{end}}>`, `<tr{{if .Deleted}} class="is-disabled"{{end}}>`},
384+
385+
// whitespace
386+
{`{{ printf " ! " }}`, `{{ printf " ! " }}`},
387+
{`a<p> {{a}} </p>b`, `a<p>{{a}}</p>b`},
388+
{`a<span> {{a}} </span>b`, `a<span> {{a}} </span>b`},
389+
{`a <span> {{a}} </span> b`, `a <span>{{a}} </span>b`},
390+
{` {{a}} {{b}} `, `{{a}} {{b}}`},
385391
}
386392

387393
m := minify.New()

0 commit comments

Comments
 (0)