diff --git a/edge_test.go b/edge_test.go index aaaaa12..9f53f0e 100644 --- a/edge_test.go +++ b/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) { diff --git a/layout.go b/layout.go index ea17221..7fba847 100644 --- a/layout.go +++ b/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) } diff --git a/node.go b/node.go index e3b8e3c..4500e53 100644 --- a/node.go +++ b/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 } diff --git a/node_test.go b/node_test.go index a2a550a..a9fccff 100644 --- a/node_test.go +++ b/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 := `