-
Notifications
You must be signed in to change notification settings - Fork 6
/
vnode.go
executable file
·117 lines (91 loc) · 2.14 KB
/
vnode.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
package vn
import (
"fmt"
vnd "github.com/mfrachet/go-vdom-wasm/dom"
vnh "github.com/mfrachet/go-vdom-wasm/helpers"
)
type Children []Node
type Node interface {
GetElement() *vnd.DomNode
GetTagName() string
GetAttrs() *Attrs
GetKey() *string
HasElement() bool
SetElement(vnd.DomNode)
HashCode() string
ChildrenCount() int
ChildAt(int) Node
GetText() *TextNode
GetChildren() Children
IsSame(Node) bool
}
type Vnode struct {
tagname string
attrs *Attrs
children Children
text *TextNode
element *vnd.DomNode
key KeyIdentifier
}
func NewNode(tagname string, attrs *Attrs, children Children, text *TextNode, element *vnd.DomNode, key KeyIdentifier) Node {
return &Vnode{tagname, attrs, children, text, element, key}
}
func (vnode *Vnode) IsSame(other Node) bool {
currKey := vnode.GetKey()
otherKey := other.GetKey()
if currKey != nil && otherKey != nil {
return *currKey == *otherKey
}
if vnh.IsNil(vnode.text) {
if vnh.IsNil(other.GetText()) {
return vnode.HashCode() == other.HashCode()
}
return false
}
if vnh.NotNil(other.GetText()) {
hasSameText := other.GetText().IsSame(vnode.text)
return hasSameText && vnode.HashCode() == other.HashCode()
}
return false
}
func (vnode *Vnode) ChildrenCount() int {
return len(vnode.children)
}
func (vnode *Vnode) ChildAt(index int) Node {
size := len(vnode.children)
if size > index {
return vnode.children[index]
}
return nil
}
func (vnode *Vnode) GetKey() *string {
if vnh.IsNil(vnode.key) {
return nil
}
key := vnode.key.GetValue()
return &key
}
func (vnode *Vnode) GetText() *TextNode {
return vnode.text
}
func (vnode *Vnode) GetElement() *vnd.DomNode {
return vnode.element
}
func (vnode *Vnode) HasElement() bool {
return vnh.NotNil(vnode.GetElement())
}
func (vnode *Vnode) GetTagName() string {
return vnode.tagname
}
func (vnode *Vnode) GetAttrs() *Attrs {
return vnode.attrs
}
func (vnode *Vnode) GetChildren() Children {
return vnode.children
}
func (vnode *Vnode) SetElement(element vnd.DomNode) {
vnode.element = &element
}
func (vnode *Vnode) HashCode() string {
return fmt.Sprintf("%s/%v", vnode.tagname, *vnode.attrs.Props)
}