Software /
code /
prosody
Comparison
util/stanza.lua @ 1984:f2b1f89e1d7c
util.stanza: Don't add xmlns to tags when serializing if same as the parent tag's xmlns. Should hopefully shut up Gajim once and for all :)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 17 Oct 2009 19:47:01 +0100 |
parent | 1935:05adeddf9f1b |
child | 2077:e33658f6052c |
comparison
equal
deleted
inserted
replaced
1983:1f49d778bd98 | 1984:f2b1f89e1d7c |
---|---|
3 -- Copyright (C) 2008-2009 Waqas Hussain | 3 -- Copyright (C) 2008-2009 Waqas Hussain |
4 -- | 4 -- |
5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
7 -- | 7 -- |
8 | |
8 | 9 |
9 local t_insert = table.insert; | 10 local t_insert = table.insert; |
10 local t_concat = table.concat; | 11 local t_concat = table.concat; |
11 local t_remove = table.remove; | 12 local t_remove = table.remove; |
12 local t_concat = table.concat; | 13 local t_concat = table.concat; |
128 local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; | 129 local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; |
129 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end | 130 function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end |
130 _M.xml_escape = xml_escape; | 131 _M.xml_escape = xml_escape; |
131 end | 132 end |
132 | 133 |
133 local function _dostring(t, buf, self, xml_escape) | 134 local function _dostring(t, buf, self, xml_escape, parentns) |
134 local nsid = 0; | 135 local nsid = 0; |
135 local name = t.name | 136 local name = t.name |
136 t_insert(buf, "<"..name); | 137 t_insert(buf, "<"..name); |
137 for k, v in pairs(t.attr) do | 138 for k, v in pairs(t.attr) do |
138 if s_find(k, "|", 1, true) then | 139 if s_find(k, "|", 1, true) then |
139 local ns, attrk = s_match(k, "^([^|]+)|(.+)$"); | 140 local ns, attrk = s_match(k, "^([^|]+)|(.+)$"); |
140 nsid = nsid + 1; | 141 nsid = nsid + 1; |
141 t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'"); | 142 t_insert(buf, " xmlns:ns"..nsid.."='"..xml_escape(ns).."' ".."ns"..nsid..":"..attrk.."='"..xml_escape(v).."'"); |
142 else | 143 elseif not(k == "xmlns" and v == parentns) then |
143 t_insert(buf, " "..k.."='"..xml_escape(v).."'"); | 144 t_insert(buf, " "..k.."='"..xml_escape(v).."'"); |
144 end | 145 end |
145 end | 146 end |
146 local len = #t; | 147 local len = #t; |
147 if len == 0 then | 148 if len == 0 then |
149 else | 150 else |
150 t_insert(buf, ">"); | 151 t_insert(buf, ">"); |
151 for n=1,len do | 152 for n=1,len do |
152 local child = t[n]; | 153 local child = t[n]; |
153 if child.name then | 154 if child.name then |
154 self(child, buf, self, xml_escape); | 155 self(child, buf, self, xml_escape, t.attr.xmlns); |
155 else | 156 else |
156 t_insert(buf, xml_escape(child)); | 157 t_insert(buf, xml_escape(child)); |
157 end | 158 end |
158 end | 159 end |
159 t_insert(buf, "</"..name..">"); | 160 t_insert(buf, "</"..name..">"); |
160 end | 161 end |
161 end | 162 end |
162 function stanza_mt.__tostring(t) | 163 function stanza_mt.__tostring(t) |
163 local buf = {}; | 164 local buf = {}; |
164 _dostring(t, buf, _dostring, xml_escape); | 165 _dostring(t, buf, _dostring, xml_escape, nil); |
165 return t_concat(buf); | 166 return t_concat(buf); |
166 end | 167 end |
167 | 168 |
168 function stanza_mt.top_tag(t) | 169 function stanza_mt.top_tag(t) |
169 local attr_string = ""; | 170 local attr_string = ""; |