aboutsummaryrefslogtreecommitdiffstats
path: root/util/stanza.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2010-08-30 04:53:41 +0100
committerMatthew Wild <mwild1@gmail.com>2010-08-30 04:53:41 +0100
commitf5fcc46905194ad7ddba1589c1d516f3907b45d2 (patch)
treefd597920ae015fb89c350ad89a0c62aeb5d6a71e /util/stanza.lua
parent8bbe120e4b0665cc00413a8816bb06cd904e80d7 (diff)
downloadprosody-f5fcc46905194ad7ddba1589c1d516f3907b45d2.tar.gz
prosody-f5fcc46905194ad7ddba1589c1d516f3907b45d2.zip
util.stanza: Add stanza:maptags() to apply a function over child tags (return nil to remove tag from stanza)
Diffstat (limited to 'util/stanza.lua')
-rw-r--r--util/stanza.lua24
1 files changed, 24 insertions, 0 deletions
diff --git a/util/stanza.lua b/util/stanza.lua
index 3ab4bb42..50fe02f0 100644
--- a/util/stanza.lua
+++ b/util/stanza.lua
@@ -151,6 +151,30 @@ function stanza_mt:childtags()
end, self.tags[1], i;
end
+function stanza_mt:maptags(callback)
+ local tags, curr_tag = self.tags, 1;
+ local n_children, n_tags = #self, #tags;
+
+ local i = 1;
+ while curr_tag <= n_tags do
+ if self[i] == tags[curr_tag] then
+ local ret = callback(self[i]);
+ if ret == nil then
+ t_remove(self, i);
+ t_remove(tags, curr_tag);
+ n_children = n_children - 1;
+ n_tags = n_tags - 1;
+ else
+ self[i] = ret;
+ tags[i] = ret;
+ end
+ i = i + 1;
+ curr_tag = curr_tag + 1;
+ end
+ end
+ return self;
+end
+
local xml_escape
do
local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };