Changeset

9674:6f97acc4583b 0.11

util.stanza: Deserialize stanza without mutating input (fixes #711)
author Kim Alvefur <zash@zash.se>
date Sat, 01 Dec 2018 18:30:19 +0100
parents 9673:e7e75b091c96
children 9675:7d8e08f80dbd 9698:e616c37756b3
files util/stanza.lua
diffstat 1 files changed, 17 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/util/stanza.lua	Sun Dec 02 02:16:21 2018 +0100
+++ b/util/stanza.lua	Sat Dec 01 18:30:19 2018 +0100
@@ -361,41 +361,31 @@
 
 stanza_mt.__freeze = preserialize;
 
-local function deserialize(stanza)
+local function deserialize(serialized)
 	-- Set metatable
-	if stanza then
-		local attr = stanza.attr;
-		for i=1,#attr do attr[i] = nil; end
+	if serialized then
+		local attr = serialized.attr;
 		local attrx = {};
-		for att in pairs(attr) do
-			if s_find(att, "|", 1, true) and not s_find(att, "\1", 1, true) then
-				local ns,na = s_match(att, "^([^|]+)|(.+)$");
-				attrx[ns.."\1"..na] = attr[att];
-				attr[att] = nil;
+		for att, val in pairs(attr) do
+			if type(att) == "string" then
+				if s_find(att, "|", 1, true) and not s_find(att, "\1", 1, true) then
+					local ns,na = s_match(att, "^([^|]+)|(.+)$");
+					attrx[ns.."\1"..na] = val;
+				else
+					attrx[att] = val;
+				end
 			end
 		end
-		for a,v in pairs(attrx) do
-			attr[a] = v;
-		end
-		setmetatable(stanza, stanza_mt);
-		for _, child in ipairs(stanza) do
+		local stanza = new_stanza(serialized.name, attrx);
+		for _, child in ipairs(serialized) do
 			if type(child) == "table" then
-				deserialize(child);
+				stanza:add_direct_child(deserialize(child));
+			elseif type(child) == "string" then
+				stanza:add_direct_child(child);
 			end
 		end
-		if not stanza.tags then
-			-- Rebuild tags
-			local tags = {};
-			for _, child in ipairs(stanza) do
-				if type(child) == "table" then
-					t_insert(tags, child);
-				end
-			end
-			stanza.tags = tags;
-		end
+		return stanza;
 	end
-
-	return stanza;
 end
 
 local function _clone(stanza)