Skip to content

Commit

Permalink
SVG: keep empty attributes; fixes #576
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed May 3, 2023
1 parent 886d69b commit 8bbcb00
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
w.Write(t.Data)
}
case xml.AttributeToken:
if len(t.AttrVal) == 0 || t.Text == nil { // data is nil when attribute has been removed
if t.Text == nil { // data is nil when attribute has been removed
continue
} else if len(t.AttrVal) == 0 {
// empty attribute value
w.Write(spaceBytes)
w.Write(t.Text)
continue
}

Expand Down
3 changes: 2 additions & 1 deletion svg/svg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestSVG(t *testing.T) {
// TODO: what about x="" y="" for viewBox?
//{`<svg width="24" height="24" viewBox="0 0 24 24"></svg>`, `<svg width="24" height="24"/>`},
{`<path x="a"> </path>`, `<path x="a"/>`},
{`<path x=""> </path>`, `<path/>`},
{`<path x=""> </path>`, `<path x/>`},
{`<path x=" a "/>`, `<path x="a"/>`},
{"<path x=\" a \n b \"/>", `<path x="a b"/>`},
{`<path x="5.0px" y="0%"/>`, `<path x="5" y="0"/>`},
Expand Down Expand Up @@ -70,6 +70,7 @@ func TestSVG(t *testing.T) {
{`<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
{`<svg x-foo/>`, `<svg x-foo/>`}, // #576

// go fuzz
{`<0 d=09e9.6e-9e0`, `<0 d="09e9.6e-9e0"`},
Expand Down

0 comments on commit 8bbcb00

Please sign in to comment.