# HG changeset patch # User Matthew Wild # Date 1283140421 -3600 # Node ID 6350b114e0e466d5800ee5c0a0279fb5344364fa # Parent 193bb0936a4eeae1c29275c3f00b605716cb6f0a util.stanza: Add stanza:maptags() to apply a function over child tags (return nil to remove tag from stanza) diff -r 193bb0936a4e -r 6350b114e0e4 util/stanza.lua --- a/util/stanza.lua Mon Aug 30 04:37:53 2010 +0100 +++ b/util/stanza.lua Mon Aug 30 04:53:41 2010 +0100 @@ -151,6 +151,30 @@ 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 = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };