Skip to content

Commit cc9997e

Browse files
author
Gusted
committed
Don't write self-closing tag with empty attributes
- Currently the code will write self-closing tags with empty attributes to the output, even when the element isn't allowed to have empty attributes. - Move to `break` to one outer scope to fix it.
1 parent 705c34a commit cc9997e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

sanitize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ func (p *Policy) sanitize(r io.Reader, w io.Writer) error {
440440
if _, err := buff.WriteString(" "); err != nil {
441441
return err
442442
}
443-
break
444443
}
444+
break
445445
}
446446
if !skipElementContent {
447447
if _, err := buff.WriteString(token.String()); err != nil {

sanitize_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3913,3 +3913,21 @@ func TestIssue147(t *testing.T) {
39133913
expected)
39143914
}
39153915
}
3916+
3917+
func TestRemovingEmptySelfClosingTag(t *testing.T) {
3918+
p := NewPolicy()
3919+
3920+
// Only broke when attribute policy was specified.
3921+
p.AllowAttrs("type").OnElements("input")
3922+
3923+
input := `<input/>`
3924+
out := p.Sanitize(input)
3925+
expected := ``
3926+
if out != expected {
3927+
t.Errorf(
3928+
"test failed;\ninput : %s\noutput : %s\nexpected: %s",
3929+
input,
3930+
out,
3931+
expected)
3932+
}
3933+
}

0 commit comments

Comments
 (0)