Skip to content

Commit

Permalink
Stanza: Add removeChildren() and quick test
Browse files Browse the repository at this point in the history
  • Loading branch information
mwild1 committed Dec 5, 2024
1 parent 8242a17 commit 845bc30
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
12 changes: 12 additions & 0 deletions snikket/Stanza.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion test/TestAll.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class TestAll {
public static function main() {
utest.UTest.run([
new TestSessionDescription(),
new TestChatMessage()
new TestChatMessage(),
new TestStanza(),
]);
}
}
25 changes: 25 additions & 0 deletions test/TestStanza.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 845bc30

Please sign in to comment.