From 43cf162fd2160602eece2e4fd830e7f1c432c890 Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Sun, 23 Jul 2023 12:47:27 +0200 Subject: [PATCH] HTML: fix whitespace removal for inline tags, ie. keep space in a for most cases --- html/html.go | 9 ++------- html/html_test.go | 12 ++++++------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/html/html.go b/html/html.go index 497a08dd0e..fe5d9ae99d 100644 --- a/html/html.go +++ b/html/html.go @@ -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 { diff --git a/html/html_test.go b/html/html_test.go index 0d02898b0e..16c2069218 100644 --- a/html/html_test.go +++ b/html/html_test.go @@ -85,23 +85,22 @@ func TestHTML(t *testing.T) { // whitespace {`cats and dogs `, `cats and dogs`}, - {`
test test
`, `
test test
`}, + {`
test test
`, `
test test
`}, {`x y`, `x y`}, - {`x y`, `x y`}, - {"x \ny", "x\ny"}, + {`x y`, `x y`}, + {"x \ny", "x y"}, {`

x

y`, `

x

y`}, {`x

y

`, `x

y`}, {`

`, `

`}, // spaces before html and at the start of html are dropped {`

x
y`, `

x
y`}, - {`

x y`, `

x y`}, + {`

x y`, `

x y`}, {`a b`, `a b`}, {`a code b`, `a code b`}, - {`a code b`, `a code b`}, + {`a code b`, `a code b`}, {`a b`, `a b`}, {"text\n\ntext", "text\ntext"}, {"abc\n\ndef", "abc\ndef"}, {"\n\n", ""}, - {"a c", "a c"}, {`

`, `

`}, {`

`, `

`}, {`

`, `

`}, @@ -111,6 +110,7 @@ func TestHTML(t *testing.T) { {`a b`, `a b`}, {`a b`, `a b`}, {``, ``}, + {`a b c`, `a b c`}, // from HTML Minifier {`

boo
`, `
boo
`},