htmltools

Various command line tools to transform HTML documents
git clone git://git.entf.net/htmltools
Log | Files | Refs | README | LICENSE

commit c6f10d13d6fbaf0ca6c8c4a68fc9130d8c09f839
parent 9c465bf7d511e56e292f8eb3f830a6df8965e969
Author: Lukas Henkel <lh@entf.net>
Date:   Fri, 19 Feb 2021 20:55:18 +0100

Handle Doctypes

Diffstat:
Mhtmltools.go | 12+++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/htmltools.go b/htmltools.go @@ -20,8 +20,18 @@ func Body(doc *html.Node) (*html.Node, error) { if doc.Type != html.DocumentNode { return nil, ErrNodeIsNotADocumentNode } + var htmln *html.Node + for n := doc.FirstChild; n != nil; n = n.NextSibling { + if n.Type == html.ElementNode && strings.ToLower(n.Data) == "html" { + htmln = n + break + } + } + if htmln == nil { + return nil, nil + } var body *html.Node - for n := doc.FirstChild.FirstChild; n != nil; n = n.NextSibling { + for n := htmln.FirstChild; n != nil; n = n.NextSibling { if strings.ToLower(n.Data) == "body" { body = n break