-
Notifications
You must be signed in to change notification settings - Fork 1
/
comps.go
396 lines (311 loc) · 7.62 KB
/
comps.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package htmx
import (
"bytes"
"fmt"
"html/template"
"io"
"strings"
"github.com/go-playground/validator/v10"
"github.com/yuin/goldmark"
"github.com/zeiss/pkg/errorx"
)
// Node is a node in the HTML tree.
type Node interface {
Render(w io.Writer) error
}
// Nodes is a slice of nodes.
type Nodes []Node
// NodeType is the type of a node.
type NodeType int
const (
ElementType = NodeType(iota)
AttributeType
)
// NodeTypeDescriptor is a node that has a type.
type NodeTypeDescriptor interface {
Type() NodeType
}
// NodeFunc is a function that renders a node.
type NodeFunc func(io.Writer) error
// Render renders the node.
func (n NodeFunc) Render(w io.Writer) error {
return n(w)
}
// Type returns the node type.
func (n NodeFunc) Type() NodeType {
return ElementType
}
// String returns the node as a string.
func (n NodeFunc) String() string {
var b strings.Builder
_ = n.Render(&b)
return b.String()
}
// Element is a node that renders an HTML element.
func Element(name string, children ...Node) NodeFunc {
return NodeFunc(func(w2 io.Writer) error {
w := &statefulWriter{w: w2}
w.Write([]byte("<" + name))
for _, c := range children {
renderChild(w, c, AttributeType)
}
w.Write([]byte(">"))
if isVoidElement(name) {
return w.err
}
for _, c := range children {
renderChild(w, c, ElementType)
}
w.Write([]byte("</" + name + ">"))
return w.err
})
}
//nolint:gocyclo
func renderChild(w *statefulWriter, c Node, t NodeType) {
if w.err != nil || c == nil {
return
}
if g, ok := c.(group); ok {
for _, groupC := range g.children {
renderChild(w, groupC, t)
}
return
}
switch t {
case ElementType:
if p, ok := c.(NodeTypeDescriptor); !ok || p.Type() == ElementType {
w.err = c.Render(w.w)
}
case AttributeType:
if p, ok := c.(NodeTypeDescriptor); ok && p.Type() == AttributeType {
w.err = c.Render(w.w)
}
}
}
type statefulWriter struct {
w io.Writer
err error
}
// Write is a node that writes to the stateful writer.
func (w *statefulWriter) Write(p []byte) {
if w.err != nil {
return
}
_, w.err = w.w.Write(p)
}
var voidElements = map[string]struct{}{
"area": {},
"base": {},
"br": {},
"col": {},
"command": {},
"embed": {},
"hr": {},
"img": {},
"input": {},
"keygen": {},
"link": {},
"meta": {},
"param": {},
"source": {},
"track": {},
"wbr": {},
}
func isVoidElement(name string) bool {
_, ok := voidElements[name]
return ok
}
// Attribute is a node that renders an HTML attribute.
func Attribute(name string, value ...string) Node {
switch len(value) {
case 0:
return &attr{name: name}
case 1:
return &attr{name: name, value: &value[0]}
default:
panic("attribute must be just name or name and value pair")
}
}
type attr struct {
name string
value *string
}
// Render is a node that renders an attribute.
func (a *attr) Render(w io.Writer) error {
if a.value == nil {
_, err := w.Write([]byte(" " + a.name))
return err
}
_, err := w.Write([]byte(" " + a.name + `="` + template.HTMLEscapeString(*a.value) + `"`))
return err
}
// Type is a node that returns the type of an attribute.
func (a *attr) Type() NodeType {
return AttributeType
}
// String is a node that returns the attribute as a string.
func (a *attr) String() string {
var b strings.Builder
_ = a.Render(&b)
return b.String()
}
// Text is a node that renders a text.
func Text(t string) Node {
return NodeFunc(func(w io.Writer) error {
_, err := w.Write([]byte(template.HTMLEscapeString(t)))
return err
})
}
// Textf is a node that renders a formatted text.
func Textf(format string, a ...interface{}) Node {
return NodeFunc(func(w io.Writer) error {
_, err := w.Write([]byte(template.HTMLEscapeString(fmt.Sprintf(format, a...))))
return err
})
}
// Raw is a node that renders raw HTML.
func Raw(t string) Node {
return NodeFunc(func(w io.Writer) error {
_, err := w.Write([]byte(t))
return err
})
}
// Rawf is a node that renders a formatted raw HTML.
func Rawf(format string, a ...interface{}) Node {
return NodeFunc(func(w io.Writer) error {
_, err := fmt.Fprintf(w, format, a...)
return err
})
}
type group struct {
children []Node
}
// String is a node that renders a group of nodes.
func (c group) String() string {
panic("cannot render group directly")
}
// Render is a node that renders a group of nodes.
func (c group) Render(io.Writer) error {
panic("cannot render children directly")
}
// Group is a node that groups children nodes.
func Group(children ...Node) Node {
return group{children: children}
}
type fragment struct {
children []Node
}
// Fragment is a node that renders a fragment of nodes.
func Fragment(children ...Node) Node {
return fragment{children: children}
}
// String is a node that renders a fragment of nodes.
func (c fragment) String() string {
var b strings.Builder
for _, child := range c.children {
_ = child.Render(&b)
}
return b.String()
}
// Render is a node that renders a fragment of nodes.
func (c fragment) Render(w io.Writer) error {
for _, child := range c.children {
if err := child.Render(w); err != nil {
return err
}
}
return nil
}
type errorBoundary struct {
n ErrBoundaryFunc
}
// ErrBoundaryFunc is a function that returns a node.
type ErrBoundaryFunc func() Node
// ErrorBoundary is a node that catches panics and renders an error.
func ErrorBoundary(n ErrBoundaryFunc) Node {
return errorBoundary{n: n}
}
// Render is a node that renders an error boundary.
func (c errorBoundary) Render(w io.Writer) error {
n := c.n()
return n.Render(w)
}
type fallback struct {
n Node
f FallbackFunc
}
// FallbackFunc is a function that returns a node.
type FallbackFunc func(error) Node
// Fallback is a node that renders a fallback node if a condition is false.
func Fallback(n Node, f FallbackFunc) Node {
return fallback{n: n, f: f}
}
// Render is a node that renders a fallback node.
func (c fallback) Render(w io.Writer) (err error) {
if c.n == nil {
n := c.f(nil)
return n.Render(w)
}
defer func() {
if r := recover(); r != nil {
n := c.f(errorx.RecoverError(r))
err = n.Render(w)
}
}()
var b bytes.Buffer
if err := c.n.Render(&b); err != nil {
return c.f(err).Render(w)
}
_, err = io.Copy(w, &b)
return err
}
// If is a node that renders a child node if a condition is true.
func If(condition bool, n Node) Node {
if condition {
return n
}
return nil
}
// IfElse is a node that renders a child node if a condition is true, otherwise it renders another child node.
func IfElse(condition bool, n, elseN Node) Node {
if condition {
return n
}
return elseN
}
// Errors is a slice of errors.
type Errors[K comparable] map[K]error
// Error is a node that renders an error.
func FromValidationErrors[K comparable](errr []validator.FieldError) Errors[K] {
ee := make(Errors[K])
for _, err := range errr {
if k, ok := any(err).(K); ok {
ee[k] = err
}
}
return ee
}
// KeyExists is a node that renders a child node if a key exists in a map.
func KeyExists[K comparable, V any](m map[K]V, key K, fn func(k K, v V) Node) Node {
if v, ok := m[key]; ok {
return fn(key, v)
}
return nil
}
// ErrorExists is a node that renders a child node if an error exists.
func ErrorExists[K comparable](e Errors[K], key K, fn func(k K, v error) Node) Node {
return KeyExists(e, key, fn)
}
type md struct {
source []byte
opts []goldmark.Option
}
// Markdown is a node that renders a markdown.
func Markdown(source []byte, opts ...goldmark.Option) Node {
return md{source: source, opts: opts}
}
// Render is a node that renders a markdown.
func (m md) Render(w io.Writer) error {
md := goldmark.New(m.opts...)
return md.Convert(m.source, w)
}