git » sdk » commit 7493f4a

clone should clone attributes

author Stephen Paul Weber
2026-11-09 16:18:06 UTC
committer Stephen Paul Weber
2026-11-16 14:55:03 UTC
parent bfa192dedaebb5fcbbb9602a79e1e2688a9390db

clone should clone attributes

mutating the new object should not mutate parent or vice versa, not even
for attributes.

borogove/Stanza.hx +2 -1
test/TestStanza.hx +8 -0

diff --git a/borogove/Stanza.hx b/borogove/Stanza.hx
index 8d42b7e..c60c466 100644
--- a/borogove/Stanza.hx
+++ b/borogove/Stanza.hx
@@ -227,7 +227,8 @@ class Stanza {
 	}
 
 	public function clone():Stanza {
-		var clone = new Stanza(this.name, this.attr);
+		var clone = new Stanza(this.name);
+		for (key => value in this.attr) clone.attr.set(key, value);
 		for (child in children) {
 			clone.addDirectChild(switch(child) {
 				case Element(c): Element(c.clone());
diff --git a/test/TestStanza.hx b/test/TestStanza.hx
index d905cb7..5f1c3d4 100644
--- a/test/TestStanza.hx
+++ b/test/TestStanza.hx
@@ -68,10 +68,18 @@ class TestStanza extends utest.Test {
 			.tag("child")
 				.text("original")
 			.up();
+		s.attr.set("original", "yes");
 
 		final cloned = s.clone();
 		Assert.equals(s.serialize(), cloned.serialize());
 
+		s.attr.set("original", "modified");
+		cloned.attr.set("cloned", "yes");
+		Assert.equals("modified", s.attr.get("original"));
+		Assert.equals("yes", cloned.attr.get("original"));
+		Assert.isNull(s.attr.get("cloned"));
+		Assert.equals("yes", cloned.attr.get("cloned"));
+
 		// Modify original
 		s.allTags("child")[0].children = [];
 		s.allTags("child")[0].text("modified");