Skip to content

Commit

Permalink
SVG: don't remove rect with zero width or height, fixes #557
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Mar 13, 2023
1 parent d6c4fa7 commit 8457bd3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 23 deletions.
18 changes: 0 additions & 18 deletions svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
tag = t.Hash
if tag == Metadata {
t.Data = nil
} else if tag == Rect {
o.shortenRect(tb, &t)
}

if t.Data == nil {
Expand Down Expand Up @@ -261,22 +259,6 @@ func (o *Minifier) shortenDimension(b []byte) ([]byte, int) {
return b, 0
}

func (o *Minifier) shortenRect(tb *TokenBuffer, t *Token) {
w, h := zeroBytes, zeroBytes
attrs := tb.Attributes(Width, Height)
if attrs[0] != nil {
n, _ := parse.Dimension(attrs[0].AttrVal)
w = minify.Number(attrs[0].AttrVal[:n], o.Precision)
}
if attrs[1] != nil {
n, _ := parse.Dimension(attrs[1].AttrVal)
h = minify.Number(attrs[1].AttrVal[:n], o.Precision)
}
if len(w) == 0 || w[0] == '0' || len(h) == 0 || h[0] == '0' {
t.Data = nil
}
}

////////////////////////////////////////////////////////////////

func printTag(w io.Writer, tb *TokenBuffer, tag Hash) {
Expand Down
8 changes: 3 additions & 5 deletions svg/svg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ func TestSVG(t *testing.T) {
{`<path fill="#fff"/>`, `<path fill="#fff"/>`},
{`<path fill="white"/>`, `<path fill="#fff"/>`},
{`<path fill="#ff0000"/>`, `<path fill="red"/>`},
{`<rect x="5" y="10" rx="2" ry="3">`, ``},
{`<rect x="5" y="10" height="40"/>`, ``},
{`<rect x="5" y="10" width="30" height="0%"/>`, ``},
{`<rect x="5" y="10" width="30" height="0%"/>`, `<rect x="5" y="10" width="30" height="0"/>`},
{`<rect x="5" y="10" width="30%" height="100%"/>`, `<rect x="5" y="10" width="30%" height="100%"/>`},
{`<svg contentStyleType="text/json ; charset=iso-8859-1"><style>{a : true}</style></svg>`, `<svg contentStyleType="text/json;charset=iso-8859-1"><style>{a : true}</style></svg>`},
{`<metadata><dc:title /></metadata>`, ``},
Expand All @@ -69,8 +67,8 @@ func TestSVG(t *testing.T) {

{`<polygon points="-0.1,"/>`, `<polygon points="-0.1,"/>`}, // #45
{`<path stroke="url(#UPPERCASE)"/>`, `<path stroke="url(#UPPERCASE)"/>`}, // #117
{`<rect height="10"/><path/>`, `<path/>`}, // #244
{`<rect height="10"><path/></rect>`, ``}, // #244
{`<rect height="10"/><path/>`, `<rect height="10"/><path/>`}, // #244
{`<rect height="10"><path/></rect>`, `<rect height="10"><path/></rect>`}, // #244
{`<foreignObject><div></div></foreignObject>`, `<foreignObject><div></div></foreignObject>`}, // #291

// go fuzz
Expand Down

0 comments on commit 8457bd3

Please sign in to comment.