Skip to content

Commit c6278cf

Browse files
committed
compress svg to be able to compare
1 parent c685c03 commit c6278cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+8282
-8
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ require (
9696
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd
9797
github.com/steveyen/gtreap v0.0.0-20150807155958-0abe01ef9be2 // indirect
9898
github.com/stretchr/testify v1.4.0
99+
github.com/tdewolff/minify/v2 v2.5.1
100+
github.com/tdewolff/parse/v2 v2.3.9 // indirect
101+
github.com/tdewolff/test v1.0.3 // indirect
99102
github.com/tecbot/gorocksdb v0.0.0-20181010114359-8752a9433481 // indirect
100103
github.com/tinylib/msgp v1.1.1 // indirect
101104
github.com/tstranex/u2f v1.0.0

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ github.com/boombuler/barcode v0.0.0-20161226211916-fe0f26ff6d26/go.mod h1:paBWMc
9696
github.com/bradfitz/gomemcache v0.0.0-20190329173943-551aad21a668 h1:U/lr3Dgy4WK+hNk4tyD+nuGjpVLPEHuJSFXMw11/HPA=
9797
github.com/bradfitz/gomemcache v0.0.0-20190329173943-551aad21a668/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
9898
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
99+
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U=
99100
github.com/chris-ramon/douceur v0.2.0 h1:IDMEdxlEUUBYBKE4z/mJnFyVXox+MjuEVDJNN27glkU=
100101
github.com/chris-ramon/douceur v0.2.0/go.mod h1:wDW5xjJdeoMm1mRt4sD4c/LbF/mWdEpRXQKjTR8nIBE=
101102
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
@@ -416,6 +417,7 @@ github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7
416417
github.com/markbates/going v1.0.0/go.mod h1:I6mnB4BPnEeqo85ynXIx1ZFLLbtiLHNXVgWeFO9OGOA=
417418
github.com/markbates/goth v1.61.2 h1:jDowrUH5qw8KGuQdKwFhLzkXkTYCIPfz3LHADJsiPIs=
418419
github.com/markbates/goth v1.61.2/go.mod h1:qh2QfwZoWRucQ+DR5KVKC6dUGkNCToWh4vS45GIzFsY=
420+
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
419421
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
420422
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
421423
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
@@ -577,6 +579,14 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
577579
github.com/syndtr/goleveldb v0.0.0-20190203031304-2f17a3356c66/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
578580
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
579581
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
582+
github.com/tdewolff/minify/v2 v2.5.1 h1:5Or+sQBV+qUHDQG45lch0iFjUp1wxW0743nLl3qZDrs=
583+
github.com/tdewolff/minify/v2 v2.5.1/go.mod h1:ltNgpU7tnfgNCIHP1D0aS8g9IV0qP7WWP1/HdXIGznE=
584+
github.com/tdewolff/parse/v2 v2.3.8/go.mod h1:HansaqmN4I/U7L6/tUp0NcwT2tFO0F4EAWYGSDzkYNk=
585+
github.com/tdewolff/parse/v2 v2.3.9 h1:d8/K6XOLy5JVpLTG9Kx+SxA72rlm5OowFmVSVgtOlmM=
586+
github.com/tdewolff/parse/v2 v2.3.9/go.mod h1:HansaqmN4I/U7L6/tUp0NcwT2tFO0F4EAWYGSDzkYNk=
587+
github.com/tdewolff/test v1.0.0/go.mod h1:DiQUlutnqlEvdvhSn2LPGy4TFwRauAaYDsL+683RNX4=
588+
github.com/tdewolff/test v1.0.3 h1:oQqvxCCoUexB1bWZzvCzWG2VTqOIvWBV1JVg2OOFQi0=
589+
github.com/tdewolff/test v1.0.3/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
580590
github.com/tecbot/gorocksdb v0.0.0-20181010114359-8752a9433481 h1:HOxvxvnntLiPn123Fk+twfUhCQdMDaqmb0cclArW0T0=
581591
github.com/tecbot/gorocksdb v0.0.0-20181010114359-8752a9433481/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8=
582592
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
@@ -705,6 +715,7 @@ golang.org/x/sys v0.0.0-20180824143301-4910a1d54f87/go.mod h1:STP8DvDyc/dI5b8T5h
705715
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
706716
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
707717
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
718+
golang.org/x/sys v0.0.0-20181031143558-9b800f95dbbc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
708719
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
709720
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
710721
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

modules/util/svg/svg.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,32 @@
44

55
package svg
66

7-
import "github.com/microcosm-cc/bluemonday"
7+
import (
8+
"bytes"
9+
"io"
10+
11+
"github.com/microcosm-cc/bluemonday"
12+
13+
minify "github.com/tdewolff/minify/v2"
14+
"github.com/tdewolff/minify/v2/svg"
15+
)
16+
17+
// MinifySVG compact svg strings
18+
func MinifySVG(svgData io.Reader) (*bytes.Buffer, error) {
19+
m := minify.New()
20+
m.AddFunc("image/svg+xml", svg.Minify)
21+
var out bytes.Buffer
22+
err := m.Minify("image/svg+xml", &out, svgData)
23+
return &out, err
24+
}
825

926
// SanitizeSVG remove potential malicious dom elements
10-
func SanitizeSVG(svg string) string {
27+
func SanitizeSVG(svgData io.Reader) *bytes.Buffer {
1128
p := bluemonday.NewPolicy()
1229
p.AllowElements("svg", "title", "path", "desc", "g")
1330
p.AllowAttrs("id", "viewbox", "role", "aria-labelledby").OnElements("svg")
1431
p.AllowAttrs("id").OnElements("title", "desc")
1532
p.AllowAttrs("id", "data-name", "class", "aria-label").OnElements("g")
1633
p.AllowAttrs("id", "data-name", "class", "d", "transform", "aria-haspopup").OnElements("path")
17-
return p.Sanitize(svg)
34+
return p.SanitizeReader(svgData)
1835
}

modules/util/svg/svg_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
package svg
66

7-
import "testing"
7+
import (
8+
"bytes"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
813

914
func TestSanitizeSVG(t *testing.T) {
1015
tests := []struct {
@@ -70,7 +75,7 @@ func TestSanitizeSVG(t *testing.T) {
7075
</g>
7176
</g>
7277
</svg>`,
73-
want: `<svg id="cat" viewBox="0 0 720 800" aria-labelledby="catTitle catDesc" role="img">
78+
want: `<svg id="cat" viewbox="0 0 720 800" aria-labelledby="catTitle catDesc" role="img">
7479
<title id="catTitle">Pixels, My Super-friendly Cat</title>
7580
<desc id="catDesc">An illustrated gray cat with bright green blinking eyes.</desc>
7681
<path id="tail" data-name="tail" class="cls-1" d="M545.9,695.9c8,28.2,23.2,42.3,27.2,46.9,21.4,24.1,41.5,40.2,81.1,42.9s65.4-14.2,60.8-26.8-23.1-9.1-51.3-8.3c-35.2.9-66.6-31.3-74.8-63.9s-7.9-63.8-36.8-85.5c-44.1-33-135.6-7.1-159.8-3.4s-48.4,52.5-9.6,45.1,91.4-23.1,123.2-12.7C537.8,640.4,537.9,667.7,545.9,695.9Z" transform="translate(-9.7 -9.3)"/>
@@ -130,9 +135,11 @@ func TestSanitizeSVG(t *testing.T) {
130135
}
131136
for _, tt := range tests {
132137
t.Run(tt.name, func(t *testing.T) {
133-
if got := SanitizeSVG(tt.input); got != tt.want {
134-
t.Errorf("SanitizeSVG() = %v, want %v", got, tt.want)
135-
}
138+
out, err := MinifySVG(SanitizeSVG(bytes.NewBufferString(tt.input)))
139+
assert.NoError(t, err)
140+
expected, err := MinifySVG(bytes.NewBufferString(tt.want)) //Compressed to limit the way it align text on clean-up
141+
assert.NoError(t, err)
142+
assert.Equal(t, expected.String(), out.String(), "The sanitized svg is not equal")
136143
})
137144
}
138145
}

vendor/github.com/tdewolff/minify/v2/.gitattributes

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tdewolff/minify/v2/.gitignore

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tdewolff/minify/v2/.goreleaser.yml

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tdewolff/minify/v2/.travis.yml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/tdewolff/minify/v2/LICENSE.md

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)