summaryrefslogtreecommitdiff
path: root/vendor/github.com/tdewolff/minify/v2/html/html.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/tdewolff/minify/v2/html/html.go')
-rw-r--r--vendor/github.com/tdewolff/minify/v2/html/html.go51
1 files changed, 29 insertions, 22 deletions
diff --git a/vendor/github.com/tdewolff/minify/v2/html/html.go b/vendor/github.com/tdewolff/minify/v2/html/html.go
index 6bec42eba..1a5aa9450 100644
--- a/vendor/github.com/tdewolff/minify/v2/html/html.go
+++ b/vendor/github.com/tdewolff/minify/v2/html/html.go
@@ -41,6 +41,13 @@ var (
////////////////////////////////////////////////////////////////
+var GoTemplateDelims = [2]string{"{{", "}}"}
+var HandlebarsTemplateDelims = [2]string{"{{", "}}"}
+var MustacheTemplateDelims = [2]string{"{{", "}}"}
+var EJSTemplateDelims = [2]string{"<%", "%>"}
+var ASPTemplateDelims = [2]string{"<%", "%>"}
+var PHPTemplateDelims = [2]string{"<?", "?>"}
+
// Minifier is an HTML minifier.
type Minifier struct {
KeepComments bool
@@ -50,6 +57,7 @@ type Minifier struct {
KeepEndTags bool
KeepQuotes bool
KeepWhitespace bool
+ TemplateDelims [2]string
}
// Minify minifies HTML data, it reads from r and writes to w.
@@ -71,7 +79,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
z := parse.NewInput(r)
defer z.Restore()
- l := html.NewLexer(z)
+ l := html.NewTemplateLexer(z, o.TemplateDelims)
tb := NewTokenBuffer(z, l)
for {
t := *tb.Shift()
@@ -126,8 +134,9 @@ 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 rawTagHash != 0 {
+ if t.HasTemplate {
+ w.Write(t.Data)
+ } else if rawTagHash != 0 {
if rawTagHash == Style || rawTagHash == Script || rawTagHash == Iframe {
var mimetype []byte
var params map[string]string
@@ -372,6 +381,9 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
break
} else if attr.Text == nil {
continue // removed attribute
+ } else if attr.HasTemplate {
+ w.Write(attr.Data)
+ continue // don't minify attributes that contain templates
}
val := attr.AttrVal
@@ -389,35 +401,30 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
attr.Hash == Action && t.Hash == Form) {
continue // omit empty attribute values
}
- if attr.Traits&caselessAttr != 0 {
- val = parse.ToLower(val)
- }
if rawTagHash != 0 && attr.Hash == Type {
rawTagMediatype = parse.Copy(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) {
+ if attr.Hash == Enctype ||
+ attr.Hash == Formenctype ||
+ attr.Hash == Accept ||
+ attr.Hash == Type && (t.Hash == A || t.Hash == Link || t.Hash == Embed || t.Hash == Object || t.Hash == Source || t.Hash == Script) {
val = minify.Mediatype(val)
}
// default attribute values can be omitted
- if !o.KeepDefaultAttrVals && (attr.Hash == Type && (t.Hash == Script && jsMimetypes[string(val)] ||
- t.Hash == Style && bytes.Equal(val, cssMimeBytes) ||
- t.Hash == Link && bytes.Equal(val, cssMimeBytes) ||
- t.Hash == Input && bytes.Equal(val, textBytes) ||
- t.Hash == Button && bytes.Equal(val, submitBytes)) ||
- attr.Hash == Language && t.Hash == Script ||
- attr.Hash == Method && bytes.Equal(val, getBytes) ||
- attr.Hash == Enctype && bytes.Equal(val, formMimeBytes) ||
+ if !o.KeepDefaultAttrVals && (attr.Hash == Type && (t.Hash == Script && jsMimetypes[string(parse.ToLower(parse.Copy(val)))] ||
+ t.Hash == Style && parse.EqualFold(val, cssMimeBytes) ||
+ t.Hash == Link && parse.EqualFold(val, cssMimeBytes) ||
+ t.Hash == Input && parse.EqualFold(val, textBytes) ||
+ t.Hash == Button && parse.EqualFold(val, submitBytes)) ||
+ attr.Hash == Method && parse.EqualFold(val, getBytes) ||
+ attr.Hash == Enctype && parse.EqualFold(val, formMimeBytes) ||
attr.Hash == Colspan && bytes.Equal(val, oneBytes) ||
attr.Hash == Rowspan && bytes.Equal(val, oneBytes) ||
- attr.Hash == Shape && bytes.Equal(val, rectBytes) ||
+ attr.Hash == Shape && parse.EqualFold(val, rectBytes) ||
attr.Hash == Span && bytes.Equal(val, oneBytes) ||
- attr.Hash == Clear && bytes.Equal(val, noneBytes) ||
- attr.Hash == Frameborder && bytes.Equal(val, oneBytes) ||
- attr.Hash == Scrolling && bytes.Equal(val, autoBytes) ||
- attr.Hash == Valuetype && bytes.Equal(val, dataBytes) ||
- attr.Hash == Media && t.Hash == Style && bytes.Equal(val, allBytes)) {
+ attr.Hash == Media && t.Hash == Style && parse.EqualFold(val, allBytes)) {
continue
}
@@ -440,7 +447,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
val = val[11:]
}
attrMinifyBuffer.Reset()
- if err := m.MinifyMimetype(jsMimeBytes, attrMinifyBuffer, buffer.NewReader(val), nil); err == nil {
+ if err := m.MinifyMimetype(jsMimeBytes, attrMinifyBuffer, buffer.NewReader(val), inlineParams); err == nil {
val = attrMinifyBuffer.Bytes()
} else if err != minify.ErrNotExist {
return minify.UpdateErrorPosition(err, z, attr.Offset)