fix(html): isolate shared nodes in Each
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
cc75f3b533
commit
f3c2bb1ca7
2 changed files with 17 additions and 1 deletions
2
node.go
2
node.go
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
16
node_test.go
16
node_test.go
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue