Diff

util/stanza.lua @ 6978:30c96a5db360

util.stanza, util.xml, util.xmppstream: Add support for tracking defined namespaces and their prefix (stanza.namespaces), knowing/preserving prefix names is required for some applications (thanks daurnimator)
author Matthew Wild <mwild1@gmail.com>
date Tue, 08 Dec 2015 23:15:42 +0000
parent 6821:5de30376bf98
child 7256:9fbb9fbf7e52
line wrap: on
line diff
--- a/util/stanza.lua	Sun Dec 06 02:43:01 2015 +0100
+++ b/util/stanza.lua	Tue Dec 08 23:15:42 2015 +0000
@@ -40,8 +40,8 @@
 local stanza_mt = { __type = "stanza" };
 stanza_mt.__index = stanza_mt;
 
-local function stanza(name, attr)
-	local stanza = { name = name, attr = attr or {}, tags = {} };
+local function stanza(name, attr, namespaces)
+	local stanza = { name = name, attr = attr or {}, namespaces = namespaces, tags = {} };
 	return setmetatable(stanza, stanza_mt);
 end
 local stanza = stanza;
@@ -54,8 +54,8 @@
 	return self:tag("body", attr):text(text);
 end
 
-function stanza_mt:tag(name, attrs)
-	local s = stanza(name, attrs);
+function stanza_mt:tag(name, attr, namespaces)
+	local s = stanza(name, attr, namespaces);
 	local last_add = self.last_add;
 	if not last_add then last_add = {}; self.last_add = last_add; end
 	(last_add[#last_add] or self):add_direct_child(s);
@@ -333,7 +333,12 @@
 local function clone(stanza)
 	local attr, tags = {}, {};
 	for k,v in pairs(stanza.attr) do attr[k] = v; end
-	local new = { name = stanza.name, attr = attr, tags = tags };
+	local old_namespaces, namespaces = stanza.namespaces;
+	if old_namespaces then
+		namespaces = {};
+		for k,v in pairs(old_namespaces) do namespaces[k] = v; end
+	end
+	local new = { name = stanza.name, attr = attr, namespaces = namespaces, tags = tags };
 	for i=1,#stanza do
 		local child = stanza[i];
 		if child.name then