From 342cbc1974162db0ad3327f7a42a623b2cd3ebbc Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Thu, 11 Jan 2024 11:23:27 -0300 Subject: [PATCH] HTML: rename KeepConditionalComments => KeepSpecialComments, fixes #657 --- cmd/minify/bash_completion | 2 +- cmd/minify/main.go | 3 ++- html/html.go | 43 +++++++++++++++++++++++--------------- html/html_test.go | 9 ++++---- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/cmd/minify/bash_completion b/cmd/minify/bash_completion index e117c67029..72d9738f28 100644 --- a/cmd/minify/bash_completion +++ b/cmd/minify/bash_completion @@ -11,7 +11,7 @@ _minify_complete() { local cur prev flags mimes types cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" - flags="-a --all --bundle --exclude --ext --include -l --list --match -o --output -p --preserve -q --quiet -r --recursive --type --url -v --verbose --version -w --watch --css-precision --html-keep-comments --html-keep-conditional-comments --html-keep-default-attrvals --html-keep-document-tags --html-keep-end-tags --html-keep-quotes --html-keep-whitespace --js-precision --js-keep-var-names --js-version --json-precision --json-keep-numbers --svg-keep-comments --svg-precision -s --sync --xml-keep-whitespace" + flags="-a --all --bundle --exclude --ext --include -l --list --match -o --output -p --preserve -q --quiet -r --recursive --type --url -v --verbose --version -w --watch --css-precision --html-keep-comments --html-keep-special-comments --html-keep-default-attrvals --html-keep-document-tags --html-keep-end-tags --html-keep-quotes --html-keep-whitespace --js-precision --js-keep-var-names --js-version --json-precision --json-keep-numbers --svg-keep-comments --svg-precision -s --sync --xml-keep-whitespace" types="css html js json svg xml text/css text/html text/javascript application/javascript application/json image/svg+xml text/xml application/xml" if echo "${cur}" | grep -Eq '^-'; then diff --git a/cmd/minify/main.go b/cmd/minify/main.go index a88d54a4af..6f256af914 100644 --- a/cmd/minify/main.go +++ b/cmd/minify/main.go @@ -212,7 +212,8 @@ func run() int { f.AddOpt(&siteurl, "", "url", nil, "URL of file to enable URL minification") f.AddOpt(&cssMinifier.Precision, "", "css-precision", 0, "Number of significant digits to preserve in numbers, 0 is all") f.AddOpt(&htmlMinifier.KeepComments, "", "html-keep-comments", false, "Preserve all comments") - f.AddOpt(&htmlMinifier.KeepConditionalComments, "", "html-keep-conditional-comments", false, "Preserve all IE conditional comments") + f.AddOpt(&htmlMinifier.KeepConditionalComments, "", "html-keep-conditional-comments", false, "Preserve all IE conditional comments (DEPRECATED)") + f.AddOpt(&htmlMinifier.KeepSpecialComments, "", "html-keep-special-comments", false, "Preserve all IE conditionals and SSI tags") f.AddOpt(&htmlMinifier.KeepDefaultAttrVals, "", "html-keep-default-attrvals", false, "Preserve default attribute values") f.AddOpt(&htmlMinifier.KeepDocumentTags, "", "html-keep-document-tags", false, "Preserve html, head and body tags") f.AddOpt(&htmlMinifier.KeepEndTags, "", "html-keep-end-tags", false, "Preserve all end tags") diff --git a/html/html.go b/html/html.go index ea817037b0..1ccc85be53 100644 --- a/html/html.go +++ b/html/html.go @@ -3,6 +3,7 @@ package html import ( "bytes" + "fmt" "io" "github.com/tdewolff/minify/v2" @@ -52,6 +53,7 @@ var PHPTemplateDelims = [2]string{""} type Minifier struct { KeepComments bool KeepConditionalComments bool + KeepSpecialComments bool KeepDefaultAttrVals bool KeepDocumentTags bool KeepEndTags bool @@ -70,6 +72,11 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st var rawTagHash Hash var rawTagMediatype []byte + if o.KeepConditionalComments { + fmt.Println("DEPRECATED: KeepConditionalComments is replaced by KeepSpecialComments") + o.KeepSpecialComments = true + } + omitSpace := true // if true the next leading space is omitted inPre := false @@ -97,27 +104,29 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st case html.CommentToken: if o.KeepComments { w.Write(t.Data) - } else if o.KeepConditionalComments && 6 < len(t.Text) && (bytes.HasPrefix(t.Text, []byte("[if ")) || bytes.HasSuffix(t.Text, []byte("[endif]")) || bytes.HasSuffix(t.Text, []byte("[endif]--"))) { - // [if ...] is always 7 or more characters, [endif] is only encountered for downlevel-revealed - // see https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx#syntax - if bytes.HasPrefix(t.Data, []byte("")) { // downlevel-hidden - begin := bytes.IndexByte(t.Data, '>') + 1 - end := len(t.Data) - len("") - if begin < end { - w.Write(t.Data[:begin]) - if err := o.Minify(m, w, buffer.NewReader(t.Data[begin:end]), nil); err != nil { - return minify.UpdateErrorPosition(err, z, t.Offset) + } else if o.KeepSpecialComments { + if 6 < len(t.Text) && (bytes.HasPrefix(t.Text, []byte("[if ")) || bytes.HasSuffix(t.Text, []byte("[endif]")) || bytes.HasSuffix(t.Text, []byte("[endif]--"))) { + // [if ...] is always 7 or more characters, [endif] is only encountered for downlevel-revealed + // see https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx#syntax + if bytes.HasPrefix(t.Data, []byte("")) { // downlevel-hidden + begin := bytes.IndexByte(t.Data, '>') + 1 + end := len(t.Data) - len("") + if begin < end { + w.Write(t.Data[:begin]) + if err := o.Minify(m, w, buffer.NewReader(t.Data[begin:end]), nil); err != nil { + return minify.UpdateErrorPosition(err, z, t.Offset) + } + w.Write(t.Data[end:]) + } else { + w.Write(t.Data) // malformed } - w.Write(t.Data[end:]) } else { - w.Write(t.Data) // malformed + w.Write(t.Data) // downlevel-revealed or short downlevel-hidden } - } else { - w.Write(t.Data) // downlevel-revealed or short downlevel-hidden + } else if 1 < len(t.Text) && t.Text[0] == '#' { + // SSI tags + w.Write(t.Data) } - } else if 1 < len(t.Text) && t.Text[0] == '#' { - // SSI tags - w.Write(t.Data) } case html.SvgToken: if err := m.MinifyMimetype(svgMimeBytes, w, buffer.NewReader(t.Data), nil); err != nil { diff --git a/html/html_test.go b/html/html_test.go index d8ff982cc3..27a742daf8 100644 --- a/html/html_test.go +++ b/html/html_test.go @@ -29,7 +29,7 @@ func TestHTML(t *testing.T) { //{"title ", `title`}, {``, ``}, {``, ``}, - {``, ``}, + {``, ``}, {``, ``}, {``, ``}, {`x`, `x`}, @@ -239,7 +239,7 @@ func TestHTMLKeepEndTags(t *testing.T) { } } -func TestHTMLKeepConditionalComments(t *testing.T) { +func TestHTMLKeepSpecialComments(t *testing.T) { htmlTests := []struct { html string expected string @@ -250,10 +250,11 @@ func TestHTMLKeepConditionalComments(t *testing.T) { {` `, ``}, {` `, ``}, {``, ``}, // #596 + {``, ``}, // #657 } m := minify.New() - htmlMinifier := &Minifier{KeepConditionalComments: true} + htmlMinifier := &Minifier{KeepSpecialComments: true} for _, tt := range htmlTests { t.Run(tt.html, func(t *testing.T) { r := bytes.NewBufferString(tt.html) @@ -434,7 +435,7 @@ func TestWriterErrors(t *testing.T) { m := minify.New() m.Add("text/html", &Minifier{ - KeepConditionalComments: true, + KeepSpecialComments: true, }) for _, tt := range errorTests {