-
Notifications
You must be signed in to change notification settings - Fork 0
/
selector_simple.go
92 lines (79 loc) · 1.78 KB
/
selector_simple.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package bracelet
import "strings"
type simpleSelector struct {
Tag string
ID string
Classes []string
AttributeSelectors []attributeSelector
}
func (s *simpleSelector) String() string {
result := s.Tag
if s.ID != "" {
result += "#" + s.ID
}
for _, class := range s.Classes {
result += "." + class
}
return result
}
func (s *simpleSelector) Specificity() specificity {
a := 0 // ID
b := len(s.Classes) + len(s.AttributeSelectors) // Class and attribute selectors
c := 0 // Type
if s.ID != "" {
a = 1
}
if s.Tag != "" {
c = 1
}
return specificity{a, b, c}
}
func (s *simpleSelector) Matches(node *Node) bool {
if s.Tag != "" && s.Tag != (*node).GetTag() {
return false
}
if s.ID != "" && s.ID != (*node).GetID() {
return false
}
contains := func(slice []string, item string) bool {
for _, a := range slice {
if a == item {
return true
}
}
return false
}
nodeClasses := (*node).GetClasses()
for _, class := range s.Classes {
if !contains(nodeClasses, class) {
return false
}
}
for _, attrSelector := range s.AttributeSelectors {
if !attrSelector.Matches(node) {
return false
}
}
return true
}
func parseSimpleSelector(token string) (Selector, error) {
selector := &simpleSelector{}
parts := strings.Split(token, ":")
if parts[0] != "" {
mainParts := strings.Split(parts[0], ".")
if len(mainParts) > 0 && mainParts[0] != "" {
if mainParts[0][0] == '#' {
selector.ID = mainParts[0][1:]
} else {
selector.Tag = mainParts[0]
}
}
if len(mainParts) > 1 {
selector.Classes = mainParts[1:]
}
}
if len(parts) > 1 {
return parsePseudoSelector(selector, parts[1])
}
return selector, nil
}