-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.go
65 lines (58 loc) · 1.37 KB
/
element.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
55
56
57
58
59
60
61
62
63
64
65
package GABAHTMLParser
import (
"strings"
)
type Element struct {
Parent *Element
Child []*Element
Tag string
InnerHTML string
Attr map[string]string
}
func (element *Element) Find(condition string) []*Element{
results := []*Element{}
conditions := strings.Split(condition,"&&")
for _, child := range element.Child {
innerSearch(conditions,child,&results)
}
return results
}
func innerSearch(conditions []string, pointer *Element, array *[]*Element){
isMatch := true
for _, cond := range conditions {
condpair := strings.Split(cond,"=")
key := strings.Replace(condpair[0]," ","",-1)
values := strings.Replace(condpair[1], "\"", "", -1)
values = strings.Replace(values, "'", "", -1)
valuearray := strings.Fields(values)
if key == "tag"{
tagName := strings.Replace(valuearray[0]," ","",-1)
if pointer.Tag != tagName{
isMatch = false
}
}else{
if val, ok := pointer.Attr[key]; ok {
for _,valchild := range valuearray{
htmlvals := strings.Split(val," ")
foundval := false
for _,htmlval := range htmlvals{
if htmlval == valchild{
foundval = true
}
}
if !foundval{
isMatch = false
}
}
}else{
isMatch = false
}
}
}
if isMatch{
*array = append(*array, pointer)
}
for _, child := range pointer.Child {
innerSearch(conditions,child,array)
}
}