forked from dominikh/go-js-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.go
3113 lines (2588 loc) · 81 KB
/
dom.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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package dom provides GopherJS bindings for the JavaScript DOM APIs.
//
// This package is an in progress effort of providing idiomatic Go
// bindings for the DOM, wrapping the JavaScript DOM APIs. The API is
// neither complete nor frozen yet, but a great amount of the DOM is
// already useable.
//
// While the package tries to be idiomatic Go, it also tries to stick
// closely to the JavaScript APIs, so that one does not need to learn
// a new set of APIs if one is already familiar with it.
//
// One decision that hasn't been made yet is what parts exactly should
// be part of this package. It is, for example, possible that the
// canvas APIs will live in a separate package. On the other hand,
// types such as StorageEvent (the event that gets fired when the
// HTML5 storage area changes) will be part of this package, simply
// due to how the DOM is structured – even if the actual storage APIs
// might live in a separate package. This might require special care
// to avoid circular dependencies.
//
// The documentation for some of the identifiers is based on the
// MDN Web Docs by Mozilla Contributors (https://developer.mozilla.org/en-US/docs/Web/API),
// licensed under CC-BY-SA 2.5 (https://creativecommons.org/licenses/by-sa/2.5/).
//
// # Getting started
//
// The usual entry point of using the dom package is by using the
// GetWindow() function which will return a Window, from which you can
// get things such as the current Document.
//
// # Interfaces
//
// The DOM has a big amount of different element and event types, but
// they all follow three interfaces. All functions that work on or
// return generic elements/events will return one of the three
// interfaces Element, HTMLElement or Event. In these interface values
// there will be concrete implementations, such as
// HTMLParagraphElement or FocusEvent. It's also not unusual that
// values of type Element also implement HTMLElement. In all cases,
// type assertions can be used.
//
// Example:
//
// el := dom.GetWindow().Document().QuerySelector(".some-element")
// htmlEl := el.(dom.HTMLElement)
// pEl := el.(*dom.HTMLParagraphElement)
//
// # Live collections
//
// Several functions in the JavaScript DOM return "live"
// collections of elements, that is collections that will be
// automatically updated when elements get removed or added to the
// DOM. Our bindings, however, return static slices of elements that,
// once created, will not automatically reflect updates to the DOM.
// This is primarily done so that slices can actually be used, as
// opposed to a form of iterator, but also because we think that
// magically changing data isn't Go's nature and that snapshots of
// state are a lot easier to reason about.
//
// This does not, however, mean that all objects are snapshots.
// Elements, events and generally objects that aren't slices or maps
// are simple wrappers around JavaScript objects, and as such
// attributes as well as method calls will always return the most
// current data. To reflect this behaviour, these bindings use
// pointers to make the semantics clear. Consider the following
// example:
//
// d := dom.GetWindow().Document()
// e1 := d.GetElementByID("my-element")
// e2 := d.GetElementByID("my-element")
//
// e1.Class().SetString("some-class")
// println(e1.Class().String() == e2.Class().String())
//
// The above example will print `true`.
//
// # DOMTokenList
//
// Some objects in the JS API have two versions of attributes, one
// that returns a string and one that returns a DOMTokenList to ease
// manipulation of string-delimited lists. Some other objects only
// provide DOMTokenList, sometimes DOMSettableTokenList. To simplify
// these bindings, only the DOMTokenList variant will be made
// available, by the type TokenList. In cases where the string
// attribute was the only way to completely replace the value, our
// TokenList will provide Set([]string) and SetString(string) methods,
// which will be able to accomplish the same. Additionally, our
// TokenList will provide methods to convert it to strings and slices.
//
// # Backwards compatibility
//
// This package has a relatively stable API. However, there will be
// backwards incompatible changes from time to time. This is because
// the package isn't complete yet, as well as because the DOM is a
// moving target, and APIs do change sometimes.
//
// While an attempt is made to reduce changing function signatures to
// a minimum, it can't always be guaranteed. Sometimes mistakes in the
// bindings are found that require changing arguments or return
// values.
//
// Interfaces defined in this package may also change on a
// semi-regular basis, as new methods are added to them. This happens
// because the bindings aren't complete and can never really be, as
// new features are added to the DOM.
package dom // import "honnef.co/go/js/dom"
import (
"image"
"image/color"
"strings"
"time"
"github.com/gopherjs/gopherjs/js"
)
// toString returns the string representation of o. If o is nil or
// undefined, the empty string will be returned instead.
func toString(o *js.Object) string {
if o == nil || o == js.Undefined {
return ""
}
return o.String()
}
func callRecover(o *js.Object, fn string, args ...interface{}) (err error) {
defer func() {
e := recover()
if e == nil {
return
}
if panicErr, ok := e.(error); ok && panicErr != nil {
err = panicErr
} else {
panic(e)
}
}()
o.Call(fn, args...)
return nil
}
func elementConstructor(o *js.Object) *js.Object {
if n := o.Get("node"); n != js.Undefined {
// Support elements wrapped in Polymer's DOM APIs.
return n.Get("constructor")
}
return o.Get("constructor")
}
func arrayToObjects(o *js.Object) []*js.Object {
var out []*js.Object
for i := 0; i < o.Length(); i++ {
out = append(out, o.Index(i))
}
return out
}
func nodeListToObjects(o *js.Object) []*js.Object {
if o.Get("constructor") == js.Global.Get("Array") {
// Support Polymer's DOM APIs, which uses Arrays instead of
// NodeLists
return arrayToObjects(o)
}
var out []*js.Object
length := o.Get("length").Int()
for i := 0; i < length; i++ {
out = append(out, o.Call("item", i))
}
return out
}
func nodeListToNodes(o *js.Object) []Node {
var out []Node
for _, obj := range nodeListToObjects(o) {
out = append(out, wrapNode(obj))
}
return out
}
func nodeListToElements(o *js.Object) []Element {
var out []Element
for _, obj := range nodeListToObjects(o) {
out = append(out, wrapElement(obj))
}
return out
}
func nodeListToHTMLElements(o *js.Object) []HTMLElement {
var out []HTMLElement
for _, obj := range nodeListToObjects(o) {
out = append(out, wrapHTMLElement(obj))
}
return out
}
func WrapDocument(o *js.Object) Document {
return wrapDocument(o)
}
func WrapDocumentFragment(o *js.Object) DocumentFragment {
return wrapDocumentFragment(o)
}
func WrapNode(o *js.Object) Node {
return wrapNode(o)
}
func WrapElement(o *js.Object) Element {
return wrapElement(o)
}
func WrapHTMLElement(o *js.Object) HTMLElement {
return wrapHTMLElement(o)
}
func wrapDocument(o *js.Object) Document {
switch elementConstructor(o) {
case js.Global.Get("HTMLDocument"):
return &htmlDocument{&document{&BasicNode{o}}}
default:
return &document{&BasicNode{o}}
}
}
func wrapDocumentFragment(o *js.Object) DocumentFragment {
switch elementConstructor(o) {
// TODO: do we have any other stuff we want to check
default:
return &documentFragment{&BasicNode{o}}
}
}
func wrapNode(o *js.Object) Node {
if o == nil || o == js.Undefined {
return nil
}
switch elementConstructor(o) {
// TODO all the non-element cases
case js.Global.Get("Text"):
return &Text{&BasicNode{o}}
default:
return wrapElement(o)
}
}
func wrapElement(o *js.Object) Element {
if o == nil || o == js.Undefined {
return nil
}
switch elementConstructor(o) {
// TODO all the non-HTML cases
default:
return wrapHTMLElement(o)
}
}
func wrapHTMLElement(o *js.Object) HTMLElement {
if o == nil || o == js.Undefined {
return nil
}
el := &BasicHTMLElement{&BasicElement{&BasicNode{o}}}
c := elementConstructor(o)
switch c {
case js.Global.Get("HTMLAnchorElement"):
return &HTMLAnchorElement{BasicHTMLElement: el, URLUtils: &URLUtils{Object: o}}
case js.Global.Get("HTMLAppletElement"):
return &HTMLAppletElement{BasicHTMLElement: el}
case js.Global.Get("HTMLAreaElement"):
return &HTMLAreaElement{BasicHTMLElement: el, URLUtils: &URLUtils{Object: o}}
case js.Global.Get("HTMLAudioElement"):
return &HTMLAudioElement{HTMLMediaElement: &HTMLMediaElement{BasicHTMLElement: el}}
case js.Global.Get("HTMLBaseElement"):
return &HTMLBaseElement{BasicHTMLElement: el}
case js.Global.Get("HTMLBodyElement"):
return &HTMLBodyElement{BasicHTMLElement: el}
case js.Global.Get("HTMLBRElement"):
return &HTMLBRElement{BasicHTMLElement: el}
case js.Global.Get("HTMLButtonElement"):
return &HTMLButtonElement{BasicHTMLElement: el}
case js.Global.Get("HTMLCanvasElement"):
return &HTMLCanvasElement{BasicHTMLElement: el}
case js.Global.Get("HTMLDataElement"):
return &HTMLDataElement{BasicHTMLElement: el}
case js.Global.Get("HTMLDataListElement"):
return &HTMLDataListElement{BasicHTMLElement: el}
case js.Global.Get("HTMLDetailsElement"):
return &HTMLDetailsElement{BasicHTMLElement: el}
case js.Global.Get("HTMLDirectoryElement"):
return &HTMLDirectoryElement{BasicHTMLElement: el}
case js.Global.Get("HTMLDivElement"):
return &HTMLDivElement{BasicHTMLElement: el}
case js.Global.Get("HTMLDListElement"):
return &HTMLDListElement{BasicHTMLElement: el}
case js.Global.Get("HTMLEmbedElement"):
return &HTMLEmbedElement{BasicHTMLElement: el}
case js.Global.Get("HTMLFieldSetElement"):
return &HTMLFieldSetElement{BasicHTMLElement: el}
case js.Global.Get("HTMLFontElement"):
return &HTMLFontElement{BasicHTMLElement: el}
case js.Global.Get("HTMLFormElement"):
return &HTMLFormElement{BasicHTMLElement: el}
case js.Global.Get("HTMLFrameElement"):
return &HTMLFrameElement{BasicHTMLElement: el}
case js.Global.Get("HTMLFrameSetElement"):
return &HTMLFrameSetElement{BasicHTMLElement: el}
case js.Global.Get("HTMLHeadElement"):
return &HTMLHeadElement{BasicHTMLElement: el}
case js.Global.Get("HTMLHeadingElement"):
return &HTMLHeadingElement{BasicHTMLElement: el}
case js.Global.Get("HTMLHtmlElement"):
return &HTMLHtmlElement{BasicHTMLElement: el}
case js.Global.Get("HTMLHRElement"):
return &HTMLHRElement{BasicHTMLElement: el}
case js.Global.Get("HTMLIFrameElement"):
return &HTMLIFrameElement{BasicHTMLElement: el}
case js.Global.Get("HTMLImageElement"):
return &HTMLImageElement{BasicHTMLElement: el}
case js.Global.Get("HTMLInputElement"):
return &HTMLInputElement{BasicHTMLElement: el}
case js.Global.Get("HTMLKeygenElement"):
return &HTMLKeygenElement{BasicHTMLElement: el}
case js.Global.Get("HTMLLabelElement"):
return &HTMLLabelElement{BasicHTMLElement: el}
case js.Global.Get("HTMLLegendElement"):
return &HTMLLegendElement{BasicHTMLElement: el}
case js.Global.Get("HTMLLIElement"):
return &HTMLLIElement{BasicHTMLElement: el}
case js.Global.Get("HTMLLinkElement"):
return &HTMLLinkElement{BasicHTMLElement: el}
case js.Global.Get("HTMLMapElement"):
return &HTMLMapElement{BasicHTMLElement: el}
case js.Global.Get("HTMLMediaElement"):
return &HTMLMediaElement{BasicHTMLElement: el}
case js.Global.Get("HTMLMenuElement"):
return &HTMLMenuElement{BasicHTMLElement: el}
case js.Global.Get("HTMLMetaElement"):
return &HTMLMetaElement{BasicHTMLElement: el}
case js.Global.Get("HTMLMeterElement"):
return &HTMLMeterElement{BasicHTMLElement: el}
case js.Global.Get("HTMLModElement"):
return &HTMLModElement{BasicHTMLElement: el}
case js.Global.Get("HTMLObjectElement"):
return &HTMLObjectElement{BasicHTMLElement: el}
case js.Global.Get("HTMLOListElement"):
return &HTMLOListElement{BasicHTMLElement: el}
case js.Global.Get("HTMLOptGroupElement"):
return &HTMLOptGroupElement{BasicHTMLElement: el}
case js.Global.Get("HTMLOptionElement"):
return &HTMLOptionElement{BasicHTMLElement: el}
case js.Global.Get("HTMLOutputElement"):
return &HTMLOutputElement{BasicHTMLElement: el}
case js.Global.Get("HTMLParagraphElement"):
return &HTMLParagraphElement{BasicHTMLElement: el}
case js.Global.Get("HTMLParamElement"):
return &HTMLParamElement{BasicHTMLElement: el}
case js.Global.Get("HTMLPreElement"):
return &HTMLPreElement{BasicHTMLElement: el}
case js.Global.Get("HTMLProgressElement"):
return &HTMLProgressElement{BasicHTMLElement: el}
case js.Global.Get("HTMLQuoteElement"):
return &HTMLQuoteElement{BasicHTMLElement: el}
case js.Global.Get("HTMLScriptElement"):
return &HTMLScriptElement{BasicHTMLElement: el}
case js.Global.Get("HTMLSelectElement"):
return &HTMLSelectElement{BasicHTMLElement: el}
case js.Global.Get("HTMLSourceElement"):
return &HTMLSourceElement{BasicHTMLElement: el}
case js.Global.Get("HTMLSpanElement"):
return &HTMLSpanElement{BasicHTMLElement: el}
case js.Global.Get("HTMLStyleElement"):
return &HTMLStyleElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTableElement"):
return &HTMLTableElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTableCaptionElement"):
return &HTMLTableCaptionElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTableCellElement"):
return &HTMLTableCellElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTableDataCellElement"):
return &HTMLTableDataCellElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTableHeaderCellElement"):
return &HTMLTableHeaderCellElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTableColElement"):
return &HTMLTableColElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTableRowElement"):
return &HTMLTableRowElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTableSectionElement"):
return &HTMLTableSectionElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTemplateElement"):
return &HTMLTemplateElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTextAreaElement"):
return &HTMLTextAreaElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTimeElement"):
return &HTMLTimeElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTitleElement"):
return &HTMLTitleElement{BasicHTMLElement: el}
case js.Global.Get("HTMLTrackElement"):
return &HTMLTrackElement{BasicHTMLElement: el}
case js.Global.Get("HTMLUListElement"):
return &HTMLUListElement{BasicHTMLElement: el}
case js.Global.Get("HTMLUnknownElement"):
return &HTMLUnknownElement{BasicHTMLElement: el}
case js.Global.Get("HTMLVideoElement"):
return &HTMLVideoElement{HTMLMediaElement: &HTMLMediaElement{BasicHTMLElement: el}}
case js.Global.Get("HTMLElement"):
return el
default:
return el
}
}
func getForm(o *js.Object) *HTMLFormElement {
form := wrapHTMLElement(o.Get("form"))
if form == nil {
return nil
}
return form.(*HTMLFormElement)
}
func getLabels(o *js.Object) []*HTMLLabelElement {
labels := nodeListToElements(o.Get("labels"))
out := make([]*HTMLLabelElement, len(labels))
for i, label := range labels {
out[i] = label.(*HTMLLabelElement)
}
return out
}
func getOptions(o *js.Object, attr string) []*HTMLOptionElement {
options := nodeListToElements(o.Get(attr))
out := make([]*HTMLOptionElement, len(options))
for i, option := range options {
out[i] = option.(*HTMLOptionElement)
}
return out
}
func GetWindow() Window {
return &window{js.Global}
}
type TokenList struct {
dtl *js.Object // the underlying DOMTokenList
o *js.Object // the object to which the DOMTokenList belongs
sa string // the name of the corresponding string attribute, empty if there isn't one
Length int `js:"length"`
}
func (tl *TokenList) Item(idx int) string {
o := tl.dtl.Call("item", idx)
return toString(o)
}
func (tl *TokenList) Contains(token string) bool {
return tl.dtl.Call("contains", token).Bool()
}
func (tl *TokenList) Add(token string) {
tl.dtl.Call("add", token)
}
func (tl *TokenList) Remove(token string) {
tl.dtl.Call("remove", token)
}
func (tl *TokenList) Toggle(token string) {
tl.dtl.Call("toggle", token)
}
func (tl *TokenList) String() string {
if tl.sa != "" {
return tl.o.Get(tl.sa).String()
}
if tl.dtl.Get("constructor") == js.Global.Get("DOMSettableTokenList") {
return tl.dtl.Get("value").String()
}
// We could manually construct the string, but I am not aware of
// any case where we have neither a string attribute nor
// DOMSettableTokenList.
return ""
}
func (tl *TokenList) Slice() []string {
var out []string
length := tl.dtl.Get("length").Int()
for i := 0; i < length; i++ {
out = append(out, tl.dtl.Call("item", i).String())
}
return out
}
// SetString sets the TokenList's value to the space-separated list of
// tokens in s.
func (tl *TokenList) SetString(s string) {
if tl.sa != "" {
tl.o.Set(tl.sa, s)
return
}
if tl.dtl.Get("constructor") == js.Global.Get("DOMSettableTokenList") {
tl.dtl.Set("value", s)
return
}
// This shouldn't be possible
panic("no way to SetString on this TokenList")
}
// Set sets the TokenList's value to the list of tokens in s.
//
// Individual tokens in s shouldn't countain spaces.
func (tl *TokenList) Set(s []string) {
tl.SetString(strings.Join(s, " "))
}
type Document interface {
Node
ParentNode
Async() bool
SetAsync(bool)
Doctype() DocumentType
DocumentElement() Element
DocumentURI() string
Implementation() DOMImplementation
LastStyleSheetSet() string
PreferredStyleSheetSet() string // TODO correct type?
SelectedStyleSheetSet() string // TODO correct type?
StyleSheets() []StyleSheet // TODO s/StyleSheet/Stylesheet/ ?
StyleSheetSets() []StyleSheet // TODO correct type?
AdoptNode(node Node) Node
ImportNode(node Node, deep bool) Node
CreateElement(name string) Element
CreateElementNS(namespace, name string) Element
CreateTextNode(s string) *Text
ElementFromPoint(x, y int) Element
EnableStyleSheetsForSet(name string)
GetElementsByClassName(name string) []Element
GetElementsByTagName(name string) []Element
GetElementsByTagNameNS(ns, name string) []Element
GetElementByID(id string) Element
QuerySelector(sel string) Element
QuerySelectorAll(sel string) []Element
CreateDocumentFragment() DocumentFragment
}
type DocumentFragment interface {
Node
ParentNode
QuerySelector(sel string) Element
QuerySelectorAll(sel string) []Element
GetElementByID(id string) Element
}
type HTMLDocument interface {
Document
ActiveElement() HTMLElement
Body() HTMLElement
Cookie() string
SetCookie(string)
DefaultView() Window
DesignMode() bool
SetDesignMode(bool)
Domain() string
SetDomain(string)
Forms() []*HTMLFormElement
Head() *HTMLHeadElement
Images() []*HTMLImageElement
LastModified() time.Time
Links() []HTMLElement
Location() *Location
Plugins() []*HTMLEmbedElement
ReadyState() string
Referrer() string
Scripts() []*HTMLScriptElement
Title() string
SetTitle(string)
URL() string
// TODO HTMLDocument methods
}
type documentFragment struct {
*BasicNode
}
func (d documentFragment) GetElementByID(id string) Element {
return wrapElement(d.Call("getElementById", id))
}
func (d documentFragment) QuerySelector(sel string) Element {
return (&BasicElement{&BasicNode{d.Object}}).QuerySelector(sel)
}
func (d documentFragment) QuerySelectorAll(sel string) []Element {
return (&BasicElement{&BasicNode{d.Object}}).QuerySelectorAll(sel)
}
type document struct {
*BasicNode
}
type htmlDocument struct {
*document
}
func (d *htmlDocument) ActiveElement() HTMLElement {
return wrapHTMLElement(d.Get("activeElement"))
}
func (d *htmlDocument) Body() HTMLElement {
return wrapHTMLElement(d.Get("body"))
}
func (d *htmlDocument) Cookie() string {
return d.Get("cookie").String()
}
func (d *htmlDocument) SetCookie(s string) {
d.Set("cookie", s)
}
func (d *htmlDocument) DefaultView() Window {
return &window{d.Get("defaultView")}
}
func (d *htmlDocument) DesignMode() bool {
s := d.Get("designMode").String()
return s != "off"
}
func (d *htmlDocument) SetDesignMode(b bool) {
s := "off"
if b {
s = "on"
}
d.Set("designMode", s)
}
func (d *htmlDocument) Domain() string {
return d.Get("domain").String()
}
func (d *htmlDocument) SetDomain(s string) {
d.Set("domain", s)
}
func (d *htmlDocument) Forms() []*HTMLFormElement {
var els []*HTMLFormElement
forms := d.Get("forms")
length := forms.Get("length").Int()
for i := 0; i < length; i++ {
els = append(els, wrapHTMLElement(forms.Call("item", i)).(*HTMLFormElement))
}
return els
}
func (d *htmlDocument) Head() *HTMLHeadElement {
head := wrapElement(d.Get("head"))
if head == nil {
return nil
}
return head.(*HTMLHeadElement)
}
func (d *htmlDocument) Images() []*HTMLImageElement {
var els []*HTMLImageElement
images := d.Get("images")
length := images.Get("length").Int()
for i := 0; i < length; i++ {
els = append(els, wrapHTMLElement(images.Call("item", i)).(*HTMLImageElement))
}
return els
}
func (d *htmlDocument) LastModified() time.Time {
return d.Get("lastModified").Interface().(time.Time)
}
func (d *htmlDocument) Links() []HTMLElement {
var els []HTMLElement
links := d.Get("links")
length := links.Get("length").Int()
for i := 0; i < length; i++ {
els = append(els, wrapHTMLElement(links.Call("item", i)))
}
return els
}
func (d *htmlDocument) Location() *Location {
o := d.Get("location")
return &Location{Object: o, URLUtils: &URLUtils{Object: o}}
}
func (d *htmlDocument) Plugins() []*HTMLEmbedElement {
var els []*HTMLEmbedElement
forms := d.Get("plugins")
length := forms.Get("length").Int()
for i := 0; i < length; i++ {
els = append(els, wrapHTMLElement(forms.Call("item", i)).(*HTMLEmbedElement))
}
return els
}
func (d *htmlDocument) ReadyState() string {
return d.Get("readyState").String()
}
func (d *htmlDocument) Referrer() string {
return d.Get("referrer").String()
}
func (d *htmlDocument) Scripts() []*HTMLScriptElement {
var els []*HTMLScriptElement
forms := d.Get("scripts")
length := forms.Get("length").Int()
for i := 0; i < length; i++ {
els = append(els, wrapHTMLElement(forms.Call("item", i)).(*HTMLScriptElement))
}
return els
}
func (d *htmlDocument) Title() string {
return d.Get("title").String()
}
func (d *htmlDocument) SetTitle(s string) {
d.Set("title", s)
}
func (d *htmlDocument) URL() string {
return d.Get("URL").String()
}
func (d document) Async() bool {
return d.Get("async").Bool()
}
func (d document) SetAsync(b bool) {
d.Set("async", b)
}
func (d document) Doctype() DocumentType {
// FIXME implement
panic("not implemented")
}
func (d document) DocumentElement() Element {
return wrapElement(d.Get("documentElement"))
}
func (d document) DocumentURI() string {
return d.Get("documentURI").String()
}
func (d document) Implementation() DOMImplementation {
// FIXME implement
panic("not implemented")
}
func (d document) LastStyleSheetSet() string {
return d.Get("lastStyleSheetSet").String()
}
func (d document) PreferredStyleSheetSet() string {
return d.Get("preferredStyleSheetSet").String()
}
func (d document) SelectedStyleSheetSet() string {
return d.Get("selectedStyleSheetSet").String()
}
func (d document) StyleSheets() []StyleSheet {
// FIXME implement
panic("not implemented")
}
func (d document) StyleSheetSets() []StyleSheet {
// FIXME implement
panic("not implemented")
}
func (d document) AdoptNode(node Node) Node {
return wrapNode(d.Call("adoptNode", node.Underlying()))
}
func (d document) ImportNode(node Node, deep bool) Node {
return wrapNode(d.Call("importNode", node.Underlying(), deep))
}
func (d document) CreateDocumentFragment() DocumentFragment {
return wrapDocumentFragment(d.Call("createDocumentFragment"))
}
func (d document) CreateElement(name string) Element {
return wrapElement(d.Call("createElement", name))
}
func (d document) CreateElementNS(ns string, name string) Element {
return wrapElement(d.Call("createElementNS", ns, name))
}
func (d document) CreateTextNode(s string) *Text {
return wrapNode(d.Call("createTextNode", s)).(*Text)
}
func (d document) ElementFromPoint(x, y int) Element {
return wrapElement(d.Call("elementFromPoint", x, y))
}
func (d document) EnableStyleSheetsForSet(name string) {
d.Call("enableStyleSheetsForSet", name)
}
func (d document) GetElementsByClassName(name string) []Element {
return (&BasicElement{&BasicNode{d.Object}}).GetElementsByClassName(name)
}
func (d document) GetElementsByTagName(name string) []Element {
return (&BasicElement{&BasicNode{d.Object}}).GetElementsByTagName(name)
}
func (d document) GetElementsByTagNameNS(ns, name string) []Element {
return (&BasicElement{&BasicNode{d.Object}}).GetElementsByTagNameNS(ns, name)
}
func (d document) GetElementByID(id string) Element {
return wrapElement(d.Call("getElementById", id))
}
func (d document) QuerySelector(sel string) Element {
return (&BasicElement{&BasicNode{d.Object}}).QuerySelector(sel)
}
func (d document) QuerySelectorAll(sel string) []Element {
return (&BasicElement{&BasicNode{d.Object}}).QuerySelectorAll(sel)
}
type URLUtils struct {
*js.Object
Href string `js:"href"`
Protocol string `js:"protocol"`
Host string `js:"host"`
Hostname string `js:"hostname"`
Port string `js:"port"`
Pathname string `js:"pathname"`
Search string `js:"search"`
Hash string `js:"hash"`
Username string `js:"username"`
Password string `js:"password"`
Origin string `js:"origin"`
}
// TODO Location methods
type Location struct {
*js.Object
*URLUtils
}
type HTMLElement interface {
Element
GlobalEventHandlers
AccessKey() string
Dataset() map[string]string
SetAccessKey(string)
AccessKeyLabel() string
SetAccessKeyLabel(string)
ContentEditable() string
SetContentEditable(string)
IsContentEditable() bool
Dir() string
SetDir(string)
Draggable() bool
SetDraggable(bool)
Lang() string
SetLang(string)
OffsetHeight() float64
OffsetLeft() float64
OffsetParent() HTMLElement
OffsetTop() float64
OffsetWidth() float64
Style() *CSSStyleDeclaration
Title() string
SetTitle(string)
Blur()
Click()
Focus()
}
type SVGElement interface {
Element
// TODO
}
type GlobalEventHandlers interface{}
type Window interface {
EventTarget
Console() *Console
Document() Document
FrameElement() Element
Location() *Location
Name() string
SetName(string)
InnerHeight() int
InnerWidth() int
Length() int
Opener() Window
OuterHeight() int
OuterWidth() int
ScrollX() int
ScrollY() int
Parent() Window
ScreenX() int
ScreenY() int
ScrollMaxX() int
ScrollMaxY() int
Top() Window
History() History
Navigator() Navigator
Screen() *Screen
Alert(string)
Back()
Blur()
CancelAnimationFrame(int)
ClearInterval(int)
ClearTimeout(int)
Close()
Confirm(string) bool
Focus()
Forward()
GetComputedStyle(el Element, pseudoElt string) *CSSStyleDeclaration
GetSelection() Selection
Home()
MoveBy(dx, dy int)
MoveTo(x, y int)
Open(url, name, features string) Window
OpenDialog(url, name, features string, args []interface{}) Window
PostMessage(message string, target string, transfer []interface{})
Print()
Prompt(prompt string, initial string) string
RequestAnimationFrame(callback func(time.Duration)) int
ResizeBy(dw, dh int)
ResizeTo(w, h int)
Scroll(x, y int)
ScrollBy(dx, dy int)
ScrollByLines(int)
ScrollTo(x, y int)
SetCursor(name string)
SetInterval(fn func(), delay int) int
SetTimeout(fn func(), delay int) int
Stop()
// TODO constructors
}
type window struct {
// TODO EventTarget
*js.Object
}
func (w *window) Console() *Console {
return &Console{w.Get("console")}
}
func (w *window) Document() Document {
return wrapDocument(w.Get("document"))
}
func (w *window) FrameElement() Element {
return wrapElement(w.Get("frameElement"))
}
func (w *window) Location() *Location {
o := w.Get("location")
return &Location{Object: o, URLUtils: &URLUtils{Object: o}}
}
func (w *window) Name() string {
return w.Get("name").String()
}
func (w *window) SetName(s string) {
w.Set("name", s)
}
func (w *window) InnerHeight() int {
return w.Get("innerHeight").Int()
}
func (w *window) InnerWidth() int {
return w.Get("innerWidth").Int()
}
func (w *window) Length() int {
return w.Get("length").Int()
}