Skip to content

Commit

Permalink
Merge pull request #121 from adtac/keependtags
Browse files Browse the repository at this point in the history
html: Add `KeepEndTags`
  • Loading branch information
tdewolff committed Feb 26, 2017
2 parents c764d19 + 2e37fce commit f1edcff
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Options:
- `KeepDefaultAttrVals` do not remove default attribute value such as `<script type="text/javascript">`
- `KeepDocumentTags` do not remove `html`, `head` and `body` tags
- `KeepWhitespace` do not remove whitespace between inline tags but still collapse multiple whitespace characters into one
- `KeepEndTags` do not remove closing tags for any tag

After recent benchmarking and profiling it became really fast and minifies pages in the 10ms range, making it viable for on-the-fly minification.

Expand Down
1 change: 1 addition & 0 deletions cmd/minify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func main() {
flag.StringVar(&siteurl, "url", "", "URL of file to enable URL minification")
flag.BoolVar(&htmlMinifier.KeepDefaultAttrVals, "html-keep-default-attrvals", false, "Preserve default attribute values")
flag.BoolVar(&htmlMinifier.KeepWhitespace, "html-keep-whitespace", false, "Preserve whitespace characters but still collapse multiple whitespace into one")
flag.BoolVar(&htmlMinifier.KeepEndTags, "html-keep-end-tags", false, "Preserve closing tags for all tags")
flag.BoolVar(&htmlMinifier.KeepDocumentTags, "html-keep-document-tags", false, "Preserve html, head and body tags")
flag.BoolVar(&xmlMinifier.KeepWhitespace, "xml-keep-whitespace", false, "Preserve whitespace characters but still collapse multiple whitespace into one")
flag.Parse()
Expand Down
5 changes: 3 additions & 2 deletions html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Minifier struct {
KeepDefaultAttrVals bool
KeepWhitespace bool
KeepDocumentTags bool
KeepEndTags bool
}

// Minify minifies HTML data, it reads from r and writes to w.
Expand Down Expand Up @@ -203,9 +204,9 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
if !hasAttributes && (!o.KeepDocumentTags && (t.Hash == html.Html || t.Hash == html.Head || t.Hash == html.Body) || t.Hash == html.Colgroup) {
break
} else if t.TokenType == html.EndTagToken {
if t.Hash == html.Thead || t.Hash == html.Tbody || t.Hash == html.Tfoot || t.Hash == html.Tr || t.Hash == html.Th || t.Hash == html.Td ||
if !o.KeepEndTags && (t.Hash == html.Thead || t.Hash == html.Tbody || t.Hash == html.Tfoot || t.Hash == html.Tr || t.Hash == html.Th || t.Hash == html.Td ||
t.Hash == html.Optgroup || t.Hash == html.Option || t.Hash == html.Dd || t.Hash == html.Dt ||
t.Hash == html.Li || t.Hash == html.Rb || t.Hash == html.Rt || t.Hash == html.Rtc || t.Hash == html.Rp {
t.Hash == html.Li || t.Hash == html.Rb || t.Hash == html.Rt || t.Hash == html.Rtc || t.Hash == html.Rp) {
break
} else if t.Hash == html.P {
i := 0
Expand Down
18 changes: 18 additions & 0 deletions html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ func TestHTML(t *testing.T) {
}
}

func TestHTMLKeepEndTags(t *testing.T) {
htmlTests := []struct {
html string
expected string
}{
{`<style>class{color:red}</style>`, `<style>class{color:red}</style>`},
{`<li>test</li>`, `<li>test</li>`},
}

m := minify.New()
htmlMinifier := &Minifier{KeepEndTags: true}
for _, tt := range htmlTests {
r := bytes.NewBufferString(tt.html)
w := &bytes.Buffer{}
test.Minify(t, tt.html, htmlMinifier.Minify(m, w, r, nil), w.String(), tt.expected)
}
}

func TestHTMLKeepWhitespace(t *testing.T) {
htmlTests := []struct {
html string
Expand Down

0 comments on commit f1edcff

Please sign in to comment.