Documentation ¶
Overview ¶
Package htmlutil provides some html utility functions.
Index ¶
- func Attr(n *html.Node, key string) string
- func First(n *html.Node, fn MatchFunc) *html.Node
- func HasAttr(n *html.Node, key, val string) bool
- func HasText(n *html.Node, sub string) bool
- func Int(n *html.Node) (int, error)
- func Int64(n *html.Node) (int64, error)
- func IsElement(n *html.Node, data string) bool
- func Last(n *html.Node, fn MatchFunc) *html.Node
- func Real(n *html.Node) (float64, error)
- func Text(n *html.Node) string
- func Uint64(n *html.Node) (uint64, error)
- func Walk(n *html.Node, fn MatchFunc)
- type MatchFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Attr ¶
Attr returns the value of keyed attribute under n. If no attribute matches the key, an empty string is returned.
Example ¶
frag := `<html lang="en"></html>` n, _ := html.Parse(strings.NewReader(frag)) // ignore error lang := htmlutil.Attr(n.FirstChild, "lang") fmt.Println(lang)
Output: en
func First ¶
First works like Walk(n, fn) but returns the node once fn(node) returns true.
To compare First(), Last() and Walk(), consider a MatchFunc, fn looks like this:
func(n *html.Node) bool { return htmlutil.IsElement(n, "li") }
Base on following HTML fragment, they work as:
<ul> | First(n, fn) | Last(n, fn) | Walk(n, fn) | <li>...</li> | return node | not walked | walked | <li>...</li> | not walked | not walked | walked | <li>...</li> | not walked | return node | walked | </ul>
func Int ¶
Int returns the first integer it found in n and n's children. Floating-point numbers are ignored. If no integer was found, it returns error.
Example ¶
frag := "<p>2018</p>" n, _ := html.Parse(strings.NewReader(frag)) // ignore error i, _ := htmlutil.Int(n) fmt.Println(i)
Output: 2018
func IsElement ¶
IsElement returns whether n is a html.ElementNode and n.Data matches given data.
Example ¶
frag := "<html></html>" n, _ := html.Parse(strings.NewReader(frag)) // ignore error b := htmlutil.IsElement(n.FirstChild, "html") fmt.Println(b)
Output: true
func Last ¶
Last works like First(n, fn) but searches nodes in different direction. It returns the last matched node in the node tree.
func Real ¶
Real returns the first number it found in n and n's children. If no number was found, it returns error.
func Text ¶
Text returns the content of n's first text child node. If no html.TextNode was found, it returns empty string.
Example ¶
frag := "<p>Hello <strong>World</strong></p>" n, _ := html.Parse(strings.NewReader(frag)) // ignore error txt := htmlutil.Text(n) fmt.Println(txt)
Output: Hello
func Walk ¶
Walk calls fn with n and all the children under n. If fn(n) returns true, it keeps searching the node's siblings but children.
Example ¶
frag := ` <div id="first"> <div id="child"></div> </div> <div id="sibling"></div>` n, err := html.Parse(strings.NewReader(frag)) if err != nil { fmt.Println(err) } fn := func(n *html.Node) bool { found := htmlutil.IsElement(n, "div") if found { fmt.Println(htmlutil.Attr(n, "id")) } return found } htmlutil.Walk(n, fn)
Output: first sibling