forked from chasefleming/elem-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elem.go
181 lines (153 loc) · 4.15 KB
/
elem.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
package elem
import (
"sort"
"strings"
"github.com/chasefleming/elem-go/attrs"
)
// List of HTML5 void elements. Void elements, also known as self-closing or empty elements,
// are elements that don't have a closing tag because they can't contain any content.
// For example, the <img> tag cannot wrap text or other tags, it stands alone, so it doesn't have a closing tag.
var voidElements = map[string]struct{}{
"area": {},
"base": {},
"br": {},
"col": {},
"command": {},
"embed": {},
"hr": {},
"img": {},
"input": {},
"keygen": {},
"link": {},
"meta": {},
"param": {},
"source": {},
"track": {},
"wbr": {},
}
// List of boolean attributes. Boolean attributes can't have literal values. The presence of an boolean
// attribute represents the "true" value. To represent the "false" value, the attribute has to be omitted.
// See https://html.spec.whatwg.org/multipage/indices.html#attributes-3 for reference
var booleanAttrs = map[string]struct{}{
attrs.AllowFullscreen: {},
attrs.Async: {},
attrs.Autofocus: {},
attrs.Autoplay: {},
attrs.Checked: {},
attrs.Controls: {},
attrs.Defer: {},
attrs.Disabled: {},
attrs.Ismap: {},
attrs.Loop: {},
attrs.Multiple: {},
attrs.Muted: {},
attrs.Novalidate: {},
attrs.Open: {},
attrs.Playsinline: {},
attrs.Readonly: {},
attrs.Required: {},
attrs.Selected: {},
}
type Node interface {
RenderTo(builder *strings.Builder)
Render() string
}
// NoneNode represents a node that renders nothing.
type NoneNode struct{}
// RenderTo for NoneNode does nothing.
func (n NoneNode) RenderTo(builder *strings.Builder) {
// Intentionally left blank to render nothing
}
// Render for NoneNode returns an empty string.
func (n NoneNode) Render() string {
return ""
}
type TextNode string
func (t TextNode) RenderTo(builder *strings.Builder) {
builder.WriteString(string(t))
}
func (t TextNode) Render() string {
return string(t)
}
type RawNode string
func (r RawNode) RenderTo(builder *strings.Builder) {
builder.WriteString(string(r))
}
func (r RawNode) Render() string {
return string(r)
}
type CommentNode string
func (c CommentNode) RenderTo(builder *strings.Builder) {
builder.WriteString("<!-- ")
builder.WriteString(string(c))
builder.WriteString(" -->")
}
func (c CommentNode) Render() string {
var builder strings.Builder
c.RenderTo(&builder)
return builder.String()
}
type Element struct {
Tag string
Attrs attrs.Props
Children []Node
}
func (e *Element) RenderTo(builder *strings.Builder) {
// Start with opening tag
builder.WriteString("<")
builder.WriteString(e.Tag)
// Sort the keys for consistent order
keys := make([]string, 0, len(e.Attrs))
for k := range e.Attrs {
keys = append(keys, k)
}
sort.Strings(keys)
// Append the attributes to the builder
for _, k := range keys {
e.renderAttrTo(k, builder)
}
// If it's a void element, close it and return
if _, exists := voidElements[e.Tag]; exists {
builder.WriteString(`>`)
return
}
// Close opening tag
builder.WriteString(`>`)
// Build the content
for _, child := range e.Children {
child.RenderTo(builder)
}
// Append closing tag
builder.WriteString(`</`)
builder.WriteString(e.Tag)
builder.WriteString(`>`)
}
// return string representation of given attribute with its value
func (e *Element) renderAttrTo(attrName string, builder *strings.Builder) {
if _, exists := booleanAttrs[attrName]; exists {
// boolean attribute presents its name only if the value is "true"
if e.Attrs[attrName] == "true" {
builder.WriteString(` `)
builder.WriteString(attrName)
}
} else {
// regular attribute has a name and a value
builder.WriteString(` `)
builder.WriteString(attrName)
builder.WriteString(`="`)
builder.WriteString(e.Attrs[attrName])
builder.WriteString(`"`)
}
}
func (e *Element) Render() string {
var builder strings.Builder
e.RenderTo(&builder)
return builder.String()
}
func NewElement(tag string, attrs attrs.Props, children ...Node) *Element {
return &Element{
Tag: tag,
Attrs: attrs,
Children: children,
}
}