-
Notifications
You must be signed in to change notification settings - Fork 1
/
text.go
41 lines (37 loc) · 866 Bytes
/
text.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
package tokeq
import (
"strings"
"golang.org/x/net/html"
)
// FindText - finds text in given node
func FindText(input *html.Node) (data string) {
var buffer string
for c := input.FirstChild; c != nil; c = c.NextSibling {
if c.Parent.DataAtom == input.DataAtom && c.Type == html.TextNode {
buffer = strings.TrimSpace(c.Data)
if len(buffer) > 0 {
data += " " + buffer
}
}
FindText(c)
}
return strings.TrimSpace(data)
}
// FindDeepText - finds text in given & child nodes
func FindDeepText(n *html.Node) (data string) {
var buffer string
var f func(*html.Node)
f = func(input *html.Node) {
for c := input.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.TextNode {
buffer = strings.TrimSpace(c.Data)
if len(buffer) > 0 {
data += " " + buffer
}
}
f(c)
}
}
f(n)
return strings.TrimSpace(data)
}