This commit is contained in:
maddalax 2024-09-30 12:47:10 -05:00
parent 7b83e2fde7
commit 3c4583c2b3
3 changed files with 14 additions and 50 deletions

View file

@ -18,7 +18,7 @@ func (m *AttributeMapOrdered) Set(key string, value any) {
case string: case string:
m.data.Set(key, v) m.data.Set(key, v)
case *AttributeMapOrdered: case *AttributeMapOrdered:
v.Each(func(k string, v any) { v.Each(func(k string, v string) {
m.Set(k, v) m.Set(k, v)
}) })
case *AttributeR: case *AttributeR:
@ -28,7 +28,7 @@ func (m *AttributeMapOrdered) Set(key string, value any) {
} }
} }
func (m *AttributeMapOrdered) Each(cb func(key string, value any)) { func (m *AttributeMapOrdered) Each(cb func(key string, value string)) {
m.data.Each(func(key string, value string) { m.data.Each(func(key string, value string) {
cb(key, value) cb(key, value)
}) })

View file

@ -23,10 +23,10 @@ func TestRender(t *testing.T) {
div := Div( div := Div(
Id("my-div"), Id("my-div"),
Attribute("data-attr-2", "value"), Attribute("data-attr-2", "value"),
AttributePairs( Attributes(&AttributeMap{
"data-attr-3", "value", "data-attr-3": "value",
"data-attr-4", "value", "data-attr-4": "value",
), }),
HxBeforeRequest( HxBeforeRequest(
SetText("before request"), SetText("before request"),
), ),

View file

@ -38,13 +38,6 @@ var voidTags = map[string]bool{
type RenderContext struct { type RenderContext struct {
builder *strings.Builder builder *strings.Builder
scripts []string scripts []string
next any
prev any
}
func (ctx *RenderContext) PrevIsAttribute() bool {
_, ok := ctx.prev.(*AttributeR)
return ok
} }
func (ctx *RenderContext) AddScript(funcName string, body string) { func (ctx *RenderContext) AddScript(funcName string, body string) {
@ -57,29 +50,6 @@ func (ctx *RenderContext) AddScript(funcName string, body string) {
ctx.scripts = append(ctx.scripts, script) ctx.scripts = append(ctx.scripts, script)
} }
func each[T any](ctx *RenderContext, arr []T, cb func(T)) {
for i, r := range arr {
if i == len(arr)-1 {
ctx.next = nil
} else {
ctx.next = arr[i+1]
}
cb(r)
}
}
func eachAttrMap(ctx *RenderContext, m *AttributeMapOrdered, cb func(string, string)) {
entries := m.Entries()
for i, entry := range entries {
if i == len(entries)-1 {
ctx.next = nil
} else {
ctx.next = entries[i+1]
}
cb(entry.Key, entry.Value)
}
}
func (node *Element) Render(context *RenderContext) { func (node *Element) Render(context *RenderContext) {
// some elements may not have a tag, such as a Fragment // some elements may not have a tag, such as a Fragment
@ -98,7 +68,7 @@ func (node *Element) Render(context *RenderContext) {
if node.tag != "" { if node.tag != "" {
context.builder.WriteString("<") context.builder.WriteString("<")
context.builder.WriteString(node.tag) context.builder.WriteString(node.tag)
eachAttrMap(context, node.attributes, func(key string, value string) { node.attributes.Each(func(key string, value string) {
NewAttribute(key, value).Render(context) NewAttribute(key, value).Render(context)
}) })
} }
@ -185,7 +155,6 @@ func (node *Element) Render(context *RenderContext) {
func renderScripts(context *RenderContext) { func renderScripts(context *RenderContext) {
for _, script := range context.scripts { for _, script := range context.scripts {
context.builder.WriteString(script) context.builder.WriteString(script)
context.prev = script
} }
context.scripts = []string{} context.scripts = []string{}
} }
@ -199,43 +168,36 @@ func (a *AttributeR) Render(context *RenderContext) {
context.builder.WriteString(html.EscapeString(a.Value)) context.builder.WriteString(html.EscapeString(a.Value))
context.builder.WriteString(`"`) context.builder.WriteString(`"`)
} }
context.prev = a
} }
func (t *TextContent) Render(context *RenderContext) { func (t *TextContent) Render(context *RenderContext) {
context.builder.WriteString(template.HTMLEscapeString(t.Content)) context.builder.WriteString(template.HTMLEscapeString(t.Content))
context.prev = t
} }
func (r *RawContent) Render(context *RenderContext) { func (r *RawContent) Render(context *RenderContext) {
context.builder.WriteString(r.Content) context.builder.WriteString(r.Content)
context.prev = r
} }
func (c *ChildList) Render(context *RenderContext) { func (c *ChildList) Render(context *RenderContext) {
for _, child := range c.Children { for _, child := range c.Children {
child.Render(context) child.Render(context)
context.prev = child
} }
} }
func (j SimpleJsCommand) Render(context *RenderContext) { func (j SimpleJsCommand) Render(context *RenderContext) {
context.builder.WriteString(j.Command) context.builder.WriteString(j.Command)
context.prev = j
} }
func (j ComplexJsCommand) Render(context *RenderContext) { func (j ComplexJsCommand) Render(context *RenderContext) {
context.builder.WriteString(j.Command) context.builder.WriteString(j.Command)
context.prev = j
} }
func (p *Partial) Render(context *RenderContext) { func (p *Partial) Render(context *RenderContext) {
p.Root.Render(context) p.Root.Render(context)
context.prev = p
} }
func (m *AttributeMapOrdered) Render(context *RenderContext) { func (m *AttributeMapOrdered) Render(context *RenderContext) {
eachAttrMap(context, m, func(key string, value string) { m.Each(func(key string, value string) {
NewAttribute(key, value).Render(context) NewAttribute(key, value).Render(context)
}) })
} }
@ -254,7 +216,8 @@ func (l *LifeCycle) Render(context *RenderContext) {
for event, commands := range l.handlers { for event, commands := range l.handlers {
m[event] = "" m[event] = ""
each(context, commands, func(command Command) {
for _, command := range commands {
switch c := command.(type) { switch c := command.(type) {
case SimpleJsCommand: case SimpleJsCommand:
m[event] += fmt.Sprintf("%s;", c.Command) m[event] += fmt.Sprintf("%s;", c.Command)
@ -262,13 +225,14 @@ func (l *LifeCycle) Render(context *RenderContext) {
context.AddScript(c.TempFuncName, c.Command) context.AddScript(c.TempFuncName, c.Command)
m[event] += fmt.Sprintf("%s(this);", c.TempFuncName) m[event] += fmt.Sprintf("%s(this);", c.TempFuncName)
case *AttributeMapOrdered: case *AttributeMapOrdered:
eachAttrMap(context, c, func(k string, v string) { c.Each(func(key string, value string) {
l.fromAttributeMap(event, k, v, context) l.fromAttributeMap(event, key, value, context)
}) })
case *AttributeR: case *AttributeR:
l.fromAttributeMap(event, c.Name, c.Value, context) l.fromAttributeMap(event, c.Name, c.Value, context)
} }
}) }
} }
children := make([]Ren, 0) children := make([]Ren, 0)