fix(html): isolate shared nodes in Each
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-04 01:05:53 +00:00
parent cc75f3b533
commit f3c2bb1ca7
2 changed files with 17 additions and 1 deletions

View file

@ -909,7 +909,7 @@ func (n *eachNode[T]) setAttr(key, value string) {
prev := n.fn
n.fn = func(item T) Node {
return Attr(prev(item), key, value)
return Attr(cloneNode(prev(item)), key, value)
}
}

View file

@ -1112,6 +1112,22 @@ func TestAttr_ThroughEachNode(t *testing.T) {
}
}
func TestAttr_ThroughEachNode_IsolatedFromSharedItems(t *testing.T) {
ctx := NewContext()
shared := El("span", Raw("item"))
node := Each([]int{1, 2}, func(int) Node {
return shared
})
Attr(node, "class", "shared")
got := node.Render(ctx)
want := `<span class="shared">item</span><span class="shared">item</span>`
if got != want {
t.Errorf("Attr through Each should clone shared items, got %q, want %q", got, want)
}
}
func TestAttr_ThroughEachSeqNode(t *testing.T) {
ctx := NewContext()
node := EachSeq(slices.Values([]string{"a", "b"}), func(item string) Node {