-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.go
679 lines (600 loc) · 22.8 KB
/
document.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
package gojadoc
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/dop251/goja"
"strings"
)
type doc struct {
vm *goja.Runtime
doc *goquery.Document
docSelection *docSelection
}
type docSelection struct {
doc *doc
selection *goquery.Selection
}
func setSelectionObjectProperties(obj *goja.Object, docS *docSelection) {
_ = obj.Set("length", docS.Length)
_ = obj.Set("html", docS.Html)
_ = obj.Set("text", docS.Text)
_ = obj.Set("attr", docS.Attr)
_ = obj.Set("find", docS.Find)
_ = obj.Set("children", docS.Children)
_ = obj.Set("each", docS.Each)
_ = obj.Set("text", docS.Text)
_ = obj.Set("parent", docS.Parent)
_ = obj.Set("parentsUntil", docS.ParentsUntil)
_ = obj.Set("parents", docS.Parents)
_ = obj.Set("end", docS.End)
_ = obj.Set("closest", docS.Closest)
_ = obj.Set("map", docS.Map)
_ = obj.Set("first", docS.First)
_ = obj.Set("last", docS.Last)
_ = obj.Set("eq", docS.Eq)
_ = obj.Set("contents", docS.Contents)
_ = obj.Set("contentsFiltered", docS.ContentsFiltered)
_ = obj.Set("filter", docS.Filter)
_ = obj.Set("not", docS.Not)
_ = obj.Set("is", docS.Is)
_ = obj.Set("has", docS.Has)
_ = obj.Set("next", docS.Next)
_ = obj.Set("nextAll", docS.NextAll)
_ = obj.Set("nextUntil", docS.NextUntil)
_ = obj.Set("prev", docS.Prev)
_ = obj.Set("prevAll", docS.PrevAll)
_ = obj.Set("prevUntil", docS.PrevUntil)
_ = obj.Set("siblings", docS.Siblings)
_ = obj.Set("data", docS.Data)
_ = obj.Set("attrs", docS.Attrs)
}
func BindDocument(vm *goja.Runtime) error {
// Set Doc "class"
err := vm.Set("Doc", func(call goja.ConstructorCall) *goja.Object {
obj := call.This
if len(call.Arguments) != 1 {
return goja.Undefined().ToObject(vm)
}
html := call.Arguments[0].String()
goqueryDoc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
return goja.Undefined().ToObject(vm)
}
d := &doc{
vm: vm,
doc: goqueryDoc,
docSelection: &docSelection{
doc: nil,
selection: goqueryDoc.Selection,
},
}
d.docSelection.doc = d
setSelectionObjectProperties(obj, d.docSelection)
return obj
})
if err != nil {
return err
}
// Set "LoadDoc" function
err = vm.Set("LoadDoc", func(call goja.FunctionCall) goja.Value {
if len(call.Arguments) != 1 {
panic(vm.ToValue("missing argument"))
}
html := call.Arguments[0].String()
goqueryDoc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
return goja.Null()
}
d := &doc{
vm: vm,
doc: goqueryDoc,
docSelection: &docSelection{
doc: nil,
selection: goqueryDoc.Selection,
},
}
d.docSelection.doc = d
docSelectionFunction := func(call goja.FunctionCall) goja.Value {
selectorStr, ok := call.Argument(0).Export().(string)
if !ok {
panic(vm.NewTypeError("argument is not a string").ToString())
}
return newDocSelectionGojaValue(d, d.doc.Find(selectorStr))
}
return vm.ToValue(docSelectionFunction)
})
return nil
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Document
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func newDocSelectionGojaValue(d *doc, selection *goquery.Selection) goja.Value {
ds := &docSelection{
doc: d,
selection: selection,
}
obj := d.vm.NewObject()
setSelectionObjectProperties(obj, ds)
return obj
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Selection
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func (s *docSelection) getFirstStringArg(call goja.FunctionCall) string {
selectorStr, ok := call.Argument(0).Export().(string)
if !ok {
panic(s.doc.vm.NewTypeError("argument is not a string").ToString())
}
return selectorStr
}
func (s *docSelection) Length(call goja.FunctionCall) goja.Value {
if s.selection == nil {
return s.doc.vm.ToValue(0)
}
return s.doc.vm.ToValue(s.selection.Length())
}
// Find gets the descendants of each element in the current set of matched elements, filtered by a selector.
//
// find(selector: string): DocSelection;
func (s *docSelection) Find(call goja.FunctionCall) (ret goja.Value) {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.Find(selectorStr))
}
func (s *docSelection) Html(call goja.FunctionCall) goja.Value {
if s.selection == nil {
return goja.Null()
}
htmlStr, err := s.selection.Html()
if err != nil {
return goja.Null()
}
return s.doc.vm.ToValue(htmlStr)
}
func (s *docSelection) Text(call goja.FunctionCall) goja.Value {
if s.selection == nil {
return s.doc.vm.ToValue("")
}
return s.doc.vm.ToValue(s.selection.Text())
}
// Attr gets the specified attribute's value for the first element in the Selection. To get the value for each element individually, use a
// looping construct such as Each or Map method.
//
// attr(name: string): string | undefined;
func (s *docSelection) Attr(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
attr, found := s.selection.Attr(s.getFirstStringArg(call))
if !found {
return goja.Undefined()
}
return s.doc.vm.ToValue(attr)
}
// Attrs gets all attributes for the first element in the Selection.
//
// attrs(): { [key: string]: string };
func (s *docSelection) Attrs(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
attrs := make(map[string]string)
for _, v := range s.selection.Get(0).Attr {
attrs[v.Key] = v.Val
}
return s.doc.vm.ToValue(attrs)
}
// Data gets data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.
//
// data(name?: string): { [key: string]: string } | string | undefined;
func (s *docSelection) Data(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
var data map[string]string
n := s.selection.Get(0)
if n == nil {
return goja.Undefined()
}
for _, v := range n.Attr {
if strings.HasPrefix(v.Key, "data-") {
if data == nil {
data = make(map[string]string)
}
data[v.Key] = v.Val
}
}
return s.doc.vm.ToValue(data)
}
name := call.Argument(0).String()
n := s.selection.Get(0)
if n == nil {
return goja.Undefined()
}
data, found := s.selection.Attr(fmt.Sprintf("data-%s", name))
if !found {
return goja.Undefined()
}
return s.doc.vm.ToValue(data)
}
// Parent gets the parent of each element in the Selection. It returns a new Selection object containing the matched elements.
//
// parent(selector?: string): DocSelection;
func (s *docSelection) Parent(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
return newDocSelectionGojaValue(s.doc, s.selection.Parent())
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.ParentFiltered(selectorStr))
}
// Parents gets the ancestors of each element in the current Selection. It returns a new Selection object with the matched elements.
//
// parents(selector?: string): DocSelection;
func (s *docSelection) Parents(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
return newDocSelectionGojaValue(s.doc, s.selection.Parents())
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.ParentsFiltered(selectorStr))
}
// ParentsUntil gets the ancestors of each element in the Selection, up to but not including the element matched by the selector. It returns a
// new Selection object containing the matched elements.
//
// parentsUntil(selector?: string, until?: string): DocSelection;
func (s *docSelection) ParentsUntil(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
selectorStr := s.getFirstStringArg(call)
if len(call.Arguments) < 2 {
return newDocSelectionGojaValue(s.doc, s.selection.ParentsUntil(selectorStr))
}
untilStr := call.Argument(1).String()
return newDocSelectionGojaValue(s.doc, s.selection.ParentsFilteredUntil(selectorStr, untilStr))
}
// End ends the most recent filtering operation in the current chain and returns the set of matched elements to its previous state.
//
// end(): DocSelection;
func (s *docSelection) End(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
return newDocSelectionGojaValue(s.doc, s.selection.End())
}
// Closest gets the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.
//
// closest(selector?: string): DocSelection;
func (s *docSelection) Closest(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
return newDocSelectionGojaValue(s.doc, s.selection.Closest(""))
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.Closest(selectorStr))
}
// Next gets the next sibling of each selected element, optionally filtered by a selector.
//
// next(selector?: string): DocSelection;
func (s *docSelection) Next(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
return newDocSelectionGojaValue(s.doc, s.selection.Next())
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.NextFiltered(selectorStr))
}
// NextAll gets all following siblings of each element in the Selection, optionally filtered by a selector.
//
// nextAll(selector?: string): DocSelection;
func (s *docSelection) NextAll(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
return newDocSelectionGojaValue(s.doc, s.selection.NextAll())
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.NextAllFiltered(selectorStr))
}
// NextUntil gets all following siblings of each element up to but not including the element matched by the selector.
//
// nextUntil(selector: string, until?: string): DocSelection;
func (s *docSelection) NextUntil(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
selectorStr := s.getFirstStringArg(call)
if len(call.Arguments) < 2 {
return newDocSelectionGojaValue(s.doc, s.selection.NextUntil(selectorStr))
}
untilStr := call.Argument(1).String()
return newDocSelectionGojaValue(s.doc, s.selection.NextFilteredUntil(selectorStr, untilStr))
}
// Prev gets the previous sibling of each selected element optionally filtered by a selector.
//
// prev(selector?: string): DocSelection;
func (s *docSelection) Prev(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
return newDocSelectionGojaValue(s.doc, s.selection.Prev())
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.PrevFiltered(selectorStr))
}
// PrevAll gets all preceding siblings of each element in the Selection, optionally filtered by a selector.
//
// prevAll(selector?: string): DocSelection;
func (s *docSelection) PrevAll(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
return newDocSelectionGojaValue(s.doc, s.selection.PrevAll())
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.PrevAllFiltered(selectorStr))
}
// PrevUntil gets all preceding siblings of each element up to but not including the element matched by the selector.
//
// prevUntil(selector: string, until?: string): DocSelection;
func (s *docSelection) PrevUntil(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
selectorStr := s.getFirstStringArg(call)
if len(call.Arguments) < 2 {
return newDocSelectionGojaValue(s.doc, s.selection.PrevUntil(selectorStr))
}
untilStr := call.Argument(1).String()
return newDocSelectionGojaValue(s.doc, s.selection.PrevFilteredUntil(selectorStr, untilStr))
}
// Siblings gets the siblings of each element (excluding the element) in the set of matched elements, optionally filtered by a selector.
//
// siblings(selector?: string): DocSelection;
func (s *docSelection) Siblings(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
return newDocSelectionGojaValue(s.doc, s.selection.Siblings())
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.SiblingsFiltered(selectorStr))
}
// Children gets the element children of each element in the set of matched elements.
//
// children(selector?: string): DocSelection;
func (s *docSelection) Children(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
return newDocSelectionGojaValue(s.doc, s.selection.Children())
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.ChildrenFiltered(selectorStr))
}
// Contents gets the children of each element in the Selection, including text and comment nodes. It returns a new Selection object containing
// these elements.
//
// contents(): DocSelection;
func (s *docSelection) Contents(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
return newDocSelectionGojaValue(s.doc, s.selection.Contents())
}
// ContentsFiltered gets the children of each element in the Selection, filtered by the specified selector. It returns a new Selection object
// containing these elements. Since selectors only act on Element nodes, this function is an alias to ChildrenFiltered unless the selector is
// empty, in which case it is an alias to Contents.
//
// contentsFiltered(selector: string): DocSelection;
func (s *docSelection) ContentsFiltered(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.ContentsFiltered(selectorStr))
}
// Filter reduces the set of matched elements to those that match the selector string. It returns a new Selection object for this subset of
// matching elements.
//
// filter(selector: string | (index: number, element: DocSelection) => boolean): DocSelection;
func (s *docSelection) Filter(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
panic(s.doc.vm.ToValue("missing argument"))
}
switch call.Argument(0).Export().(type) {
case string:
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.Filter(selectorStr))
case func(call goja.FunctionCall) goja.Value:
callback := call.Argument(0).Export().(func(call goja.FunctionCall) goja.Value)
return newDocSelectionGojaValue(s.doc, s.selection.FilterFunction(func(i int, selection *goquery.Selection) bool {
ret, ok := callback(goja.FunctionCall{Arguments: []goja.Value{
s.doc.vm.ToValue(i),
newDocSelectionGojaValue(s.doc, selection),
}}).Export().(bool)
if !ok {
panic(s.doc.vm.NewTypeError("callback did not return a boolean").ToString())
}
return ret
}))
default:
panic(s.doc.vm.NewTypeError("argument is not a string or function").ToString())
}
}
// Not removes elements from the Selection that match the selector string. It returns a new Selection object with the matching elements removed.
//
// not(selector: string | (index: number, element: DocSelection) => boolean): DocSelection;
func (s *docSelection) Not(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
panic(s.doc.vm.ToValue("missing argument"))
}
switch call.Argument(0).Export().(type) {
case string:
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.Not(selectorStr))
case func(call goja.FunctionCall) goja.Value:
callback := call.Argument(0).Export().(func(call goja.FunctionCall) goja.Value)
return newDocSelectionGojaValue(s.doc, s.selection.NotFunction(func(i int, selection *goquery.Selection) bool {
ret, ok := callback(goja.FunctionCall{Arguments: []goja.Value{
s.doc.vm.ToValue(i),
newDocSelectionGojaValue(s.doc, selection),
}}).Export().(bool)
if !ok {
panic(s.doc.vm.NewTypeError("callback did not return a boolean").ToString())
}
return ret
}))
default:
panic(s.doc.vm.NewTypeError("argument is not a string or function").ToString())
}
}
// Is checks the current matched set of elements against a selector and returns true if at least one of these elements matches.
//
// is(selector: string | (index: number, element: DocSelection) => boolean): boolean;
func (s *docSelection) Is(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
if len(call.Arguments) == 0 || !gojaValueIsDefined(call.Argument(0)) {
panic(s.doc.vm.ToValue("missing argument"))
}
switch call.Argument(0).Export().(type) {
case string:
selectorStr := s.getFirstStringArg(call)
return s.doc.vm.ToValue(s.selection.Is(selectorStr))
case func(call goja.FunctionCall) goja.Value:
callback := call.Argument(0).Export().(func(call goja.FunctionCall) goja.Value)
return s.doc.vm.ToValue(s.selection.IsFunction(func(i int, selection *goquery.Selection) bool {
ret, ok := callback(goja.FunctionCall{Arguments: []goja.Value{
s.doc.vm.ToValue(i),
newDocSelectionGojaValue(s.doc, selection),
}}).Export().(bool)
if !ok {
panic(s.doc.vm.NewTypeError("callback did not return a boolean").ToString())
}
return ret
}))
default:
panic(s.doc.vm.NewTypeError("argument is not a string or function").ToString())
}
}
// Has reduces the set of matched elements to those that have a descendant that matches the selector. It returns a new Selection object with the
// matching elements.
//
// has(selector: string): DocSelection;
func (s *docSelection) Has(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
selectorStr := s.getFirstStringArg(call)
return newDocSelectionGojaValue(s.doc, s.selection.Has(selectorStr))
}
// Each iterates over a Selection object, executing a function for each matched element. It returns the current Selection object. The function f
// is called for each element in the selection with the index of the element in that selection starting at 0, and a *Selection that contains only
// that element.
//
// each(callback: (index: number, element: DocSelection) => void): DocSelection;
func (s *docSelection) Each(call goja.FunctionCall) (ret goja.Value) {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
callback, ok := call.Argument(0).Export().(func(call goja.FunctionCall) goja.Value)
if !ok {
panic(s.doc.vm.NewTypeError("argument is not a function").ToString())
}
s.selection.Each(func(i int, selection *goquery.Selection) {
callback(goja.FunctionCall{Arguments: []goja.Value{
s.doc.vm.ToValue(i),
newDocSelectionGojaValue(s.doc, selection),
}})
})
return goja.Undefined()
}
// Map passes each element in the current matched set through a function, producing a slice of string holding the returned values. The function f
// is called for each element in the selection with the index of the element in that selection starting at 0, and a *Selection that contains only
// that element.
//
// map(callback: (index: number, element: DocSelection) => DocSelection): DocSelection[];
func (s *docSelection) Map(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
callback, ok := call.Argument(0).Export().(func(call goja.FunctionCall) goja.Value)
if !ok {
panic(s.doc.vm.NewTypeError("argument is not a function").ToString())
}
var retStr []interface{}
var retDocSelection map[string]interface{}
s.selection.Each(func(i int, selection *goquery.Selection) {
val := callback(goja.FunctionCall{Arguments: []goja.Value{
s.doc.vm.ToValue(i),
newDocSelectionGojaValue(s.doc, selection),
}})
if valExport, ok := val.Export().(map[string]interface{}); ok {
retDocSelection = valExport
}
retStr = append(retStr, val.Export())
})
if len(retStr) > 0 {
return s.doc.vm.ToValue(retStr)
}
return s.doc.vm.ToValue(retDocSelection)
}
// First reduces the set of matched elements to the first in the set. It returns a new Selection object, and an empty Selection object if the
// selection is empty.
//
// first(): DocSelection;
func (s *docSelection) First(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
return newDocSelectionGojaValue(s.doc, s.selection.First())
}
// Last reduces the set of matched elements to the last in the set. It returns a new Selection object, and an empty Selection object if the
// selection is empty.
//
// last(): DocSelection;
func (s *docSelection) Last(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
return newDocSelectionGojaValue(s.doc, s.selection.Last())
}
// Eq reduces the set of matched elements to the one at the specified index. If a negative index is given, it counts backwards starting at the
// end of the set. It returns a new Selection object, and an empty Selection object if the index is invalid.
//
// eq(index: number): DocSelection;
func (s *docSelection) Eq(call goja.FunctionCall) goja.Value {
if s.selection == nil {
panic(s.doc.vm.ToValue("selection is nil"))
}
index, ok := call.Argument(0).Export().(int64)
if !ok {
panic(s.doc.vm.NewTypeError("argument is not a number").String())
}
return newDocSelectionGojaValue(s.doc, s.selection.Eq(int(index)))
}