/
selectors.go
65 lines (52 loc) · 1.35 KB
/
selectors.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 target
import "strings"
type Selectors []Selector
func (s Selectors) canMergeType(selectorType Type) bool {
if len(s) == 0 {
return false
}
last := s[len(s)-1]
bothCSS := selectorType == CSS && last.Type == CSS
return bothCSS && !last.Indexed && !last.Single
}
func (s Selectors) Append(selectorType Type, value string) Selectors {
selector := Selector{Type: selectorType, Value: value}
if s.canMergeType(selectorType) {
lastIndex := len(s) - 1
selector.Value = s[lastIndex].Value + " " + selector.Value
return s[:lastIndex].append(selector)
}
return s.append(selector)
}
func (s Selectors) Single() Selectors {
lastIndex := len(s) - 1
if lastIndex < 0 {
return nil
}
selector := s[lastIndex]
selector.Single = true
selector.Indexed = false
return s[:lastIndex].append(selector)
}
func (s Selectors) At(index int) Selectors {
lastIndex := len(s) - 1
if lastIndex < 0 {
return nil
}
selector := s[lastIndex]
selector.Single = false
selector.Indexed = true
selector.Index = index
return s[:lastIndex].append(selector)
}
func (s Selectors) String() string {
var tags []string
for _, selector := range s {
tags = append(tags, selector.String())
}
return strings.Join(tags, " | ")
}
func (s Selectors) append(selector Selector) Selectors {
selectorsCopy := append(Selectors(nil), s...)
return append(selectorsCopy, selector)
}