feat: add NewDocumentFromReaderWithOptions

goquery calls html.Parse, so callers cannot reach the parse options that
golang.org/x/net/html has since grown. The one that matters is
ParseOptionEnableScripting(false): with scripting assumed enabled the
contents of a noscript element are parsed as raw text and no selector
can reach inside it.

Add a constructor taking html.ParseOption values. Existing constructors
are untouched.

Refs #139
This commit is contained in:
dualfroz committed 2026-09-06 01:24:56 +02:00
1 parent 8592be9b0e
commit 3701788802
2 files changed
+55

No files matched your search

+18
View File
@@ -56,6 +56,24 @@ func NewDocumentFromReader(r io.Reader) (*Document, error) {
return newDocument(root, nil), nil
}
// NewDocumentFromReaderWithOptions is like NewDocumentFromReader, with the
// parse options of the html package.
//
// The option that usually matters is html.ParseOptionEnableScripting(false),
// which makes <noscript> content reachable by selectors:
//
// doc, err := goquery.NewDocumentFromReaderWithOptions(r,
// html.ParseOptionEnableScripting(false))
//
// The options apply to this parse only.
func NewDocumentFromReaderWithOptions(r io.Reader, opts ...html.ParseOption) (*Document, error) {
root, e := html.ParseWithOptions(r, opts...)
if e != nil {
return nil, e
}
return newDocument(root, nil), nil
}
// NewDocumentFromResponse is another Document constructor that takes an http response as argument.
// It loads the specified response's document, parses it, and stores the root Document
// node, ready to be manipulated. The response's body is closed on return.