fix iframe autoclosing

This commit is contained in:
Nazar Kanaev 2021-05-13 22:37:02 +01:00
parent 28f08ad42a
commit 37ed856d8b
2 changed files with 17 additions and 2 deletions

View File

@ -69,8 +69,12 @@ func Sanitize(baseURL, input string) string {
buffer.WriteString("<" + tagName + ">") buffer.WriteString("<" + tagName + ">")
} }
if wrap { if tagName == "iframe" {
buffer.WriteString("</iframe></div>") // autoclose iframes
buffer.WriteString("</iframe>")
if wrap {
buffer.WriteString("</div>")
}
} else { } else {
tagStack = append(tagStack, tagName) tagStack = append(tagStack, tagName)
} }
@ -80,6 +84,7 @@ func Sanitize(baseURL, input string) string {
} }
case html.EndTagToken: case html.EndTagToken:
tagName := token.Data tagName := token.Data
// iframes are autoclosed. see above
if tagName == "iframe" { if tagName == "iframe" {
continue continue
} }

View File

@ -163,6 +163,16 @@ func TestInvalidNestedTag(t *testing.T) {
} }
} }
func TestValidIFrame(t *testing.T) {
input := `<iframe src="http://example.org/"></iframe>`
expected := `<iframe src="http://example.org/" sandbox="allow-scripts allow-same-origin allow-popups" loading="lazy"></iframe>`
output := Sanitize("http://example.org/", input)
if expected != output {
t.Errorf("Wrong output:\nwant: %s\nhave: %s", expected, output)
}
}
func TestInvalidIFrame(t *testing.T) { func TestInvalidIFrame(t *testing.T) {
input := `<iframe src="http://example.org/"></iframe>` input := `<iframe src="http://example.org/"></iframe>`
expected := `` expected := ``