-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.v
433 lines (377 loc) · 10.1 KB
/
dom.v
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
module cdv
import x.json2 as json
import os
@[heap]
pub struct Element {
pub mut:
data string
node_id int
page &Page = unsafe { nil }
var_id int
}
@[params]
pub struct ElementParams {
pub:
node_id ?int
data ?string
}
pub fn (mut page Page) get_document_opt() !Element {
node_id := page.send_warn('DOM.getDocument').result['root']!.as_map()['nodeId']!.int()
return Element{
node_id: node_id
page: page
data: 'document'
}
}
pub fn (mut page Page) get_document() Element {
return page.get_document_opt() or { page.noop(err) }
}
pub fn (mut page Page) selector_opt(s string, params ElementParams) !Element {
root_id := params.node_id or { page.get_document().node_id }
data := params.data or { 'document' }
node_id := page.send_warn('DOM.querySelector',
params: {
'nodeId': json.Any(root_id)
'selector': s
}
).result['nodeId']!.int()
return Element{
node_id: node_id
page: page
data: '${data}.querySelector(`${s}`)'
}
}
pub fn (mut page Page) selector(s string, params ElementParams) Element {
return page.selector_opt(s, params) or { page.noop(err) }
}
pub fn (mut page Page) selector_all_opt(s string, params ElementParams) ![]Element {
root_id := params.node_id or { page.get_document().node_id }
data := params.data or { 'document' }
node_ids := page.send_warn('DOM.querySelectorAll',
params: {
'nodeId': json.Any(root_id)
'selector': s
}
).result['nodeIds']!.arr()
mut elems := []Element{}
for i, id in node_ids {
node_id := id.int()
elems << Element{
node_id: node_id
page: page
data: '${data}.querySelectorAll(`${s}`)[${i}]'
}
}
return elems
}
pub fn (mut page Page) selector_all(s string, params ElementParams) []Element {
return page.selector_all_opt(s, params) or { page.noop(err) }
}
pub type EachCb = fn (mut elem Element, i int) !
pub type FindCb = fn (mut elem Element) !bool
pub fn (mut elems []Element) each(cb EachCb) {
for i, mut elem in elems {
cb(mut elem, i) or { elem.page.noop(err) }
}
}
pub fn (mut elems []Element) find(cb FindCb) ?Element {
for mut elem in elems {
stat := cb(mut elem) or { elem.page.noop(err) }
if stat {
return elem
}
}
return none
}
pub fn (mut el Element) eval(exp string, opts RuntimeEvaluateParams) json.Any {
return el.page.eval(exp, opts)
}
pub fn (mut el Element) eval_fn(js_fn string, opts RuntimeEvaluateParams) json.Any {
mut await_promise := opts.await_promise or { false }
if !await_promise && js_fn.starts_with('async') {
await_promise = true
}
exp := '(${js_fn})(${el.data})'
return el.eval(exp, RuntimeEvaluateParams{ ...opts, await_promise: await_promise })
}
pub fn (mut el Element) selector(s string) Element {
return el.page.selector(s, node_id: el.node_id, data: el.data)
}
pub fn (mut el Element) selector_all(s string) []Element {
return el.page.selector_all(s, node_id: el.node_id, data: el.data)
}
pub fn (mut el Element) get(key string) string {
return el.eval('${el.data}.${key}').str()
}
pub fn (mut el Element) set(key string) {
el.eval('${el.data}.${key}')
}
pub fn (mut el Element) set_files_opt(paths []string) ! {
mut files := []json.Any{}
for path in paths {
if !os.exists(path) {
return error('${path} not found')
}
if os.is_abs_path(path) {
files << path
} else {
files << os.abs_path(path)
}
}
el.page.send_warn('DOM.setFileInputFiles',
params: {
'nodeId': el.node_id
'files': files
}
)
}
pub fn (mut el Element) set_files(paths []string) {
el.set_files_opt(paths) or { el.page.noop(err) }
}
pub fn (mut el Element) set_file(path string) {
el.set_files([path])
}
pub fn (mut el Element) get_file() string {
remote := el.resolve_node()
if object_id := remote.object_id {
res := el.page.send_warn('DOM.getFileInfo',
params: {
'objectId': object_id
}
).result
if path := res['path'] {
return path.str()
}
}
return ''
}
pub fn (mut el Element) click() {
el.eval('${el.data}.click()')
}
pub fn (mut el Element) set_style(key string, val json.Any) {
value := json.encode(val)
el.set('style.${key}=${value}')
}
pub fn (mut el Element) set_outer_html(val string) {
el.page.send_warn('DOM.setOuterHTML',
params: {
'nodeId': el.node_id
'outerHTML': val
}
)
}
pub fn (mut el Element) get_outer_html(opts WithBackendParams) string {
params := el.page.struct_to_json_any(WithBackendParams{ ...opts, node_id: el.node_id }).as_map()
res := el.page.send_warn('DOM.getOuterHTML', params: params).result
if outer := res['outerHTML'] {
return outer.str()
}
return ''
}
pub fn (mut el Element) set_inner_html(val string) {
el.set('innerHTML=`${val}`')
}
pub fn (mut el Element) get_inner_html() string {
return el.get('innerHTML')
}
pub fn (mut el Element) set_text_content(val string) {
el.set('textContent=`${val}`')
}
pub fn (mut el Element) get_text_content() string {
return el.get('textContent')
}
pub fn (mut el Element) set_value(val string) {
el.set('value=`${val}`')
}
pub fn (mut el Element) get_value() string {
return el.get('value')
}
pub fn (mut el Element) set_node_value(val string) {
el.page.send_warn('DOM.setNodeValue',
params: {
'nodeId': el.node_id
'value': val
}
)
}
pub fn (mut el Element) get_node_value() string {
return el.get('nodeValue')
}
pub fn (mut el Element) set_node_name(val string) {
res := el.page.send_warn('DOM.setNodeName',
params: {
'nodeId': el.node_id
'name': val
}
).result
if node_id := res['nodeId'] {
el.node_id = node_id.int()
}
}
pub fn (mut el Element) get_node_name() string {
return el.get('nodeName')
}
pub fn (mut el Element) set_attr(key string, val string) {
el.page.send_warn('DOM.setAttributeValue',
params: {
'nodeId': json.Any(el.node_id)
'name': key
'value': val
}
)
}
pub fn (mut el Element) get_attr(key string) string {
return el.get('getAttribute(`${key}`)')
}
pub fn (mut el Element) remove_attr(key string) {
el.set('removeAttribute(`${key}`)')
}
pub fn (mut el Element) get_attrs() []string {
res := el.page.send_warn('DOM.getAttributes',
params: {
'nodeId': el.node_id
}
).result
if attrs := res['attributes'] {
return attrs.arr().map(it.str())
}
return []string{}
}
@[params]
pub struct WithBackendParams {
pub:
node_id ?int @[json: 'nodeId']
backend_node_id ?int @[json: 'backendNodeId']
object_id ?string @[json: 'objectId']
}
pub fn (mut el Element) focus(opts WithBackendParams) {
params := el.page.struct_to_json_any(WithBackendParams{ ...opts, node_id: el.node_id }).as_map()
el.page.send_warn('DOM.focus', params: params)
}
@[params]
pub struct MoveToParams {
pub:
insert_before ?Element
}
pub fn (mut el Element) move_to_opt(target Element, opts MoveToParams) !Element {
node_id := el.node_id
target_node_id := target.node_id
mut params := map[string]json.Any{}
params['nodeId'] = node_id
params['targetNodeId'] = target_node_id
if insert_before := opts.insert_before {
params['insertBeforeNodeId'] = insert_before.node_id
}
res_node_id := el.page.send_warn('DOM.moveTo', params: params).result['nodeId']!.int()
return Element{
node_id: res_node_id
page: el.page
data: el.data
}
}
pub fn (mut el Element) move_to(target Element, opts MoveToParams) Element {
return el.move_to_opt(target, opts) or { el.page.noop(err) }
}
@[params]
pub struct NodeInfoParams {
pub:
node_id ?int @[json: 'nodeId']
backend_node_id ?int @[json: 'backendNodeId']
object_id ?string @[json: 'objectId']
depth ?int
pierce ?bool
}
pub fn (mut el Element) get_info(opts NodeInfoParams) map[string]json.Any {
params := el.page.struct_to_json_any(NodeInfoParams{ ...opts, node_id: el.node_id }).as_map()
res := el.page.send_warn('DOM.describeNode', params: params).result
if node := res['node'] {
return node.as_map()
}
return map[string]json.Any{}
}
@[params]
pub struct ResolveNodeParams {
pub:
node_id ?int @[json: 'nodeId']
backend_node_id ?int @[json: 'backendNodeId']
object_group ?string @[json: 'objectGroup']
execution_context_id ?int @[json: 'executionContextId']
}
pub fn (mut el Element) resolve_node(opts ResolveNodeParams) RuntimeRemoteObject {
params := el.page.struct_to_json_any(ResolveNodeParams{ ...opts, node_id: el.node_id }).as_map()
res := el.page.send_warn('DOM.resolveNode', params: params).result
if data := res['object'] {
return json.decode[RuntimeRemoteObject](data.str()) or { el.page.noop(err) }
}
return RuntimeRemoteObject{}
}
pub fn (mut el Element) scroll_into_view() {
el.eval_fn('(el) => {
el.scrollIntoView({
block: "center",
inline: "center",
behavior: "instant",
});
}')
}
pub fn (mut el Element) bounding_box() ?Viewport {
res := el.eval_fn('(el) => {
if (!(el instanceof Element)) {
return null;
}
if (el.getClientRects().length === 0) {
return null;
}
const rect = el.getBoundingClientRect();
return { x: rect.x, y: rect.y, width: rect.width, height: rect.height };
}')
if res is json.Null {
return none
}
return json.decode[Viewport](res.json_str()) or { el.page.noop(err) }
}
pub fn (mut el Element) screenshot(opts ScreenshotParams) Screenshot {
el.scroll_into_view()
mut clip := el.bounding_box() or { Viewport{} }
size := el.eval_fn('() => {
if (!window.visualViewport) {
throw new Error("visualViewport not supported");
}
return [
window.visualViewport.pageLeft,
window.visualViewport.pageTop
]
}').arr().map(it.f64())
clip.x = size[0] + (clip.x or { 0.0 })
clip.y = size[1] + (clip.y or { 0.0 })
if oc := opts.clip {
if x := oc.x {
clip.x = x + (clip.x or { 0.0 })
}
if y := oc.y {
clip.y = y + (clip.y or { 0.0 })
}
if height := oc.height {
clip.height = height
}
if width := oc.width {
clip.width = width
}
if scale := oc.scale {
clip.scale = scale
}
}
return el.page.screenshot(ScreenshotParams{ ...opts, clip: clip })
}
pub fn (mut page Page) wait_for_document_updated(params MessageParams) {
timeout := params.timeout or { def_timeout }
page.wait_for(timeout, MessageParams{ ...params, method: 'DOM.documentUpdated' })
}
pub fn (mut el Element) focus_and_click(opts WithBackendParams) {
el.focus(opts)
mut kb := el.page.use_keyboard()
kb.press('Enter')
}
pub fn (mut el Element) str() string {
return el.get_outer_html()
}