From f5fcc46905194ad7ddba1589c1d516f3907b45d2 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 30 Aug 2010 04:53:41 +0100 Subject: util.stanza: Add stanza:maptags() to apply a function over child tags (return nil to remove tag from stanza) --- util/stanza.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'util/stanza.lua') 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 = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; -- cgit v1.2.3