Skip to content

Commit

Permalink
Spaces after certain elements are preserved, fixes #89
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Aug 20, 2016
1 parent a41c61d commit 3dd6c8b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
6 changes: 3 additions & 3 deletions html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func TestHTML(t *testing.T) {
{`<meta e t n content=ful><a b`, `<meta e t n content=ful><a b>`},
{`<img alt=a'b="">`, `<img alt='a&#39;b=""'>`},
{`</b`, `</b`},

// bugs
{`text <img> text`, `text <img> text`}, // #89
{`text <progress></progress> text`, `text <progress></progress> text`}, // #89
}

m := minify.New()
Expand Down
20 changes: 14 additions & 6 deletions html/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type traits uint8
const (
rawTag traits = 1 << iota
nonPhrasingTag
objectTag
booleanAttr
caselessAttr
urlAttr
Expand All @@ -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,
Expand All @@ -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{
Expand Down

0 comments on commit 3dd6c8b

Please sign in to comment.