git » sdk » commit 845bc30

Stanza: Add removeChildren() and quick test

author Matthew Wild
2024-12-05 13:22:40 UTC
committer Matthew Wild
2024-12-05 13:27:32 UTC
parent 8242a17dd137a94250f3ffa2be9ef9aba0bfd0f1

Stanza: Add removeChildren() and quick test

snikket/Stanza.hx +12 -0
test/TestAll.hx +2 -1
test/TestStanza.hx +25 -0

diff --git a/snikket/Stanza.hx b/snikket/Stanza.hx
index 26e21ea..b4ef9c7 100644
--- a/snikket/Stanza.hx
+++ b/snikket/Stanza.hx
@@ -338,6 +338,18 @@ class Stanza implements NodeInterface {
 			errorTag.getChildText("text", "urn:ietf:params:xml:ns:xmpp-stanzas")
 		);
 	}
+
+	public function removeChildren(?name: String, ?xmlns_:String):Void {
+		final xmlns = xmlns_??attr.get("xmlns");
+		children = children.filter((child:Node) -> {
+			switch(child) {
+				case Element(c):
+					return !( (name == null || c.name == name) && c.attr.get("xmlns")??xmlns == xmlns);
+				default:
+					return true;
+			}
+		});
+	}
 }
 
 enum IqRequestType {
diff --git a/test/TestAll.hx b/test/TestAll.hx
index 2f215fa..1c11a8f 100644
--- a/test/TestAll.hx
+++ b/test/TestAll.hx
@@ -7,7 +7,8 @@ class TestAll {
 	public static function main() {
 		utest.UTest.run([
 			new TestSessionDescription(),
-			new TestChatMessage()
+			new TestChatMessage(),
+			new TestStanza(),
 		]);
 	}
 }
diff --git a/test/TestStanza.hx b/test/TestStanza.hx
new file mode 100644
index 0000000..210b795
--- /dev/null
+++ b/test/TestStanza.hx
@@ -0,0 +1,25 @@
+package test;
+
+
+import utest.Assert;
+import utest.Async;
+import snikket.Stanza;
+
+class TestStanza extends utest.Test {
+    public function testRemoveChildren() {
+        final s = new Stanza("test", { xmlns: "urn:example:foo" })
+            .textTag("odd", "")
+            .textTag("even", "")
+            .textTag("odd", "")
+            .textTag("even", "");
+
+        s.removeChildren("odd");
+
+        var count = 0;
+        for(tag in s.allTags()) {
+            count++;
+            Assert.equals("even", tag.name);
+        }
+        Assert.equals(2, count);
+    }
+}