Skip to content

Commit

Permalink
HTML: rename KeepConditionalComments => KeepSpecialComments, fixes #657
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Jan 11, 2024
1 parent 0eaf60e commit 342cbc1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/minify/bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/minify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
43 changes: 26 additions & 17 deletions html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package html

import (
"bytes"
"fmt"
"io"

"github.com/tdewolff/minify/v2"
Expand Down Expand Up @@ -52,6 +53,7 @@ var PHPTemplateDelims = [2]string{"<?", "?>"}
type Minifier struct {
KeepComments bool
KeepConditionalComments bool
KeepSpecialComments bool
KeepDefaultAttrVals bool
KeepDocumentTags bool
KeepEndTags bool
Expand All @@ -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

Expand Down Expand Up @@ -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("<!--[if ")) && bytes.HasSuffix(t.Data, []byte("<![endif]-->")) { // downlevel-hidden
begin := bytes.IndexByte(t.Data, '>') + 1
end := len(t.Data) - len("<![endif]-->")
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("<!--[if ")) && bytes.HasSuffix(t.Data, []byte("<![endif]-->")) { // downlevel-hidden
begin := bytes.IndexByte(t.Data, '>') + 1
end := len(t.Data) - len("<![endif]-->")
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 {
Expand Down
9 changes: 5 additions & 4 deletions html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestHTML(t *testing.T) {
//{"<title>title</title> <body>", `<title>title</title>`},
{`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">`, `<!doctype html>`},
{`<!-- comment -->`, ``},
{`<!--# SSI Tag -->`, `<!--# SSI Tag -->`},
{`<!--# SSI Tag -->`, ``},
{`<style><!--\ncss\n--></style>`, `<style><!--\ncss\n--></style>`},
{`<style>&</style>`, `<style>&</style>`},
{`<html><head></head><body>x</body></html>`, `x`},
Expand Down Expand Up @@ -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
Expand All @@ -250,10 +250,11 @@ func TestHTMLKeepConditionalComments(t *testing.T) {
{`<!--[if !mso]><!--> <b> </b> <!--<![endif]-->`, `<!--[if !mso]><!--><b></b><!--<![endif]-->`},
{`<!--[if gt IE 6]><!--> <b> </b> <![endif]-->`, `<!--[if gt IE 6]><!--><b></b><![endif]-->`},
{`<!--[if IE]foo<![endif]-->`, `<!--[if IE]foo<![endif]-->`}, // #596
{`<!--# SSI-->`, `<!--# SSI-->`}, // #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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 342cbc1

Please sign in to comment.