This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
forked from dennwc/dom
-
Notifications
You must be signed in to change notification settings - Fork 4
/
node.go
164 lines (127 loc) · 3.5 KB
/
node.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
package dom
import "github.com/gascore/dom/js"
type Node interface {
EventTarget
// properties
BaseURI() string
NodeName() string
ChildNodes() NodeList
ParentNode() Node
ParentElement() *Element
TextContent() string
SetTextContent(s string)
// methods
AppendChild(n Node)
Contains(n Node) bool
IsEqualNode(n Node) bool
IsSameNode(n Node) bool
RemoveChild(n Node) Node
ReplaceChild(n, old Node) Node
}
type NodeList []*Element
type NodeBase struct {
v js.Value
funcs []js.Func
}
func (e *NodeBase) JSValue() js.Ref {
return e.v.JSValue()
}
func (e *NodeBase) Remove() {
e.ParentNode().RemoveChild(e)
for _, c := range e.funcs {
c.Release()
}
e.funcs = nil
}
func (e *NodeBase) RemoveCustomEventListener(typ string, h EventHandler, values js.Value) {
cb := js.NewEventCallback(func(v js.Value) {
h(ConvertEvent(v))
})
e.v.Call("removeEventListener", typ, cb, values)
}
func (e *NodeBase) RemoveEventListener(typ string, h EventHandler) {
e.RemoveCustomEventListener(typ, h, js.NewObject())
}
func (e *NodeBase) RemovePassiveEventListener(typ string, h EventHandler) {
obj := js.NewObject()
obj.Set("passive", true)
obj.Set("capture", false)
e.RemoveCustomEventListener(typ, h, obj)
}
func (e *NodeBase) AddCustomEventListener(typ string, h EventHandler, values js.Value) {
cb := js.NewEventCallback(func(v js.Value) {
h(ConvertEvent(v))
})
e.funcs = append(e.funcs, cb)
e.v.Call("addEventListener", typ, cb, values)
}
func (e *NodeBase) AddEventListener(typ string, h EventHandler) {
e.AddCustomEventListener(typ, h, js.NewObject())
}
func (e *NodeBase) AddPassiveEventListener(typ string, h EventHandler) {
obj := js.NewObject()
obj.Set("passive", true)
obj.Set("capture", false)
e.AddCustomEventListener(typ, h, obj)
}
func (e *NodeBase) AddErrorListener(h func(err error)) {
e.AddEventListener("error", func(e Event) {
ConsoleLog(e.JSValue())
h(js.Error{Value: e.JSValue()})
})
}
func (e *NodeBase) BaseURI() string {
return e.v.Get("baseURI").String()
}
func (e *NodeBase) NodeName() string {
return e.v.Get("nodeName").String()
}
func (e *NodeBase) ChildNodes() NodeList {
return AsNodeList(e.v.Get("childNodes"))
}
func (e *NodeBase) ParentNode() Node {
return AsElement(e.v.Get("parentNode"))
}
func (e *NodeBase) ParentElement() *Element {
return AsElement(e.v.Get("parentElement"))
}
func (e *NodeBase) TextContent() string {
return e.v.Get("textContent").String()
}
func (e *NodeBase) SetTextContent(s string) {
e.v.Set("textContent", s)
}
func (e *NodeBase) AppendChild(n Node) {
e.v.Call("appendChild", n)
}
func (e *NodeBase) Contains(n Node) bool {
return e.v.Call("contains", n).Bool()
}
func (e *NodeBase) IsEqualNode(n Node) bool {
return e.v.Call("isEqualNode", n).Bool()
}
func (e *NodeBase) IsSameNode(n Node) bool {
return e.v.Call("isSameNode", n).Bool()
}
func (e *NodeBase) RemoveChild(n Node) Node {
return AsElement(e.v.Call("removeChild", n))
}
func (e *NodeBase) ReplaceChild(n, old Node) Node {
return AsElement(e.v.Call("replaceChild", n, old))
}
func (e *NodeBase) InsertBefore(a, b Node) {
e.JSValue().Call("insertBefore", a.JSValue(), b.JSValue())
}
func (e *NodeBase) InsertAfter(a, b Node) {
e.JSValue().Call("insertBefore", a.JSValue(), b.JSValue().Get("nextSibling"))
}
func (e *NodeBase) Clone() Node {
v := js.Value{Ref: e.JSValue().Call("cloneNode", true)}
return AsElement(v)
}
func (e *NodeBase) Value() string {
return e.JSValue().Get("value").String()
}
func (e *NodeBase) SetValue(a interface{}) {
e.JSValue().Set("value", a)
}