Skip to content

Commit 9b9e4f7

Browse files
committed
Keep the xmlns:svg namespace, see #803
1 parent 573513d commit 9b9e4f7

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

svg/svg.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ import (
1414
)
1515

1616
var (
17-
voidBytes = []byte("/>")
18-
isBytes = []byte("=")
19-
spaceBytes = []byte(" ")
20-
cdataEndBytes = []byte("]]>")
21-
zeroBytes = []byte("0")
22-
cssMimeBytes = []byte("text/css")
23-
noneBytes = []byte("none")
24-
urlBytes = []byte("url(")
17+
voidBytes = []byte("/>")
18+
isBytes = []byte("=")
19+
spaceBytes = []byte(" ")
20+
cdataEndBytes = []byte("]]>")
21+
zeroBytes = []byte("0")
22+
svgStartTagBytes = []byte("<svg")
23+
cssMimeBytes = []byte("text/css")
24+
noneBytes = []byte("none")
25+
urlBytes = []byte("url(")
2526
)
2627

2728
////////////////////////////////////////////////////////////////
@@ -129,9 +130,13 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, params map[stri
129130
tag = t.Hash
130131
if tag == Metadata {
131132
t.Data = nil
132-
} else if colon := bytes.IndexByte(t.Text, ':'); colon != -1 || t.Data == nil {
133+
} else if colon := bytes.IndexByte(t.Data, ':'); colon != -1 {
133134
// skip attributes in namespace (eg. inkscape or sodipodi)
134-
t.Data = nil
135+
if bytes.Equal(t.Data[:colon], svgStartTagBytes) {
136+
t.Data = append(t.Data[:1], t.Data[5:]...)
137+
} else {
138+
t.Data = nil
139+
}
135140
} else if tag == Defs && tb.Peek(1).TokenType == xml.StartTagCloseVoidToken {
136141
// skip empty tags
137142
t.Data = nil

svg/svg_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ func TestSVG(t *testing.T) {
6161
{`<foreignObject>`, `<foreignObject>`},
6262
{`<foreignObject/> text`, `<foreignObject/>text`},
6363
{`<foreignObject><foreignObject/></foreignObject> text`, `<foreignObject><foreignObject/></foreignObject>text`},
64+
{`<xyz:rect width="100%" height="100%" fill="green"/>`, ``},
65+
{`<svg:rect width="100%" height="100%" fill="green"/>`, `<rect width="100%" height="100%" fill="green"/>`},
6466

6567
// from SVGO
6668
{`<!DOCTYPE bla><?xml?><!-- comment --><metadata/>`, ``},

0 commit comments

Comments
 (0)