Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bindings/js/go/minify.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ func parseOptions(opts *C.MinifyOptions) (minifyOptions, error) {
}, nil
}

func toSet(tags []string) map[string]struct{} {
if len(tags) == 0 {
return nil
}

m := make(map[string]struct{}, len(tags))
for _, t := range tags {
m[strings.ToLower(t)] = struct{}{}
}
return m
}

func newMinifier(opts minifyOptions) (*minify.M, error) {
m := minify.New()

Expand All @@ -125,6 +137,7 @@ func newMinifier(opts minifyOptions) (*minify.M, error) {
KeepQuotes: opts.HTMLKeepQuotes,
KeepSpecialComments: opts.HTMLKeepSpecialComments,
KeepWhitespace: opts.HTMLKeepWhitespace,
PreserveWhitespaceTags: toSet(opts.PreserveWhitespaceTags),
}
jsMinifier := &js.Minifier{
Precision: opts.JSPrecision,
Expand Down
27 changes: 23 additions & 4 deletions html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"io"
"strings"

"github.com/tdewolff/minify/v2"
"github.com/tdewolff/parse/v2"
Expand Down Expand Up @@ -61,13 +62,31 @@ type Minifier struct {
KeepQuotes bool
KeepWhitespace bool
TemplateDelims [2]string
PreserveWhitespaceTags map[string]struct{}
}

// Minify minifies HTML data, it reads from r and writes to w.
func Minify(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error {
return (&Minifier{}).Minify(m, w, r, params)
}

func (o *Minifier) isWhitespaceTag(t Token) bool {
if o.PreserveWhitespaceTags == nil {
return false
}

if t.TokenType != html.StartTagToken && t.TokenType != html.EndTagToken {
return false
}

tag := strings.ToLower(string(t.Data))
tag = strings.TrimPrefix(tag, "<")
tag = strings.TrimPrefix(tag, "</")

_, ok := o.PreserveWhitespaceTags[strings.ToLower(string(tag))]
return ok
}

// Minify minifies HTML data, it reads from r and writes to w.
func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
var rawTagHash Hash
Expand All @@ -80,7 +99,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
}

omitSpace := true // if true the next leading space is omitted
inPre, inTemplate := false, false
inWhitespacePreservingMode, inTemplate := false, false

attrMinifyBuffer := buffer.NewWriter(make([]byte, 0, 64))
attrByteBuffer := make([]byte, 0, 64)
Expand Down Expand Up @@ -180,7 +199,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
} else {
w.Write(t.Data)
}
} else if inPre {
} else if inWhitespacePreservingMode {
w.Write(t.Data)
// omitSpace = true after block element
} else {
Expand Down Expand Up @@ -260,8 +279,8 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
omitSpace = true // EndTagToken
}

if t.Hash == Pre {
inPre = t.TokenType == html.StartTagToken
if t.Hash == Pre || o.isWhitespaceTag(t) {
inWhitespacePreservingMode = t.TokenType == html.StartTagToken
} else if t.Hash == Template {
inTemplate = t.TokenType == html.StartTagToken
}
Expand Down
38 changes: 38 additions & 0 deletions html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,44 @@ func TestMinifyErrors(t *testing.T) {
}
}

func TestHTMLPreserveWhitespaceTags(t *testing.T) {
htmlTests := []struct {
html string
expected string
}{
{
"<custom-component>\n foo\n bar\n</custom-component>",
"<custom-component>\n foo\n bar\n</custom-component>",
},
{
"<strip-component>\n foo\n bar\n</strip-component>",
"<strip-component>foo\nbar</strip-component>",
},
{
"<pre>\n foo\n bar\n</pre>",
"<pre>\n foo\n bar\n</pre>",
},
}

m := minify.New()
m.AddFunc("text/css", css.Minify)

htmlMinifier := &Minifier{
PreserveWhitespaceTags: map[string]struct{}{
"custom-component": {},
},
}

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 TestMinifyErrorPropagation(t *testing.T) {
errorTests := []struct {
html string
Expand Down