fix(html): preserve switch wrapper paths
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
14c16b5385
commit
4a3a69e8b7
4 changed files with 45 additions and 0 deletions
16
edge_test.go
16
edge_test.go
|
|
@ -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) {
|
||||
|
|
|
|||
10
layout.go
10
layout.go
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
4
node.go
4
node.go
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
15
node_test.go
15
node_test.go
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue