-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvnode.go
More file actions
60 lines (50 loc) · 1.43 KB
/
vnode.go
File metadata and controls
60 lines (50 loc) · 1.43 KB
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
// Package gox provides JSX-like syntax for Go and the core types for virtual DOM trees.
package gox
// VNode is the core tree node type.
type VNode struct {
Type any // string for intrinsic elements, Component for components
Props Props
Children []VNode
}
// Props is a flexible property map.
type Props map[string]any
// Component is a function that returns a VNode.
type Component func(props Props) VNode
// NodeType constants for special node types.
const (
TextNodeType = "__text__"
FragmentNodeType = "__fragment__"
)
// IsText returns true if this VNode is a text node.
func (v VNode) IsText() bool {
s, ok := v.Type.(string)
return ok && s == TextNodeType
}
// IsFragment returns true if this VNode is a fragment.
func (v VNode) IsFragment() bool {
s, ok := v.Type.(string)
return ok && s == FragmentNodeType
}
// IsComponent returns true if this VNode represents a component.
func (v VNode) IsComponent() bool {
_, ok := v.Type.(Component)
return ok
}
// GetTextContent returns the text content if this is a text node.
func (v VNode) GetTextContent() (string, bool) {
if !v.IsText() {
return "", false
}
if content, ok := v.Props["content"].(string); ok {
return content, true
}
return "", false
}
// Empty returns an empty VNode.
func Empty() VNode {
return VNode{}
}
// IsEmpty returns true if this VNode is empty/nil.
func (v VNode) IsEmpty() bool {
return v.Type == nil && v.Props == nil && v.Children == nil
}