Apply the document's parse options to HTML added to it later

This commit is contained in:
dualfroz committed 2026-09-23 07:01:09 +02:00
1 parent bff34b529a
commit 52ce458d92
3 files changed
+53 -15

No files matched your search

+26
View File
@@ -290,3 +290,29 @@ func TestNewDocumentFromReaderWithOptionsNoOptions(t *testing.T) {
t.Errorf("got %q, want %q", got, "hi")
}
}
func TestNewDocumentFromReaderWithOptionsAppliesToAddedHtml(t *testing.T) {
const doc = `<html><body><p>before</p></body></html>`
const frag = `<noscript><a href="http://example.org">click this link</a></noscript>`
// The options given to the constructor hold for HTML added later, so the
// fragment is parsed the same way the document was.
d, e := NewDocumentFromReaderWithOptions(strings.NewReader(doc), html.ParseOptionEnableScripting(false))
if e != nil {
t.Fatal(e)
}
d.Find("body").AppendHtml(frag)
if _, ok := d.Find("noscript a").Attr("href"); !ok {
t.Error("expected the added noscript to be parsed with scripting disabled")
}
// Without the options the added fragment keeps the default behaviour.
d, e = NewDocumentFromReader(strings.NewReader(doc))
if e != nil {
t.Fatal(e)
}
d.Find("body").AppendHtml(frag)
if _, ok := d.Find("noscript a").Attr("href"); ok {
t.Error("expected no match inside the added noscript with scripting enabled")
}
}