Software /
code /
prosody
Comparison
util/stanza.lua @ 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 |
parent | 9630:bff66c3faceb |
child | 9732:51583ea2b4fd |
child | 11205:9d1e21c23784 |
comparison
equal
deleted
inserted
replaced
9673:e7e75b091c96 | 9674:6f97acc4583b |
---|---|
359 return s; | 359 return s; |
360 end | 360 end |
361 | 361 |
362 stanza_mt.__freeze = preserialize; | 362 stanza_mt.__freeze = preserialize; |
363 | 363 |
364 local function deserialize(stanza) | 364 local function deserialize(serialized) |
365 -- Set metatable | 365 -- Set metatable |
366 if stanza then | 366 if serialized then |
367 local attr = stanza.attr; | 367 local attr = serialized.attr; |
368 for i=1,#attr do attr[i] = nil; end | |
369 local attrx = {}; | 368 local attrx = {}; |
370 for att in pairs(attr) do | 369 for att, val in pairs(attr) do |
371 if s_find(att, "|", 1, true) and not s_find(att, "\1", 1, true) then | 370 if type(att) == "string" then |
372 local ns,na = s_match(att, "^([^|]+)|(.+)$"); | 371 if s_find(att, "|", 1, true) and not s_find(att, "\1", 1, true) then |
373 attrx[ns.."\1"..na] = attr[att]; | 372 local ns,na = s_match(att, "^([^|]+)|(.+)$"); |
374 attr[att] = nil; | 373 attrx[ns.."\1"..na] = val; |
375 end | 374 else |
376 end | 375 attrx[att] = val; |
377 for a,v in pairs(attrx) do | 376 end |
378 attr[a] = v; | 377 end |
379 end | 378 end |
380 setmetatable(stanza, stanza_mt); | 379 local stanza = new_stanza(serialized.name, attrx); |
381 for _, child in ipairs(stanza) do | 380 for _, child in ipairs(serialized) do |
382 if type(child) == "table" then | 381 if type(child) == "table" then |
383 deserialize(child); | 382 stanza:add_direct_child(deserialize(child)); |
384 end | 383 elseif type(child) == "string" then |
385 end | 384 stanza:add_direct_child(child); |
386 if not stanza.tags then | 385 end |
387 -- Rebuild tags | 386 end |
388 local tags = {}; | 387 return stanza; |
389 for _, child in ipairs(stanza) do | 388 end |
390 if type(child) == "table" then | |
391 t_insert(tags, child); | |
392 end | |
393 end | |
394 stanza.tags = tags; | |
395 end | |
396 end | |
397 | |
398 return stanza; | |
399 end | 389 end |
400 | 390 |
401 local function _clone(stanza) | 391 local function _clone(stanza) |
402 local attr, tags = {}, {}; | 392 local attr, tags = {}, {}; |
403 for k,v in pairs(stanza.attr) do attr[k] = v; end | 393 for k,v in pairs(stanza.attr) do attr[k] = v; end |