-
Notifications
You must be signed in to change notification settings - Fork 175
/
vgnode.go
376 lines (337 loc) · 11.8 KB
/
vgnode.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
package vugu
import (
"fmt"
"html"
"reflect"
"strconv"
"github.com/vugu/vugu/js"
)
// VGNodeType is one of the valid node types (error, text, document, element, comment, doctype).
// Note that only text, element and comment are currently used.
type VGNodeType uint32
// Available VGNodeTypes.
const (
ErrorNode = VGNodeType(0 /*html.ErrorNode*/)
TextNode = VGNodeType(1 /*html.TextNode*/)
DocumentNode = VGNodeType(2 /*html.DocumentNode*/)
ElementNode = VGNodeType(3 /*html.ElementNode*/)
CommentNode = VGNodeType(4 /*html.CommentNode*/)
DoctypeNode = VGNodeType(5 /*html.DoctypeNode*/)
)
// VGAtom is an integer corresponding to golang.org/x/net/html/atom.Atom.
// Note that this may be removed for simplicity and to remove the dependency
// on the package above. Suggest you don't use it.
// type VGAtom uint32
// VGAttribute is the attribute on an HTML tag.
type VGAttribute struct {
Namespace, Key, Val string
}
// VGProperty is a JS property to be set on a DOM element.
type VGProperty struct {
Key string
JSONVal []byte // value as JSON expression
}
// VGNode represents a node from our virtual DOM with the dynamic parts wired up into functions.
//
// For the static parts, an instance of VGNode corresponds directly to the DOM representation of
// an HTML node. The pointers to other VGNode instances (Parent, FirstChild, etc.) are used to manage the tree.
// Type, Data, Namespace and Attr have the usual meanings for nodes.
//
// The Component field, if not-nil indicates that rendering should be delegated to the specified component,
// all other fields are ignored.
//
// Another special case is when Type is ElementNode and Data is an empty string (and Component is nil) then this node is a "template"
// (i.e. <vg-template>) and its children will be "flattened" into the DOM in position of this element
// and attributes, events, etc. ignored.
//
// Prop contains JavaScript property values to be assigned during render. InnerHTML provides alternate
// HTML content instead of children. DOMEventHandlerSpecList specifies DOM handlers to register.
// And the JS...Handler fields are used to register callbacks to obtain information at JS render-time.
//
// TODO: This and its related parts should probably move into a sub-package (vgnode?) and
// the "VG" prefixes removed.
type VGNode struct {
Parent, FirstChild, LastChild, PrevSibling, NextSibling *VGNode
Type VGNodeType
// DataAtom VGAtom // this needed to come out, we're not using it and without well-defined behavior it just becomes confusing and problematic
Data string
Namespace string
Attr []VGAttribute
// JS properties to e set during render
Prop []VGProperty
// Props Props // dynamic attributes, used as input for components or converted to attributes for regular HTML elements
InnerHTML *string // indicates that children should be ignored and this raw HTML is the children of this tag; nil means not set, empty string means explicitly set to empty string
DOMEventHandlerSpecList []DOMEventHandlerSpec // describes invocations when DOM events happen
// indicates this node's output should be delegated to the specified component
Component interface{}
// if not-nil, called when element is created (but before examining child nodes)
JSCreateHandler JSValueHandler
// if not-nil, called after children have been visited
JSPopulateHandler JSValueHandler
}
// IsComponent returns true if this is a component (Component != nil).
// Components have rendering delegated to them instead of processing this node.
func (n *VGNode) IsComponent() bool {
return n.Component != nil
}
// IsTemplate returns true if this is a template (Type is ElementNode and Data is an empty string and not a Component).
// Templates have their children flattened into the output DOM instead of being processed directly.
func (n *VGNode) IsTemplate() bool {
if n.Type == ElementNode && n.Data == "" && n.Component == nil {
return true
}
return false
}
// InsertBefore inserts newChild as a child of n, immediately before oldChild
// in the sequence of n's children. oldChild may be nil, in which case newChild
// is appended to the end of n's children.
//
// It will panic if newChild already has a parent or siblings.
func (n *VGNode) InsertBefore(newChild, oldChild *VGNode) {
if newChild.Parent != nil || newChild.PrevSibling != nil || newChild.NextSibling != nil {
panic("html: InsertBefore called for an attached child Node")
}
var prev, next *VGNode
if oldChild != nil {
prev, next = oldChild.PrevSibling, oldChild
} else {
prev = n.LastChild
}
if prev != nil {
prev.NextSibling = newChild
} else {
n.FirstChild = newChild
}
if next != nil {
next.PrevSibling = newChild
} else {
n.LastChild = newChild
}
newChild.Parent = n
newChild.PrevSibling = prev
newChild.NextSibling = next
}
// AppendChild adds a node c as a child of n.
//
// It will panic if c already has a parent or siblings.
func (n *VGNode) AppendChild(c *VGNode) {
if c.Parent != nil || c.PrevSibling != nil || c.NextSibling != nil {
panic("html: AppendChild called for an attached child Node")
}
last := n.LastChild
if last != nil {
last.NextSibling = c
} else {
n.FirstChild = c
}
n.LastChild = c
c.Parent = n
c.PrevSibling = last
}
// RemoveChild removes a node c that is a child of n. Afterwards, c will have
// no parent and no siblings.
//
// It will panic if c's parent is not n.
func (n *VGNode) RemoveChild(c *VGNode) {
if c.Parent != n {
panic("html: RemoveChild called for a non-child Node")
}
if n.FirstChild == c {
n.FirstChild = c.NextSibling
}
if c.NextSibling != nil {
c.NextSibling.PrevSibling = c.PrevSibling
}
if n.LastChild == c {
n.LastChild = c.PrevSibling
}
if c.PrevSibling != nil {
c.PrevSibling.NextSibling = c.NextSibling
}
c.Parent = nil
c.PrevSibling = nil
c.NextSibling = nil
}
// type IfFunc func(data interface{}) (bool, error)
// Walk will walk the tree under a VGNode using the specified callback function.
// If the function provided returns a non-nil error then walking will be stopped
// and this error will be returned. Only FirstChild and NextSibling are used
// while walking and so with well-formed documents should not loop. (But loops
// created manually by modifying FirstChild or NextSibling pointers could cause
// this function to recurse indefinitely.) Note that f may modify nodes as it
// visits them with predictable results as long as it does not modify elements
// higher on the tree (up, toward the parent); it is safe to modify self and children.
func (n *VGNode) Walk(f func(*VGNode) error) error {
if n == nil {
return nil
}
err := f(n)
if err != nil {
return err
}
err = n.FirstChild.Walk(f)
if err != nil {
return err
}
err = n.NextSibling.Walk(f)
if err != nil {
return err
}
return nil
}
// AddAttrInterface sets an attribute based on the given interface. The followings types are supported
// - string - value is used as attr value as it is
// - int,float,... - the value is converted to string with strconv and used as attr value
// - bool - treat the attribute as a flag. If false, the attribute will be ignored, if true outputs the attribute without a value
// - fmt.Stringer - if the value implements fmt.Stringer, the returned string of StringVar() is used
// - ptr - If the ptr is nil, the attribute will be ignored. Else, the rules above apply
// any other type is handled via fmt.Sprintf()
func (n *VGNode) AddAttrInterface(key string, val interface{}) {
// ignore nil attributes
if val == nil {
return
}
nattr := VGAttribute{
Key: key,
}
switch v := val.(type) {
case string:
nattr.Val = v
case int:
nattr.Val = strconv.Itoa(v)
case int8:
nattr.Val = strconv.Itoa(int(v))
case int16:
nattr.Val = strconv.Itoa(int(v))
case int32:
nattr.Val = strconv.Itoa(int(v))
case int64:
nattr.Val = strconv.FormatInt(v, 10)
case uint:
nattr.Val = strconv.FormatUint(uint64(v), 10)
case uint8:
nattr.Val = strconv.FormatUint(uint64(v), 10)
case uint16:
nattr.Val = strconv.FormatUint(uint64(v), 10)
case uint32:
nattr.Val = strconv.FormatUint(uint64(v), 10)
case uint64:
nattr.Val = strconv.FormatUint(v, 10)
case float32:
nattr.Val = strconv.FormatFloat(float64(v), 'f', 6, 32)
case float64:
nattr.Val = strconv.FormatFloat(v, 'f', 6, 64)
case bool:
if !v {
return
}
// FIXME: according to the HTML spec this is valid for flags, but not pretty
// To change this, we have to adopt the changes inside the domrender and staticrender too.
nattr.Val = key
case fmt.Stringer:
// we have to check that the given interface does not hide a nil ptr
if p := reflect.ValueOf(val); p.Kind() == reflect.Ptr && p.IsNil() {
return
}
nattr.Val = v.String()
default:
// check if this is a ptr
rv := reflect.ValueOf(val)
if rv.Kind() == reflect.Ptr {
// indirect the ptr and recurse
if !rvIsZero(rv) {
n.AddAttrInterface(key, reflect.Indirect(rv).Interface())
}
return
}
// fall back to fmt
nattr.Val = fmt.Sprintf("%v", val)
}
n.Attr = append(n.Attr, nattr)
}
// AddAttrList takes a VGAttributeLister and sets the returned attributes to the node
func (n *VGNode) AddAttrList(lister VGAttributeLister) {
n.Attr = append(n.Attr, lister.AttributeList()...)
}
// SetInnerHTML assigns the InnerHTML field with useful logic based on the type of input.
// Values of string type are escaped using html.EscapeString(). Values in the
// int or float type families or bool are converted to a string using the strconv package
// (no escaping is required). Nil values set InnerHTML to nil. Non-nil pointers
// are followed and the same rules applied. Values implementing HTMLer will have their
// HTML() method called and the result put into InnerHTML without escaping.
// Values implementing fmt.Stringer have thier String() method called and the escaped result
// used as in string above.
//
// All other values have undefined behavior but are currently handled by setting InnerHTML
// to the result of: `html.EscapeString(fmt.Sprintf("%v", val))`
func (n *VGNode) SetInnerHTML(val interface{}) {
var s string
if val == nil {
n.InnerHTML = nil
return
}
switch v := val.(type) {
case string:
s = html.EscapeString(v)
case int:
s = strconv.Itoa(v)
case int8:
s = strconv.Itoa(int(v))
case int16:
s = strconv.Itoa(int(v))
case int32:
s = strconv.Itoa(int(v))
case int64:
s = strconv.FormatInt(v, 10)
case uint:
s = strconv.FormatUint(uint64(v), 10)
case uint8:
s = strconv.FormatUint(uint64(v), 10)
case uint16:
s = strconv.FormatUint(uint64(v), 10)
case uint32:
s = strconv.FormatUint(uint64(v), 10)
case uint64:
s = strconv.FormatUint(v, 10)
case float32:
s = strconv.FormatFloat(float64(v), 'f', 6, 32)
case float64:
s = strconv.FormatFloat(v, 'f', 6, 64)
case bool:
// I don't see any use in making false result in no output in this case.
// I'm guessing if someone puts a bool in vg-content/vg-html
// they are expecting it to print out the text "true" or "false".
// It's different from AddAttrInterface but I think appropriate here.
s = strconv.FormatBool(v)
case HTMLer:
s = v.HTML()
case fmt.Stringer:
// we have to check that the given interface does not hide a nil ptr
if p := reflect.ValueOf(val); p.Kind() == reflect.Ptr && p.IsNil() {
n.InnerHTML = nil
return
}
s = html.EscapeString(v.String())
default:
// check if this is a ptr
rv := reflect.ValueOf(val)
if rv.Kind() == reflect.Ptr {
// indirect the ptr and recurse
if !rvIsZero(rv) {
n.SetInnerHTML(reflect.Indirect(rv).Interface())
}
return
}
// fall back to fmt
s = html.EscapeString(fmt.Sprintf("%v", val))
}
n.InnerHTML = &s
}
// JSValueHandler does something with a js.Value
type JSValueHandler interface {
JSValueHandle(js.Value)
}
// JSValueFunc implements JSValueHandler as a function.
type JSValueFunc func(js.Value)
// JSValueHandle implements the JSValueHandler interface.
func (f JSValueFunc) JSValueHandle(v js.Value) { f(v) }