Skip to content

Commit 7f03e40

Browse files
committed
Update to parse@v2.3.13; minify entities in HTML, XML and SVG; optimizations to improve ReplaceMultipleWhitespace by 20%--25%
1 parent 200330f commit 7f03e40

File tree

7 files changed

+26
-19
lines changed

7 files changed

+26
-19
lines changed

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/fsnotify/fsnotify v1.4.7
99
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
1010
github.com/spf13/pflag v1.0.3
11-
github.com/tdewolff/parse/v2 v2.3.11
11+
github.com/tdewolff/parse/v2 v2.3.13
1212
github.com/tdewolff/test v1.0.4
1313
golang.org/x/sys v0.0.0-20181031143558-9b800f95dbbc // indirect
1414
)

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2 h1:JAEbJn3j/FrhdWA9jW8
88
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
99
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
1010
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
11-
github.com/tdewolff/parse/v2 v2.3.11 h1:CSprCHYKUHqsa0EemAjXzVWGV/LQFAYwSsd5jlIvezE=
12-
github.com/tdewolff/parse/v2 v2.3.11/go.mod h1:pclWRpgD95an4pJvzjbp1A+bl6e7R9DemblveSm/Zo4=
11+
github.com/tdewolff/parse/v2 v2.3.13 h1:NoKdGfd4ZNIzDRFSWlioy+a4AYT2O65mrmlpCnxzSY8=
12+
github.com/tdewolff/parse/v2 v2.3.13/go.mod h1:+V2lSZ93xpH2Csfs/vtNY1Fjr8kcFMsZKjyLoSkZbM0=
1313
github.com/tdewolff/test v1.0.4 h1:ih38SXuQJ32Hng5EtSW32xqEsVeMnPp6nNNRPhBBDE8=
1414
github.com/tdewolff/test v1.0.4/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
1515
golang.org/x/sys v0.0.0-20181031143558-9b800f95dbbc h1:SdCq5U4J+PpbSDIl9bM0V1e1Ug1jsnBkAFvTs1htn7U=

‎html/html.go‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,18 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
140140
return err
141141
}
142142
} else {
143-
t.Data = parse.ReplaceMultipleWhitespace(t.Data)
144-
t.Data = parse.ReplaceEntities(t.Data, html.EntitiesMap, html.TextRevEntitiesMap)
143+
t.Data = parse.ReplaceMultipleWhitespaceAndEntities(t.Data, html.EntitiesMap, html.TextRevEntitiesMap)
145144

146145
// whitespace removal; trim left
147-
if omitSpace && (t.Data[0] == ' ' || t.Data[0] == '\n') {
146+
if omitSpace && parse.IsWhitespace(t.Data[0]) {
148147
t.Data = t.Data[1:]
149148
}
150149

151150
// whitespace removal; trim right
152151
omitSpace = false
153152
if len(t.Data) == 0 {
154153
omitSpace = true
155-
} else if t.Data[len(t.Data)-1] == ' ' || t.Data[len(t.Data)-1] == '\n' {
154+
} else if parse.IsWhitespace(t.Data[len(t.Data)-1]) {
156155
omitSpace = true
157156
i := 0
158157
for {
@@ -348,12 +347,12 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
348347
}
349348

350349
val := attr.AttrVal
351-
val = parse.ReplaceEntities(val, html.EntitiesMap, nil)
350+
if attr.Traits&trimAttr != 0 {
351+
val = parse.ReplaceMultipleWhitespaceAndEntities(val, html.EntitiesMap, nil)
352+
} else {
353+
val = parse.ReplaceEntities(val, html.EntitiesMap, nil)
354+
}
352355
if t.Traits != 0 {
353-
if attr.Traits&trimAttr != 0 {
354-
val = parse.TrimWhitespace(val)
355-
val = parse.ReplaceMultipleWhitespace(val)
356-
}
357356
if len(val) == 0 && (attr.Hash == html.Class ||
358357
attr.Hash == html.Dir ||
359358
attr.Hash == html.Id ||

‎html/html_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ func TestHTML(t *testing.T) {
3232
{`<html><head></head><body>x</body></html>`, `x`},
3333
{`<meta http-equiv="content-type" content="text/html; charset=utf-8">`, `<meta charset=utf-8>`},
3434
{`<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />`, `<meta charset=utf-8>`},
35+
{`<meta http-equiv=" content-type " content=" text/html; charset=utf-8 ">`, `<meta charset=utf-8>`},
3536
{`<meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src https://*; child-src 'none';">`, `<meta http-equiv=content-security-policy content="default-src 'self'; img-src https://*; child-src 'none';">`},
3637
{`<meta name="keywords" content="a, b">`, `<meta name=keywords content="a,b">`},
38+
//{`<meta name="keywords" content=" a, b ">`, `<meta name=keywords content="a,b">`}, // TODO
3739
{`<meta name="viewport" content="width = 996" />`, `<meta name=viewport content="width=996">`},
3840
{`<span attr="test"></span>`, `<span attr=test></span>`},
3941
{`<span attr='test&apos;test'></span>`, `<span attr="test'test"></span>`},
@@ -45,6 +47,7 @@ func TestHTML(t *testing.T) {
4547
{`<span>&lt;</span>`, `<span>&lt;</span>`},
4648
{`<span>&gt;</span>`, `<span>></span>`},
4749
{`<code>&quot;&rsquor;</code>`, `<code>"&#8217;</code>`},
50+
{`<small>&#160;</small>`, `<small>&#160;</small>`},
4851
{`<span name="&lt;&apos;">a</span>`, `<span name="<'">a</span>`},
4952
{`<span clear=none method=GET></span>`, `<span></span>`},
5053
{`<span onload="javascript:x;"></span>`, `<span onload=x;></span>`},

‎svg/buffer.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ func (z *TokenBuffer) read(t *Token) {
3939
if t.TokenType == xml.AttributeToken {
4040
t.AttrVal = z.l.AttrVal()
4141
if len(t.AttrVal) > 1 && (t.AttrVal[0] == '"' || t.AttrVal[0] == '\'') {
42-
t.AttrVal = parse.ReplaceMultipleWhitespace(parse.TrimWhitespace(t.AttrVal[1 : len(t.AttrVal)-1])) // quotes will be readded in attribute loop if necessary
42+
t.AttrVal = t.AttrVal[1 : len(t.AttrVal)-1] // quotes will be readded in attribute loop if necessary
43+
t.AttrVal = parse.ReplaceMultipleWhitespaceAndEntities(t.AttrVal, xml.EntitiesMap, nil)
44+
t.AttrVal = parse.TrimWhitespace(t.AttrVal)
4345
}
4446
t.Hash = svg.ToHash(t.Text)
4547
} else if t.TokenType == xml.StartTagToken || t.TokenType == xml.EndTagToken {

‎svg/svg.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
7171
}
7272
}
7373
case xml.TextToken:
74-
t.Data = parse.ReplaceMultipleWhitespace(parse.TrimWhitespace(t.Data))
74+
t.Data = parse.ReplaceMultipleWhitespaceAndEntities(t.Data, xml.EntitiesMap, nil)
75+
t.Data = parse.TrimWhitespace(t.Data)
76+
7577
if tag == svg.Style && len(t.Data) > 0 {
7678
if err := m.MinifyMimetype(defaultStyleType, w, buffer.NewReader(t.Data), defaultStyleParams); err != nil {
7779
if err != minify.ErrNotExist {
@@ -96,7 +98,9 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
9698
}
9799
var useText bool
98100
if t.Text, useText = xml.EscapeCDATAVal(&attrByteBuffer, t.Text); useText {
99-
t.Text = parse.ReplaceMultipleWhitespace(parse.TrimWhitespace(t.Text))
101+
t.Text = parse.ReplaceMultipleWhitespace(t.Text)
102+
t.Text = parse.TrimWhitespace(t.Text)
103+
100104
if _, err := w.Write(t.Text); err != nil {
101105
return err
102106
}

‎xml/xml.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,18 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
6969
omitSpace = true
7070
}
7171
case xml.TextToken:
72-
t.Data = parse.ReplaceMultipleWhitespace(t.Data)
73-
t.Data = parse.ReplaceEntities(t.Data, xml.EntitiesMap, nil)
72+
t.Data = parse.ReplaceMultipleWhitespaceAndEntities(t.Data, xml.EntitiesMap, nil)
7473

7574
// whitespace removal; trim left
76-
if omitSpace && (t.Data[0] == ' ' || t.Data[0] == '\n') {
75+
if omitSpace && parse.IsWhitespace(t.Data[0]) {
7776
t.Data = t.Data[1:]
7877
}
7978

8079
// whitespace removal; trim right
8180
omitSpace = false
8281
if len(t.Data) == 0 {
8382
omitSpace = true
84-
} else if t.Data[len(t.Data)-1] == ' ' || t.Data[len(t.Data)-1] == '\n' {
83+
} else if parse.IsWhitespace(t.Data[len(t.Data)-1]) {
8584
omitSpace = true
8685
i := 0
8786
for {

0 commit comments

Comments
 (0)