-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelements.go
159 lines (117 loc) · 3.41 KB
/
elements.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
158
159
//go:build js && wasm
package js
type Elements []Element
func (lst Elements) Length() int {
return len(lst)
}
// -----------------------------------------------------------------------------
func (lst Elements) Foreach(fn func(int, Element)) {
for i, elm := range lst {
fn(i, elm)
}
}
// -----------------------------------------------------------------------------
func (lst Elements) Filter(p func(Element) bool) (ret Elements) {
ret = Elements{}
for i := range lst {
if p(lst[i]) {
ret = append(ret, lst[i])
}
}
return
}
// -----------------------------------------------------------------------------
func (lst Elements) FilterNot(p func(Element) bool) Elements {
return lst.Filter(func(elm Element) bool {
return !p(elm)
})
}
// -----------------------------------------------------------------------------
// Attr returns some attribute values.
func (lst Elements) Attr(a string) (ret []string) {
lst.Foreach(func(_ int, elm Element) {
ret = append(ret, elm.Attr(a))
})
return
}
// -----------------------------------------------------------------------------
// SetAttr sets attribute to all elements.
func (e Elements) SetAttr(a, val string) Elements {
e.Foreach(func(_ int, elm Element) {
elm.SetAttr(a, val)
})
return e
}
// -----------------------------------------------------------------------------
// Prop returns some property values.
func (e Elements) Prop(p string) (ret []Value) {
e.Foreach(func(_ int, elm Element) {
ret = append(ret, elm.Prop(p))
})
return
}
// -----------------------------------------------------------------------------
// SetProp sets property to all elements.
func (e Elements) SetProp(a string, val Value) Elements {
e.Foreach(func(_ int, elm Element) {
elm.SetProp(a, val)
})
return e
}
// -----------------------------------------------------------------------------
func (e Elements) Add(clz string) Elements {
e.Foreach(func(_ int, elm Element) {
elm.Add(clz)
})
return e
}
// -----------------------------------------------------------------------------
func (e Elements) Remove(clz string) Elements {
e.Foreach(func(_ int, elm Element) {
elm.Remove(clz)
})
return e
}
// -----------------------------------------------------------------------------
func (e Elements) Toggle(clz string) Elements {
e.Foreach(func(_ int, elm Element) {
elm.Toggle(clz)
})
return e
}
// -----------------------------------------------------------------------------
func (e Elements) Replace(oldClz, newClz string) Elements {
e.Foreach(func(_ int, elm Element) {
elm.Replace(oldClz, newClz)
})
return e
}
// -----------------------------------------------------------------------------
func (e Elements) Has(clz string) Elements {
return e.Filter(func(elm Element) bool {
return elm.Has(clz)
})
}
// -----------------------------------------------------------------------------
func (e Elements) Value() (ret []string) {
e.Foreach(func(_ int, elm Element) {
ret = append(ret, elm.Value())
})
return
}
// -----------------------------------------------------------------------------
func (e Elements) SetValue(val string) Elements {
e.Foreach(func(_ int, elm Element) {
e.SetValue(val)
})
return e
}
// -----------------------------------------------------------------------------
func ElementsOf(v Value) (ret Elements) {
size := v.Length()
ret = make([]Element, size)
for i := 0; i < size; i++ {
ret[i] = elementOf(v.Index(i))
}
return
}