-
Notifications
You must be signed in to change notification settings - Fork 58
/
node.go
133 lines (104 loc) · 2.72 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
package dom
import "github.com/dennwc/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 NodeType int
//See https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#Node_type_constants
const (
ElementNode NodeType = 1
TextNode NodeType = 3
CDataSectionNode NodeType = 4
ProcessingInstructionNode NodeType = 7
CommentNode NodeType = 8
DocumentNode NodeType = 9
DocumentTypeNode NodeType = 10
DocumentFragmentNode NodeType = 11
)
var _ js.Wrapper = NodeBase{}
type NodeList []*Element
type NodeBase struct {
v js.Value
funcs []js.Func
}
// JSValue implements js.Wrapper.
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) AddEventListener(typ string, h EventHandler) {
cb := js.NewEventCallback(func(v js.Value) {
h(convertEvent(v))
})
e.funcs = append(e.funcs, cb)
e.v.Call("addEventListener", typ, cb)
}
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) NodeType() NodeType {
return NodeType(e.v.Get("nodeType").Int())
}
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))
}