-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
49 lines (43 loc) · 939 Bytes
/
parser.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
package main
import (
"strings"
"golang.org/x/net/html"
)
// GetFirstTextNode ...
func GetFirstTextNode(htmlParsed *html.Node) *html.Node {
if htmlParsed == nil {
return nil
}
for m := htmlParsed.FirstChild; m != nil; m = m.NextSibling {
if m.Type == html.TextNode {
return m
}
r := GetFirstTextNode(m)
if r != nil {
return r
}
}
return nil
}
// GetFirstElementByClass ...
func GetFirstElementByClass(htmlParsed *html.Node, elm, className string) *html.Node {
for m := htmlParsed.FirstChild; m != nil; m = m.NextSibling {
if m.Data == elm && HasClass(m.Attr, className) {
return m
}
r := GetFirstElementByClass(m, elm, className)
if r != nil {
return r
}
}
return nil
}
// HasClass ...
func HasClass(attribs []html.Attribute, className string) bool {
for _, attr := range attribs {
if attr.Key == "class" && strings.Contains(attr.Val, className) {
return true
}
}
return false
}