Skip to content

Commit

Permalink
HTML: fix whitespace removal for inline tags, ie. keep space in <del>…
Browse files Browse the repository at this point in the history
…a </del> for most cases
  • Loading branch information
tdewolff committed Jul 23, 2023
1 parent 3fa4d56 commit 43cf162
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
9 changes: 2 additions & 7 deletions html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,8 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
t.Data = t.Data[:len(t.Data)-1]
omitSpace = false
break
} else if next.TokenType == html.TextToken {
// this only happens when a comment, doctype or phrasing end tag (only for !o.KeepWhitespace) was in between
// remove if the text token starts with a whitespace
if len(next.Data) > 0 && parse.IsWhitespace(next.Data[0]) {
t.Data = t.Data[:len(t.Data)-1]
omitSpace = false
}
} else if next.TokenType == html.TextToken && !parse.IsAllWhitespace(next.Data) {
// stop looking when text encountered
break
} else if next.TokenType == html.StartTagToken || next.TokenType == html.EndTagToken {
if o.KeepWhitespace {
Expand Down
12 changes: 6 additions & 6 deletions html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,22 @@ func TestHTML(t *testing.T) {

// whitespace
{`cats and dogs `, `cats and dogs`},
{` <div> <i> test </i> <b> test </b> </div> `, `<div><i>test</i> <b>test</b></div>`},
{` <div> <i> test </i> <b> test </b> </div> `, `<div><i>test </i><b>test</b></div>`},
{`<strong>x </strong>y`, `<strong>x </strong>y`},
{`<strong>x </strong> y`, `<strong>x</strong> y`},
{"<strong>x </strong>\ny", "<strong>x</strong>\ny"},
{`<strong>x </strong> y`, `<strong>x </strong>y`},
{"<strong>x </strong>\ny", "<strong>x </strong>y"},
{`<p>x </p>y`, `<p>x</p>y`},
{`x <p>y</p>`, `x<p>y`},
{` <!doctype html> <!--comment--> <html> <body><p></p></body></html> `, `<!doctype html><p>`}, // spaces before html and at the start of html are dropped
{`<p>x<br> y`, `<p>x<br>y`},
{`<p>x </b> <b> y`, `<p>x</b> <b>y`},
{`<p>x </b> <b> y`, `<p>x </b><b>y`},
{`a <code></code> b`, `a <code></code> b`},
{`a <code>code</code> b`, `a <code>code</code> b`},
{`a <code> code </code> b`, `a <code>code</code> b`},
{`a <code> code </code> b`, `a <code>code </code>b`},
{`a <script>script</script> b`, `a <script>script</script>b`},
{"text\n<!--comment-->\ntext", "text\ntext"},
{"abc\n</body>\ndef", "abc\ndef"},
{"<x>\n<!--y-->\n</x>", "<x></x>"},
{"a <template> b </template> c", "a <template>b</template>c"},
{`<p class=" name ">`, `<p class=name>`},
{`<p class=" name other ">`, `<p class="name other">`},
{`<p onclick=" javascript:lala ">`, `<p onclick=lala>`},
Expand All @@ -111,6 +110,7 @@ func TestHTML(t *testing.T) {
{`a<picture> <img> </picture>b`, `a<picture> <img> </picture>b`},
{`a <picture> <img> </picture>b`, `a <picture><img> </picture>b`},
{`<input placeholder=" a " value=" b ">`, `<input placeholder=" a " value=" b ">`},
{`a <strike> b </strike> c`, `a <strike>b </strike>c`},

// from HTML Minifier
{`<DIV TITLE="blah">boo</DIV>`, `<div title=blah>boo</div>`},
Expand Down

0 comments on commit 43cf162

Please sign in to comment.