package ibd import ( "strings" "golang.org/x/net/html" ) func findChildren(node *html.Node, f func(node *html.Node) bool) (found []*html.Node) { for c := node.FirstChild; c != nil; c = c.NextSibling { if f(c) { found = append(found, c) } } return } func findChildrenRecursive(node *html.Node, f func(node *html.Node) bool) (found []*html.Node) { if f(node) { found = append(found, node) } for c := node.FirstChild; c != nil; c = c.NextSibling { found = append(found, findChildrenRecursive(c, f)...) } return } func findClass(node *html.Node, className string) (found *html.Node) { if isClass(node, className) { return node } for c := node.FirstChild; c != nil; c = c.NextSibling { if found = findClass(c, className); found != nil { return } } return } func isClass(node *html.Node, className string) bool { if node.Type == html.ElementNode { for _, attr := range node.Attr { if attr.Key != "class" { continue } classes := strings.Fields(attr.Val) for _, class := range classes { if class == className { return true } } } } return false } func extractText(node *html.Node) string { var result strings.Builder extractTextInner(node, &result) return result.String() } func extractTextInner(node *html.Node, result *strings.Builder) { if node.Type == html.TextNode { result.WriteString(node.Data) } for c := node.FirstChild; c != nil; c = c.NextSibling { extractTextInner(c, result) } } func findId(node *html.Node, id string) (found *html.Node) { if isId(node, id) { return node } for c := node.FirstChild; c != nil; c = c.NextSibling { if found = findId(c, id); found != nil { return } } return } func isId(node *html.Node, id string) bool { if node.Type == html.ElementNode { for _, attr := range node.Attr { if attr.Key == "id" && attr.Val == id { return true } } } return false }