-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.go
54 lines (43 loc) · 1016 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package watcher
import (
"errors"
"golang.org/x/net/html"
)
// A simple helper function to iterate over HTML node.
func crawlDocument(node *html.Node, handler func(node *html.Node) bool) bool {
if handler(node) {
return true
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
if crawlDocument(child, handler) {
return true
}
}
return false
}
// Extract all tags from HTML page.
func extractTags(doc *html.Node) []string {
var tags []string
crawlDocument(doc, func(node *html.Node) bool {
if node.Type == html.ElementNode {
tags = append(tags, node.Data)
}
return false
})
return tags
}
// Extract body from HTML page.
func getBody(doc *html.Node) (*html.Node, error) {
var body *html.Node
crawlDocument(doc, func(node *html.Node) bool {
if node.Type == html.ElementNode && node.Data == "body" {
body = node
return true
}
return false
})
if body != nil {
return body, nil
}
return nil, errors.New("missing <body> in the node tree")
}