Comparison

util/stanza.lua @ 4:09c3845ed442

Presence unavailable on disconnect
author matthew
date Sun, 24 Aug 2008 14:52:02 +0000
parent 2:9bb397205f26
child 6:7ad47ce20394
comparison
equal deleted inserted replaced
3:f674eb704134 4:09c3845ed442
4 local tostring = tostring; 4 local tostring = tostring;
5 local setmetatable = setmetatable; 5 local setmetatable = setmetatable;
6 local pairs = pairs; 6 local pairs = pairs;
7 local ipairs = ipairs; 7 local ipairs = ipairs;
8 local type = type; 8 local type = type;
9 9 local s_gsub = string.gsub;
10 module "stanza" 10 module "stanza"
11 11
12 stanza_mt = {}; 12 stanza_mt = {};
13 stanza_mt.__index = stanza_mt; 13 stanza_mt.__index = stanza_mt;
14 14
76 if v then return v; end 76 if v then return v; end
77 end, self.tags[1], i; 77 end, self.tags[1], i;
78 78
79 end 79 end
80 80
81 do
82 local xml_entities = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
83 function xml_escape(s) return s_gsub(s, "['&<>\"]", xml_entities); end
84 end
85
86 local xml_escape = xml_escape;
87
81 function stanza_mt.__tostring(t) 88 function stanza_mt.__tostring(t)
82 local children_text = ""; 89 local children_text = "";
83 for n, child in ipairs(t) do 90 for n, child in ipairs(t) do
84 children_text = children_text .. tostring(child); 91 if type(child) == "string" then
92 children_text = children_text .. xml_escape(child);
93 else
94 children_text = children_text .. tostring(child);
95 end
85 end 96 end
86 97
87 local attr_string = ""; 98 local attr_string = "";
88 if t.attr then 99 if t.attr then
89 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. format(" %s='%s'", k, tostring(v)); end end 100 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. format(" %s='%s'", k, tostring(v)); end end