Categorygithub.com/bobg/htree/v2
modulepackage
2.2.0
Repository: https://github.com/bobg/htree.git
Documentation: pkg.go.dev

# README

Htree - Go package for working with html.Node trees

Go Reference Go Report Card Tests Coverage Status Mentioned in Awesome Go

This is htree, a Go package that helps traverse, navigate, filter, and otherwise process trees of html.Node objects.

Usage

root, err := html.Parse(input)
if err != nil { ... }

body := htree.FindEl(root, func(n *html.Node) bool {
  return n.DataAtom == atom.Body
})

content := htree.FindEl(body, func(n *html.Node) bool {
  return n.DataAtom == atom.Div && htree.ElClassContains(n, "content")
})

...etc...

# Functions

ElAttr returns `node`'s value for the attribute `key`.
ElClassContains tells whether `node` has a `class` attribute containing the class name `probe`.
Find finds the first node, in a depth-first search of the given tree, satisfying the given predicate.
FindAll produces an iterator over the nodes in the tree that satisfy the given predicate, skipping that node's children.
FindAllChildEls is the same as [FindAllEls] but operates only on the children of `node`, not `node` itself.
FindAllChildren is the same as [FindAll] but operates only on the children of `node`, not `node` itself.
FindAllEls is like [FindAll] but calls `pred` only for nodes with type `ElementNode`.
FindEl finds the first `ElementNode`-typed node, in a depth-first search of the tree, satisfying the given predicate.
Indent writes the HTML node n to w with indentation to show nesting.
Indent writes the HTML node n to w with indentation to show nesting and ensures the output ends with a newline.
Prune returns a copy of `node` and its children, minus any subnodes that cause the supplied predicate to return true.
Text returns the content of the tree rooted at `node` as plain text.
Walk produces an iterator over the nodes in the tree in a recursive, preorder, depth-first walk.
WriteText converts the content of the tree rooted at `node` into plain text and writes it to `w`.