| 368 | } |
| 369 | |
| 370 | func TestHTMLGoTemplates(t *testing.T) { |
| 371 | htmlTests := []struct { |
| 372 | html string |
| 373 | expected string |
| 374 | }{ |
| 375 | {`<a href={{ .Link }} />`, `<a href={{ .Link }}>`}, |
| 376 | {`<input type="file" accept="{{ .Accept }}, image/jpeg">`, `<input type=file accept="{{ .Accept }}, image/jpeg">`}, |
| 377 | {`<option value="0" {{ if eq .Type 0 }}selected{{ end }}>Foo</option>`, `<option value=0 {{ if eq .Type 0 }}selected{{ end }}>Foo`}, |
| 378 | {`<style>a { color: {{.Color}} }</style>`, `<style>a { color: {{.Color}} }</style>`}, |
| 379 | {`<div style=" color: {{.Color}} ">`, `<div style=" color: {{.Color}} ">`}, |
| 380 | {`<script>alert( {{.Alert}} )</script>`, `<script>alert( {{.Alert}} )</script>`}, |
| 381 | {`<button onclick=" alert( {{.Alert}} ) ">`, `<button onclick=" alert( {{.Alert}} ) ">`}, |
| 382 | {`<select>{{ range . }}<option>{{ . }}{{ end }}</select>`, `<select>{{ range . }}<option>{{ . }}{{ end }}</select>`}, |
| 383 | {`<p>Hello <code>{{""}}</code> there</p>`, `<p>Hello <code>{{""}}</code> there`}, |
| 384 | {`<select><option>Default</option>{{range $i, $lang := .Languages}}<option>{{$lang}}</option>{{end}}</select>`, `<select><option>Default{{range $i, $lang := .Languages}}<option>{{$lang}}{{end}}</select>`}, |
| 385 | {`<tr{{if .Deleted}} class="is-disabled"{{end}}>`, `<tr{{if .Deleted}} class="is-disabled"{{end}}>`}, |
| 386 | {`<a {{.Foo}}{{xx .Bar}}>`, `<a {{.Foo}}{{xx .Bar}}>`}, |
| 387 | |
| 388 | // whitespace |
| 389 | {`{{ printf " ! " }}`, `{{ printf " ! " }}`}, |
| 390 | {`a<p> {{a}} </p>b`, `a<p>{{a}}</p>b`}, |
| 391 | {`a<span> {{a}} </span>b`, `a<span> {{a}} </span>b`}, |
| 392 | {`a <span> {{a}} </span> b`, `a <span>{{a}} </span>b`}, |
| 393 | {` {{a}} {{b}} `, `{{a}} {{b}}`}, |
| 394 | } |
| 395 | |
| 396 | m := minify.New() |
| 397 | m.AddFunc("text/css", css.Minify) |
| 398 | m.AddFunc("application/javascript", js.Minify) |
| 399 | htmlMinifier := &Minifier{TemplateDelims: GoTemplateDelims} |
| 400 | for _, tt := range htmlTests { |
| 401 | t.Run(tt.html, func(t *testing.T) { |
| 402 | r := bytes.NewBufferString(tt.html) |
| 403 | w := &bytes.Buffer{} |
| 404 | err := htmlMinifier.Minify(m, w, r, nil) |
| 405 | test.Minify(t, tt.html, err, w.String(), tt.expected) |
| 406 | }) |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | func TestHTMLPHPTemplates(t *testing.T) { |
| 411 | htmlTests := []struct { |