This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wq.go
290 lines (240 loc) · 5.37 KB
/
wq.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package wq
import (
"html"
"sync"
"github.com/manvalls/wit"
)
// Node represents one or more HTML nodes
type Node struct {
Send func(wit.Delta) // Send will be called as a result of this node's methods
}
// Selector encapsulates a CSS selector. Must be initialised by Node.S()
type Selector struct {
selector wit.Selector
parent Node
Node
}
// Tx represents a transaction
type Tx struct {
Commit func()
Node
}
// First returns a Node that applies deltas to the first matching element
func (s Selector) First() Node {
return Node{func(delta wit.Delta) {
s.parent.Send(wit.First{
Selector: s.selector,
Delta: delta,
})
}}
}
// S returns a Selector. Accepts either a string or a wit.Selector as parameter.
func (n Node) S(selector interface{}) Selector {
var witSelector wit.Selector
switch s := selector.(type) {
case string:
witSelector = wit.S(s)
case wit.Selector:
witSelector = s
default:
witSelector = wit.S("*")
}
return Selector{
selector: witSelector,
parent: n,
Node: Node{
Send: func(delta wit.Delta) {
n.Send(wit.First{
Selector: witSelector,
Delta: delta,
})
},
},
}
}
// Root matches the root node
func (n Node) Root() Node {
return Node{func(delta wit.Delta) {
n.Send(wit.Root{
Delta: delta,
})
}}
}
// Parent matches the parents of matched elements
func (n Node) Parent() Node {
return Node{func(delta wit.Delta) {
n.Send(wit.Root{
Delta: delta,
})
}}
}
// FirstChild matches the first child of matched elements
func (n Node) FirstChild() Node {
return Node{func(delta wit.Delta) {
n.Send(wit.FirstChild{
Delta: delta,
})
}}
}
// LastChild matches the last child of matched elements
func (n Node) LastChild() Node {
return Node{func(delta wit.Delta) {
n.Send(wit.LastChild{
Delta: delta,
})
}}
}
// PrevSibling matches the previous sibling of matched elements
func (n Node) PrevSibling() Node {
return Node{func(delta wit.Delta) {
n.Send(wit.PrevSibling{
Delta: delta,
})
}}
}
// NextSibling matches the previous sibling of matched elements
func (n Node) NextSibling() Node {
return Node{func(delta wit.Delta) {
n.Send(wit.NextSibling{
Delta: delta,
})
}}
}
// Remove removes matched elements
func (n Node) Remove() Node {
n.Send(wit.Remove{})
return n
}
// Clear empties matched elements
func (n Node) Clear() Node {
n.Send(wit.Clear{})
return n
}
// Text sets the text content of matched elements
func (n Node) Text(text string) Node {
n.Send(wit.HTML{
HTMLSource: wit.HTMLFromString(html.EscapeString(text)),
})
return n
}
func getHTMLSource(html interface{}) wit.HTMLSource {
switch h := html.(type) {
case string:
return wit.HTMLFromString(h)
case wit.HTMLSource:
return h
default:
return wit.HTMLFromString("")
}
}
// HTML sets the inner HTML of matched elements. Accepts either a string or an HTMLSource as argument.
func (n Node) HTML(html interface{}) Node {
n.Send(wit.HTML{
HTMLSource: getHTMLSource(html),
})
return n
}
// Replace replaces matching elements with the provided HTML
func (n Node) Replace(html interface{}) Node {
n.Send(wit.Replace{
HTMLSource: getHTMLSource(html),
})
return n
}
// Append appends content to matched elements
func (n Node) Append(html interface{}) Node {
n.Send(wit.Append{
HTMLSource: getHTMLSource(html),
})
return n
}
// Prepend prepends content to matched elements
func (n Node) Prepend(html interface{}) Node {
n.Send(wit.Prepend{
HTMLSource: getHTMLSource(html),
})
return n
}
// InsertAfter inserts content after matched elements
func (n Node) InsertAfter(html interface{}) Node {
n.Send(wit.InsertAfter{
HTMLSource: getHTMLSource(html),
})
return n
}
// InsertBefore inserts content before matched elements
func (n Node) InsertBefore(html interface{}) Node {
n.Send(wit.InsertBefore{
HTMLSource: getHTMLSource(html),
})
return n
}
// SetAttr sets the attributes of matching elements
func (n Node) SetAttr(attr map[string]string) Node {
n.Send(wit.SetAttr{
Attributes: attr,
})
return n
}
// ReplaceAttr replaces the attributes of matching elements
func (n Node) ReplaceAttr(attr map[string]string) Node {
n.Send(wit.ReplaceAttr{
Attributes: attr,
})
return n
}
// RmAttr removes the provided attributes from matching elements
func (n Node) RmAttr(attrs ...string) Node {
n.Send(wit.RmAttr{
Attributes: attrs,
})
return n
}
// SetStyles sets the styles of matching elements
func (n Node) SetStyles(styles map[string]string) Node {
n.Send(wit.SetStyles{
Styles: styles,
})
return n
}
// RmStyles removes the provided styles from matching elements
func (n Node) RmStyles(styles ...string) Node {
n.Send(wit.RmStyles{
Styles: styles,
})
return n
}
// AddClass adds the provided class to matching elements
func (n Node) AddClass(class string) Node {
n.Send(wit.AddClasses{
Classes: class,
})
return n
}
// RmClass removes the provided class from matching elements
func (n Node) RmClass(class string) Node {
n.Send(wit.RmClasses{
Classes: class,
})
return n
}
// Tx starts a transaction against the current node
func (n Node) Tx() Tx {
mux := &sync.Mutex{}
deltas := []wit.Delta{}
return Tx{
Commit: func() {
mux.Lock()
defer mux.Unlock()
n.Send(wit.NextSibling{
Delta: wit.List{Deltas: deltas},
})
deltas = []wit.Delta{}
},
Node: Node{func(delta wit.Delta) {
mux.Lock()
defer mux.Unlock()
deltas = append(deltas, delta)
}},
}
}