-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.go
279 lines (254 loc) · 4.63 KB
/
element.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
package nodes
import (
"fmt"
"io"
"strings"
"golazy.dev/lazysupport"
)
var Beautify = true
type Element struct {
tag string
children []io.WriterTo
attributes []Attr // Attr List of element attributes
}
// NewElement creates a new element with the provided tagname and the provided options
// The options can be:
//
// * An Attr that will be render
// * A string or Text
// * Another Element
// * Any WriterTo interface
// Attributes are output in order
// The rest is output in the same order as received
func NewElement(tagname string, options ...any) Element {
r := Element{
tag: tagname,
}
r.add(options)
return r
}
// WriteTo writes the current string to the writer w
func (r Element) WriteTo(w io.Writer) (n64 int64, err error) {
var session *writeSession
if s, ok := w.(*writeSession); ok {
session = s
} else {
session = &writeSession{Writer: w, level: 0}
}
r.writeOpenTag(session)
if voidElements.Has(r.tag) {
return session.n, session.err
}
// Content
isInline := r.isInline()
if !isInline {
session.level = session.level + 1
}
for _, c := range r.children {
if r.tag == "html" {
session.NewLine()
c.WriteTo(session)
continue
}
if !isInline {
session.NewLine()
}
c.WriteTo(session)
}
if !isInline {
session.NewLine()
session.level = session.level - 1
}
// Some elements
if skipCloseTag.Has(r.tag) {
//session.WriteS("\n")
return session.n, session.err
}
// Close tag
session.WriteS("</" + r.tag + ">")
return session.n, session.err
}
func (r Element) String() string {
buf := &strings.Builder{}
r.WriteTo(buf)
return buf.String()
}
func (r *Element) add(something any) {
switch v := something.(type) {
case Element:
r.children = append(r.children, v)
case Raw:
r.children = append(r.children, v)
case Attr:
r.attributes = append(r.attributes, v)
case []Attr:
r.attributes = append(r.attributes, v...)
case []Element:
for _, arg := range v {
r.add(arg)
}
case io.WriterTo:
r.children = append(r.children, v)
case []io.WriterTo:
for _, arg := range v {
r.add(arg)
}
case []any:
for _, arg := range v {
r.add(arg)
}
case int64, int32, int16, int8, int:
r.children = append(r.children, Text(fmt.Sprintf("%d", v)))
case uint64, uint32, uint16, uint8, uint:
r.children = append(r.children, Text(fmt.Sprintf("%d", v)))
case []byte:
r.children = append(r.children, Raw(v))
case string:
r.children = append(r.children, Text(v))
case bool:
r.children = append(r.children, Text(fmt.Sprint(v)))
case nil:
default:
panic(fmt.Errorf("when processing elements of a view, found an unexpected type: %T inside %v", v, r.tag))
}
}
func (r Element) isInline() bool {
for _, e := range r.children {
switch child := e.(type) {
case Element:
if !child.isInline() {
return false
}
case Text:
default:
return false
}
}
return inlineElements.Has(r.tag)
}
// Rule to render a the content of a tag inline
// The tag is title, p, b, strong, i, em,li or there are no Elements inside
func (r Element) writeOpenTag(session *writeSession) {
if Beautify {
for i := 0; i < session.level; i++ {
session.WriteS(" ")
}
}
if r.tag == "html" {
session.WriteS("<!DOCTYPE html>")
session.NewLine()
}
// Open tag
session.WriteS("<" + r.tag)
// Process atributes
for _, attr := range r.attributes {
session.WriteS(" ")
attr.WriteTo(session)
}
session.WriteS(">")
}
// voidElements don't require a closing tag neither need to be self close
var voidElements = lazysupport.NewSet(
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr",
)
// html elements that don't require a closing tag
var skipCloseTag = lazysupport.NewSet(
"html",
"head",
"body",
// "p",
"li",
"dt",
"dd",
// "option",
"thead",
"th",
"tbody",
"tr",
"td",
"tfoot",
"colgroup",
)
// https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements
var inlineElements = lazysupport.NewSet(
"a",
"abbr",
"acronym",
"audio",
"b",
"bdi",
"bdo",
"big",
"br",
"button",
"canvas",
"cite",
"code",
"data",
"datalist",
"del",
"dfn",
"em",
"embed",
"i",
"iframe",
"img",
"input",
"ins",
"kbd",
"label",
"map",
"mark",
"meter",
"noscript",
"object",
"output",
"picture",
"progress",
"q",
"ruby",
"s",
"samp",
"script",
"select",
"slot",
"small",
"span",
"strong",
"sub",
"sup",
"svg",
"template",
"textarea",
"time",
"u",
"tt",
"var",
"video",
"wbr",
// Plus some that are not styled like the ones in head
"title",
"meta",
// Plus some that are rendered as block by usually formated as onelines
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"h7",
)