2024-09-20 01:24:44 +00:00
|
|
|
package h
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2024-09-20 01:34:50 +00:00
|
|
|
func (node *Element) Render(builder *strings.Builder) {
|
2024-09-20 01:24:44 +00:00
|
|
|
// some elements may not have a tag, such as a Fragment
|
2024-09-20 02:13:08 +00:00
|
|
|
|
2024-09-20 01:24:44 +00:00
|
|
|
if node.tag != "" {
|
|
|
|
|
builder.WriteString("<" + node.tag)
|
|
|
|
|
builder.WriteString(" ")
|
2024-09-20 02:13:08 +00:00
|
|
|
|
2024-09-20 01:24:44 +00:00
|
|
|
for name, value := range node.attributes {
|
2024-09-20 01:34:50 +00:00
|
|
|
NewAttribute(name, value).Render(builder)
|
2024-09-20 01:24:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// first pass, flatten the children
|
|
|
|
|
flatChildren := make([]Ren, 0)
|
|
|
|
|
for _, child := range node.children {
|
|
|
|
|
switch child.(type) {
|
|
|
|
|
case *ChildList:
|
|
|
|
|
flatChildren = append(flatChildren, child.(*ChildList).Children...)
|
|
|
|
|
default:
|
|
|
|
|
flatChildren = append(flatChildren, child)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node.children = flatChildren
|
|
|
|
|
|
|
|
|
|
// second pass, render any attributes within the tag
|
|
|
|
|
for _, child := range node.children {
|
|
|
|
|
switch child.(type) {
|
|
|
|
|
case *AttributeMap:
|
2024-09-20 01:34:50 +00:00
|
|
|
child.Render(builder)
|
2024-09-20 01:24:44 +00:00
|
|
|
case *LifeCycle:
|
2024-09-20 01:34:50 +00:00
|
|
|
child.Render(builder)
|
2024-09-20 01:24:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// close the tag
|
|
|
|
|
if node.tag != "" {
|
|
|
|
|
builder.WriteString(">")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// render the children elements that are not attributes
|
|
|
|
|
for _, child := range node.children {
|
|
|
|
|
switch child.(type) {
|
|
|
|
|
case *AttributeMap:
|
|
|
|
|
continue
|
|
|
|
|
case *LifeCycle:
|
|
|
|
|
continue
|
|
|
|
|
default:
|
2024-09-20 01:34:50 +00:00
|
|
|
child.Render(builder)
|
2024-09-20 01:24:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if node.tag != "" {
|
|
|
|
|
builder.WriteString("</" + node.tag + ">")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 01:34:50 +00:00
|
|
|
func (a *AttributeR) Render(builder *strings.Builder) {
|
|
|
|
|
builder.WriteString(fmt.Sprintf(`%s="%s"`, a.Name, a.Value))
|
2024-09-20 01:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-20 01:34:50 +00:00
|
|
|
func (t *TextContent) Render(builder *strings.Builder) {
|
|
|
|
|
builder.WriteString(t.Content)
|
2024-09-20 01:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-20 01:34:50 +00:00
|
|
|
func (r *RawContent) Render(builder *strings.Builder) {
|
|
|
|
|
builder.WriteString(r.Content)
|
2024-09-20 01:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-20 01:34:50 +00:00
|
|
|
func (c *ChildList) Render(builder *strings.Builder) {
|
2024-09-20 01:24:44 +00:00
|
|
|
for _, child := range c.Children {
|
2024-09-20 01:34:50 +00:00
|
|
|
child.Render(builder)
|
2024-09-20 01:24:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 01:34:50 +00:00
|
|
|
func (m *AttributeMap) Render(builder *strings.Builder) {
|
2024-09-20 01:24:44 +00:00
|
|
|
m2 := m.ToMap()
|
|
|
|
|
|
|
|
|
|
for k, v := range m2 {
|
2024-09-20 02:13:08 +00:00
|
|
|
builder.WriteString(" ")
|
2024-09-20 01:34:50 +00:00
|
|
|
NewAttribute(k, v).Render(builder)
|
2024-09-20 01:24:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 01:34:50 +00:00
|
|
|
func (l *LifeCycle) Render(builder *strings.Builder) {
|
2024-09-20 01:24:44 +00:00
|
|
|
m := make(map[string]string)
|
|
|
|
|
|
|
|
|
|
for event, commands := range l.handlers {
|
|
|
|
|
m[event] = ""
|
|
|
|
|
for _, command := range commands {
|
|
|
|
|
m[event] += fmt.Sprintf("%s;", command.Command)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
children := make([]Ren, 0)
|
|
|
|
|
|
|
|
|
|
for event, js := range m {
|
|
|
|
|
children = append(children, Attribute(event, js))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 01:34:50 +00:00
|
|
|
Children(children...).Render(builder)
|
2024-09-20 01:24:44 +00:00
|
|
|
}
|