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); + } +}