Software / code / prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
| 6977:450db0b83fe9 | 6978:30c96a5db360 |
|---|---|
| 38 local _ENV = nil; | 38 local _ENV = nil; |
| 39 | 39 |
| 40 local stanza_mt = { __type = "stanza" }; | 40 local stanza_mt = { __type = "stanza" }; |
| 41 stanza_mt.__index = stanza_mt; | 41 stanza_mt.__index = stanza_mt; |
| 42 | 42 |
| 43 local function stanza(name, attr) | 43 local function stanza(name, attr, namespaces) |
| 44 local stanza = { name = name, attr = attr or {}, tags = {} }; | 44 local stanza = { name = name, attr = attr or {}, namespaces = namespaces, tags = {} }; |
| 45 return setmetatable(stanza, stanza_mt); | 45 return setmetatable(stanza, stanza_mt); |
| 46 end | 46 end |
| 47 local stanza = stanza; | 47 local stanza = stanza; |
| 48 | 48 |
| 49 function stanza_mt:query(xmlns) | 49 function stanza_mt:query(xmlns) |
| 52 | 52 |
| 53 function stanza_mt:body(text, attr) | 53 function stanza_mt:body(text, attr) |
| 54 return self:tag("body", attr):text(text); | 54 return self:tag("body", attr):text(text); |
| 55 end | 55 end |
| 56 | 56 |
| 57 function stanza_mt:tag(name, attrs) | 57 function stanza_mt:tag(name, attr, namespaces) |
| 58 local s = stanza(name, attrs); | 58 local s = stanza(name, attr, namespaces); |
| 59 local last_add = self.last_add; | 59 local last_add = self.last_add; |
| 60 if not last_add then last_add = {}; self.last_add = last_add; end | 60 if not last_add then last_add = {}; self.last_add = last_add; end |
| 61 (last_add[#last_add] or self):add_direct_child(s); | 61 (last_add[#last_add] or self):add_direct_child(s); |
| 62 t_insert(last_add, s); | 62 t_insert(last_add, s); |
| 63 return self; | 63 return self; |
| 331 end | 331 end |
| 332 | 332 |
| 333 local function clone(stanza) | 333 local function clone(stanza) |
| 334 local attr, tags = {}, {}; | 334 local attr, tags = {}, {}; |
| 335 for k,v in pairs(stanza.attr) do attr[k] = v; end | 335 for k,v in pairs(stanza.attr) do attr[k] = v; end |
| 336 local new = { name = stanza.name, attr = attr, tags = tags }; | 336 local old_namespaces, namespaces = stanza.namespaces; |
| 337 if old_namespaces then | |
| 338 namespaces = {}; | |
| 339 for k,v in pairs(old_namespaces) do namespaces[k] = v; end | |
| 340 end | |
| 341 local new = { name = stanza.name, attr = attr, namespaces = namespaces, tags = tags }; | |
| 337 for i=1,#stanza do | 342 for i=1,#stanza do |
| 338 local child = stanza[i]; | 343 local child = stanza[i]; |
| 339 if child.name then | 344 if child.name then |
| 340 child = clone(child); | 345 child = clone(child); |
| 341 t_insert(tags, child); | 346 t_insert(tags, child); |