util/stanza.lua

changeset 2077
e33658f6052c
parent 1984
f2b1f89e1d7c
child 2264
49580a13f71e
--- a/util/stanza.lua	Thu Nov 12 13:34:38 2009 +0500
+++ b/util/stanza.lua	Thu Nov 12 13:42:44 2009 +0500
@@ -136,11 +136,11 @@
 	local name = t.name
 	t_insert(buf, "<"..name);
 	for k, v in pairs(t.attr) do
-		if s_find(k, "|", 1, true) then
-			local ns, attrk = s_match(k, "^([^|]+)|(.+)$");
+		if s_find(k, "\1", 1, true) then
+			local ns, attrk = s_match(k, "^([^\1]*)\1?(.*)$");
 			nsid = nsid + 1;
 			t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'");
-               elseif not(k == "xmlns" and v == parentns) then
+		elseif not(k == "xmlns" and v == parentns) then
 			t_insert(buf, " "..k.."='"..xml_escape(v).."'");
 		end
 	end
@@ -152,7 +152,7 @@
 		for n=1,len do
 			local child = t[n];
 			if child.name then
-                               self(child, buf, self, xml_escape, t.attr.xmlns);
+				self(child, buf, self, xml_escape, t.attr.xmlns);
 			else
 				t_insert(buf, xml_escape(child));
 			end
@@ -162,7 +162,7 @@
 end
 function stanza_mt.__tostring(t)
 	local buf = {};
-       _dostring(t, buf, _dostring, xml_escape, nil);
+	_dostring(t, buf, _dostring, xml_escape, nil);
 	return t_concat(buf);
 end
 
@@ -210,6 +210,17 @@
 	if stanza then
 		local attr = stanza.attr;
 		for i=1,#attr do attr[i] = nil; end
+		local attrx = {};
+		for att in pairs(attr) do
+			if s_find(att, "|", 1, true) and not s_find(k, "\1", 1, true) then
+				local ns,na = s_match(k, "^([^|]+)|(.+)$");
+				attrx[ns.."\1"..na] = attr[att];
+				attr[att] = nil;
+			end
+		end
+		for a,v in pairs(attrx) do
+			attr[x] = v;
+		end
 		setmetatable(stanza, stanza_mt);
 		for _, child in ipairs(stanza) do
 			if type(child) == "table" then

mercurial