diff util/xmppstream.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
parents 5de6b93d0190
children bf8aa0d1951c
line wrap: on
line diff
--- a/util/xmppstream.lua	Sun Dec 06 02:43:01 2015 +0100
+++ b/util/xmppstream.lua	Tue Dec 08 23:15:42 2015 +0000
@@ -196,6 +196,29 @@
 		end
 	end
 
+	if stream_callbacks.track_namespaces then
+		local namespaces = {}
+		function xml_handlers:StartNamespaceDecl(prefix, url)
+			if prefix ~= nil then
+				namespaces[prefix] = url
+			end
+		end
+		function xml_handlers:EndNamespaceDecl(prefix)
+			if prefix ~= nil then
+				namespaces[prefix] = nil
+			end
+		end
+		local old_startelement = xml_handlers.StartElement
+		function xml_handlers:StartElement(tagname, attr)
+			old_startelement(self, tagname, attr)
+			local n = {}
+			for prefix, url in pairs(namespaces) do
+				n[prefix] = url
+			end
+			stanza.namespaces = n
+		end
+	end
+
 	local function restricted_handler(parser)
 		cb_error(session, "parse-error", "restricted-xml", "Restricted XML, see RFC 6120 section 11.1.");
 		if not parser.stop or not parser:stop() then