-
Notifications
You must be signed in to change notification settings - Fork 2
/
gax_elem.go
88 lines (77 loc) · 2.17 KB
/
gax_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
package gax
import "fmt"
/*
Primary API. Short for "element" or "HTML element". Expresses an HTML/XML tag,
with attributes and inner content. Creates an instance of `Elem`, which
implements the `Ren` interface. It can render itself as HTML/XML, or be passed
as a child to `F`, `E`, `Bui.E`.
For special rules regarding child encoding, see `Bui.E`.
*/
func E(tag string, attrs Attrs, child ...any) Elem {
if child == nil {
return Elem{tag, attrs, nil}
}
return Elem{tag, attrs, child}
}
/*
Represents an HTML element. Usually created via `E`. Can render itself, or be
passed as a child to `F` or `Bui.E`.
*/
type Elem struct {
Tag string
Attrs Attrs
Child any
}
var _ = Ren(Elem{})
/*
Implement `Ren`. This allows `Elem` to be passed as a child to the various
rendering functions like `E`, `F`, `Bui.E`. As a special case, `Elem` with
an empty `.Tag` does not render anything.
*/
func (self Elem) Render(b *Bui) {
if self.Tag != `` {
b.E(self.Tag, self.Attrs, self.Child)
}
}
// Implement `fmt.Stringer` for debug purposes. Not used by builder methods.
func (self Elem) String() string { return F(self).String() }
// Returns a modified version where `.Attrs` contain an attribute with
func (self Elem) AttrSet(key, val string) Elem {
self.Attrs = self.Attrs.Set(key, val)
return self
}
func (self Elem) AttrAdd(key, val string) Elem {
self.Attrs = self.Attrs.Add(key, val)
return self
}
/*
Implement `fmt.GoStringer` for debug purposes. Not used by builder methods.
Represents itself as a call to `E`, which is the recommended way to write
this.
*/
func (self Elem) GoString() string {
var buf NonEscWri
_, _ = buf.WriteString(`E(`)
buf = NonEscWri(appendQuote(buf, self.Tag))
_, _ = buf.WriteString(`, `)
buf = append(buf, self.Attrs.GoString()...)
buf = appendElemChild(buf, self.Child)
_, _ = buf.WriteString(`)`)
return buf.String()
}
func appendElemChild(buf NonEscWri, val any) NonEscWri {
switch val := val.(type) {
case nil:
case []any:
for _, val := range val {
buf = appendElemChild(buf, val)
}
case string:
_, _ = buf.WriteString(`, `)
buf = NonEscWri(appendQuote(buf, val))
default:
_, _ = buf.WriteString(`, `)
fmt.Fprintf(&buf, `%#v`, val)
}
return buf
}