From 3dd6c8b3ef4b897604967ca6600f09f727dbae9d Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Sat, 20 Aug 2016 22:48:24 +0200 Subject: [PATCH] Spaces after certain elements are preserved, fixes #89 --- html/html.go | 6 +++--- html/html_test.go | 4 ++++ html/table.go | 20 ++++++++++++++------ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/html/html.go b/html/html.go index 3aae64bd0a..6656ee542c 100644 --- a/html/html.go +++ b/html/html.go @@ -155,10 +155,10 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st } break } else if next.TokenType == html.StartTagToken || next.TokenType == html.EndTagToken { - // remove when followed up by a block tag if o.KeepWhitespace { break } + // remove when followed up by a block tag if next.Traits&nonPhrasingTag != 0 { t.Data = t.Data[:len(t.Data)-1] omitSpace = false @@ -221,7 +221,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st } } - if o.KeepWhitespace { + if o.KeepWhitespace || t.Traits&objectTag != 0 { omitSpace = false } else if t.Traits&nonPhrasingTag != 0 { omitSpace = true // omit spaces after block elements @@ -237,7 +237,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st break } - if o.KeepWhitespace { + if o.KeepWhitespace || t.Traits&objectTag != 0 { omitSpace = false } else if t.Traits&nonPhrasingTag != 0 { omitSpace = true // omit spaces after block elements diff --git a/html/html_test.go b/html/html_test.go index 297a9b5555..faf7ebd3be 100644 --- a/html/html_test.go +++ b/html/html_test.go @@ -117,6 +117,10 @@ func TestHTML(t *testing.T) { {``}, {`a'b=""`, `a'b=""`}, {` text`, `text text`}, // #89 + {`text text`, `text text`}, // #89 } m := minify.New() diff --git a/html/table.go b/html/table.go index 8d4382e82f..fac3d6548d 100644 --- a/html/table.go +++ b/html/table.go @@ -7,6 +7,7 @@ type traits uint8 const ( rawTag traits = 1 << iota nonPhrasingTag + objectTag booleanAttr caselessAttr urlAttr @@ -19,7 +20,8 @@ var tagMap = map[html.Hash]traits{ html.Blockquote: nonPhrasingTag, html.Body: nonPhrasingTag, html.Br: nonPhrasingTag, - html.Canvas: nonPhrasingTag, + html.Button: objectTag, + html.Canvas: objectTag, html.Caption: nonPhrasingTag, html.Code: rawTag, html.Col: nonPhrasingTag, @@ -45,33 +47,39 @@ var tagMap = map[html.Hash]traits{ html.Hgroup: nonPhrasingTag, html.Hr: nonPhrasingTag, html.Html: nonPhrasingTag, - html.Iframe: rawTag, + html.Iframe: rawTag | objectTag, + html.Img: objectTag, + html.Input: objectTag, + html.Keygen: objectTag, html.Li: nonPhrasingTag, html.Main: nonPhrasingTag, html.Math: rawTag, html.Meta: nonPhrasingTag, + html.Meter: objectTag, html.Nav: nonPhrasingTag, html.Noscript: nonPhrasingTag, + html.Object: objectTag, html.Ol: nonPhrasingTag, html.Output: nonPhrasingTag, html.P: nonPhrasingTag, html.Pre: rawTag | nonPhrasingTag, - html.Progress: nonPhrasingTag, + html.Progress: objectTag, html.Script: rawTag, html.Section: nonPhrasingTag, + html.Select: objectTag, html.Style: rawTag | nonPhrasingTag, - html.Svg: rawTag, + html.Svg: rawTag | objectTag, html.Table: nonPhrasingTag, html.Tbody: nonPhrasingTag, html.Td: nonPhrasingTag, - html.Textarea: rawTag, + html.Textarea: rawTag | objectTag, html.Tfoot: nonPhrasingTag, html.Th: nonPhrasingTag, html.Thead: nonPhrasingTag, html.Title: nonPhrasingTag, html.Tr: nonPhrasingTag, html.Ul: nonPhrasingTag, - html.Video: nonPhrasingTag, + html.Video: objectTag, } var attrMap = map[html.Hash]traits{