/
attributes.go
157 lines (135 loc) · 3.21 KB
/
attributes.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package hck
import (
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
var (
attrID = atom.Id.String()
attrClass = atom.Class.String()
)
func atomize(s string) (atom.Atom, string) {
// wasteful due to []byte allocs?
if a := atom.Lookup([]byte(s)); a != 0 {
return a, a.String()
}
return 0, s
}
func (as Attributes) atomize() {
for i := range as {
_, as[i].Key = atomize(as[i].Key)
}
}
type Attributes []html.Attribute
func (as Attributes) Len() int {
return len(as)
}
func (as Attributes) Swap(i, j int) {
as[i], as[j] = as[j], as[i]
}
func (as Attributes) Less(i, j int) bool {
ai, aj := &as[i], &as[j]
return ai.Namespace < aj.Namespace ||
ai.Namespace == aj.Namespace &&
ai.Key < aj.Key ||
ai.Key == aj.Key &&
ai.Val < aj.Val
}
func (as Attributes) index(key, namespace string) int {
for i := range as {
if as[i].Key == key && as[i].Namespace == namespace {
return i
}
}
return -1
}
func (as Attributes) find(key, namespace string) *html.Attribute {
if i := as.index(key, namespace); i >= 0 {
return &as[i]
}
return nil
}
func (as *Attributes) findOrAdd(key, namespace string) *html.Attribute {
if attr := as.find(key, namespace); attr != nil {
return attr
}
attrs := *as
attrs = append(attrs, html.Attribute{
Namespace: namespace,
Key: key,
})
*as = attrs
return &attrs[len(attrs)-1]
}
func (as Attributes) Get(key, namespace string) string {
if i := as.index(key, namespace); i >= 0 {
return as[i].Val
}
return ""
}
func (as *Attributes) Set(key, namespace, value string) (was string) {
a := as.findOrAdd(key, namespace)
a.Val, was = value, a.Val
return was
}
func (as Attributes) ID() string {
return as.Get(attrID, "")
}
func (as *Attributes) SetID(val string) (was string) {
return as.Set(attrID, "", val)
}
// Class retrieves the value of the "class" attribute
func (as Attributes) Class() string {
return as.Get(attrClass, "")
}
func (as *Attributes) SetClass(val string) (was string) {
return as.Set(attrClass, "", val)
}
// Classes splits the value of the class attribute into the individual classes.
func Classes(val string) []string {
// WHATWG: a class is a space separated string;
// split on sequences of space characters as defined in
// html.spec.whatwg.org/multipage/infrastructure.html#space-character
var strs []string
var i, offs int
for i = 0; i < len(val); i++ {
switch val[i] {
case ' ', '\t', '\r', '\n', '\f':
if offs < i {
strs = append(strs, val[offs:i])
}
offs = i + 1
continue
}
}
if offs < i {
strs = append(strs, val[offs:i])
}
return strs
}
func (as *Attributes) Delete(a *html.Attribute) {
s := *as
if i := s.index(a.Key, a.Namespace); i >= 0 {
copy(s[i:], s[i+1:])
*as = s[:len(s)-1]
}
}
func (as *Attributes) AddClass(class string) {
attr := as.findOrAdd(attrClass, "")
val := " " + class + " "
for _, c := range Classes(attr.Val) {
if seek := " " + c + " "; !strings.Contains(val, seek) {
val += seek[1:]
}
}
attr.Val = "" + val[1:len(val)-1]
}
func (as *Attributes) DelClass(class string) {
attr := as.find(attrClass, "")
if attr == nil {
return
}
// TODO " " is not the only separator...
val := strings.Replace(" "+attr.Val+" ", " "+class, "", -1)
attr.Val = "" + val[1:]
}