Comparison

util/stanza.lua @ 180:d8b9a19d70eb

Make add_child() behave as expected. Old add_child() is now add_direct_child()
author Matthew Wild <mwild1@gmail.com>
date Sun, 26 Oct 2008 14:27:10 +0000
parent 145:fbb3a4ff9cf1
child 181:d952eae776dc
comparison
equal deleted inserted replaced
179:774a172b03c8 180:d8b9a19d70eb
27 function stanza_mt:query(xmlns) 27 function stanza_mt:query(xmlns)
28 return self:tag("query", { xmlns = xmlns }); 28 return self:tag("query", { xmlns = xmlns });
29 end 29 end
30 function stanza_mt:tag(name, attrs) 30 function stanza_mt:tag(name, attrs)
31 local s = stanza(name, attrs); 31 local s = stanza(name, attrs);
32 (self.last_add[#self.last_add] or self):add_child(s); 32 (self.last_add[#self.last_add] or self):add_direct_child(s);
33 t_insert(self.last_add, s); 33 t_insert(self.last_add, s);
34 return self; 34 return self;
35 end 35 end
36 36
37 function stanza_mt:text(text) 37 function stanza_mt:text(text)
38 (self.last_add[#self.last_add] or self):add_child(text); 38 (self.last_add[#self.last_add] or self):add_direct_child(text);
39 return self; 39 return self;
40 end 40 end
41 41
42 function stanza_mt:up() 42 function stanza_mt:up()
43 t_remove(self.last_add); 43 t_remove(self.last_add);
44 return self; 44 return self;
45 end 45 end
46 46
47 function stanza_mt:add_child(child) 47 function stanza_mt:add_direct_child(child)
48 if type(child) == "table" then 48 if type(child) == "table" then
49 t_insert(self.tags, child); 49 t_insert(self.tags, child);
50 end 50 end
51 t_insert(self, child); 51 t_insert(self, child);
52 end
53
54 function stanza_mt:add_child(child)
55 (self.last_add[#self.last_add] or self):add_direct_child(child);
56 t_insert(self.last_add, s);
57 return self;
52 end 58 end
53 59
54 function stanza_mt:child_with_name(name) 60 function stanza_mt:child_with_name(name)
55 for _, child in ipairs(self) do 61 for _, child in ipairs(self) do
56 if child.name == name then return child; end 62 if child.name == name then return child; end
99 end 105 end
100 return s_format("<%s%s>%s</%s>", t.name, attr_string, children_text, t.name); 106 return s_format("<%s%s>%s</%s>", t.name, attr_string, children_text, t.name);
101 end 107 end
102 108
103 function stanza_mt.__add(s1, s2) 109 function stanza_mt.__add(s1, s2)
104 return s1:add_child(s2); 110 return s1:add_direct_child(s2);
105 end 111 end
106 112
107 113
108 do 114 do
109 local id = 0; 115 local id = 0;