diff --git a/node.go b/node.go
index 3571527..9f32fa0 100644
--- a/node.go
+++ b/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)
}
}
diff --git a/node_test.go b/node_test.go
index 1cbdda7..bf87cf0 100644
--- a/node_test.go
+++ b/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 := `itemitem`
+ 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 {