Documentation ¶
Overview ¶
Package xhtml makes x/net/html easier
Index ¶
- func AdoptChildren(dst, src *html.Node)
- func AppendText(n *html.Node, text string)
- func Attr(n *html.Node, name string) string
- func Clone(n *html.Node) *html.Node
- func Closest(n *html.Node, match Selector) *html.Node
- func DeepEqual(a, b *html.Node) bool
- func DeleteAttr(n *html.Node, key string)
- func InnerHTML(n *html.Node) string
- func InnerHTMLBlocks(n *html.Node) string
- func IsBalanced(s string) bool
- func LastChildOrNew(p *html.Node, tag string, attrs ...string) *html.Node
- func New(tag string, attrs ...string) *html.Node
- func OuterHTML(n *html.Node) string
- func RemoveAll(nodes []*html.Node)
- func ReplaceWith(old, new *html.Node)
- func Select(n *html.Node, match Selector) *html.Node
- func SelectAll(n *html.Node, match Selector) iter.Seq[*html.Node]
- func SelectSlice(n *html.Node, match Selector) []*html.Node
- func SetAttr(n *html.Node, key, value string)
- func SetInnerHTML(n *html.Node, s string) error
- func ShallowEqual(a, b *html.Node) bool
- func TextContent(n *html.Node) string
- func ToBuffer(n *html.Node) *bytes.Buffer
- func UnnestChildren(n *html.Node)
- type Selector
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdoptChildren ¶
func AppendText ¶
func Closest ¶
Closest traverses the node and its parents until it finds a node that matches the Selector.
Example ¶
package main import ( "fmt" "strings" "github.com/earthboundkid/xhtml" "golang.org/x/net/html" ) func main() { doc, err := html.Parse(strings.NewReader(` <div data-server-id="abc123"> <button id="theButton"></button </div>`)) if err != nil { panic(err) } // Find #theButton bttnEl := xhtml.Select(doc, xhtml.WithID("theButton")) // Find its data-server-id serverIDEl := xhtml.Closest(bttnEl, xhtml.WithDataset("server-id")) serverID := xhtml.Attr(serverIDEl, "data-server-id") fmt.Println(serverID) }
Output: abc123
func DeepEqual ¶
DeepEqual returns true if a and b are ShallowEqual and all of their descendants are ShallowEqual as well.
func DeleteAttr ¶
func InnerHTMLBlocks ¶
InnerHTMLBlocks is the same as InnerHTML, but it separates top level nodes with a line break.
func IsBalanced ¶
IsBalanced reports whether every opening tag has a closing pair.
func LastChildOrNew ¶
LastChildOrNew returns the last child of p if it is ShallowEqual to a new *html.Node with tag and attrs. Otherwise, it appends a new *html.Node with tag and attrs and returns that. For why this is operation useful, see Converting docx to clean HTML.
func RemoveAll ¶
RemoveAll orphans the nodes it is passed. It ignores a node if the node is nil or already an orphan.
func ReplaceWith ¶
func SelectAll ¶
SelectAll returns an iterator yielding matching nodes in n.Descendants().
Example ¶
package main import ( "fmt" "strings" "github.com/earthboundkid/xhtml" "golang.org/x/net/html" "golang.org/x/net/html/atom" ) func main() { doc, err := html.Parse(strings.NewReader(` <ul> <li><a href="https://example.com/en-us">Hello, World!</a></li> <li><a href="https://example.com/ja-jp">こんにちは世界!</a></li> </ul>`)) if err != nil { panic(err) } // Find all links in a document // And print the link URL and text for link := range xhtml.SelectAll(doc, xhtml.WithAtom(atom.A)) { fmt.Println(xhtml.Attr(link, "href"), xhtml.TextContent(link)) } }
Output: https://example.com/en-us Hello, World! https://example.com/ja-jp こんにちは世界!
func SelectSlice ¶
SelectSlice returns a slice of descendant nodes matched by the Selector.
func ShallowEqual ¶
ShallowEqual returns true if a and b have the same Type, DataAtom, Data, Namespace, and Attr. It does not look at parents, children, or siblings. nil *html.Nodes are never equal to each other.
func TextContent ¶
TextContent joins and trims the text node children of n.
func UnnestChildren ¶
UnnestChildren has all of the children of node adopted by its parent, and then it removes the node.
Types ¶
type Selector ¶
Selector is a function that matches html.Nodes.
func WithDataset ¶
WithDataset returns a Selector that matches html.Nodes with the given data attribute set. The attribute should be in kebab-case, not camelCase, without the "data-" prefix.