fix(html): preserve switch wrapper paths
All checks were successful
Security Scan / security (push) Successful in 9s
Test / test (push) Successful in 59s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-03-31 20:07:35 +00:00
parent 14c16b5385
commit 4a3a69e8b7
4 changed files with 45 additions and 0 deletions

View file

@ -458,6 +458,22 @@ func TestLayout_NestedThroughIf_Ugly(t *testing.T) {
}
}
func TestLayout_NestedThroughSwitch_Ugly(t *testing.T) {
ctx := NewContext()
inner := NewLayout("C").C(Raw("wrapped"))
outer := NewLayout("C").C(Switch(func(*Context) string { return "match" }, map[string]Node{
"match": inner,
"miss": Raw("ignored"),
}))
got := outer.Render(ctx)
if !containsText(got, `data-block="C-0-C-0"`) {
t.Fatalf("nested layout inside Switch should inherit block path, got:\n%s", got)
}
}
// --- Render convenience function edge cases ---
func TestRender_NilContext_Ugly(t *testing.T) {

View file

@ -71,6 +71,16 @@ func renderWithLayoutPath(node Node, ctx *Context, path string) string {
return ""
}
return renderWithLayoutPath(t.node, ctx, path)
case *switchNode:
if t == nil || t.selector == nil || t.cases == nil {
return ""
}
key := t.selector(ctx)
node, ok := t.cases[key]
if !ok || node == nil {
return ""
}
return renderWithLayoutPath(node, ctx, path)
default:
return node.Render(ctx)
}

View file

@ -102,6 +102,10 @@ func Attr(n Node, key, value string) Node {
Attr(t.node, key, value)
case *entitledNode:
Attr(t.node, key, value)
case *switchNode:
for _, child := range t.cases {
Attr(child, key, value)
}
}
return n
}

View file

@ -274,6 +274,21 @@ func TestAttr_ThroughEntitledNode_Good(t *testing.T) {
}
}
func TestAttr_ThroughSwitchNode_Good(t *testing.T) {
ctx := NewContext()
inner := El("div", Raw("content"))
node := Switch(func(*Context) string { return "match" }, map[string]Node{
"match": inner,
"miss": El("span", Raw("unused")),
})
Attr(node, "data-state", "active")
got := node.Render(ctx)
want := `<div data-state="active">content</div>`
if got != want {
t.Errorf("Attr through Switch = %q, want %q", got, want)
}
}
func TestTextNode_WithService_Good(t *testing.T) {
svc, _ := i18n.New()
ctx := NewContextWithService(svc)