Changeset

13568:3615590fd9ed

util.stanza: Handle namespace prefixes for attributes in :find() More correct handling of namespaces here. This works with both prefixes from the parser and hacky .attr["foo:bar"]
author Kim Alvefur <zash@zash.se>
date Sun, 17 Nov 2024 12:35:51 +0100
parents 13567:515ac7e8ae6d
children 13569:59dacbd637c2
files spec/util_stanza_spec.lua spec/util_xtemplate_spec.lua util/stanza.lua
diffstat 3 files changed, 10 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_stanza_spec.lua	Sat Nov 16 09:20:29 2024 +0100
+++ b/spec/util_stanza_spec.lua	Sun Nov 17 12:35:51 2024 +0100
@@ -577,5 +577,9 @@
 			assert.equal("text", s:find("{urn:example:not:same}child/nested#"), "finds nested text")
 			assert.is_nil(s:find("child"), "respects namespaces")
 		end);
+		it("handles namespaced attributes", function()
+			local s = st.stanza("root", { ["urn:example:namespace\1attr"] = "value" }, { e = "urn:example:namespace" });
+			assert.equal("value", s:find("@e:attr"), "finds prefixed attr")
+		end)
 	end);
 end);
--- a/spec/util_xtemplate_spec.lua	Sat Nov 16 09:20:29 2024 +0100
+++ b/spec/util_xtemplate_spec.lua	Sun Nov 17 12:35:51 2024 +0100
@@ -10,7 +10,7 @@
 		end)
 		it("supports conditionals", function ()
 			local atom_tmpl = "{@pubsub:title|and{*{@pubsub:title}*\n\n}}{summary|or{{author/name|and{{author/name} posted }}{title}}}";
-			local atom_data = st.stanza("entry", { xmlns = "http://www.w3.org/2005/Atom" });
+			local atom_data = st.stanza("entry", { xmlns = "http://www.w3.org/2005/Atom" }, {["pubsub"] = "http://jabber.org/protocol/pubsub"});
 			assert.same("", xtemplate.render(atom_tmpl, atom_data));
 
 			atom_data:text_tag("title", "an Entry")
@@ -22,8 +22,7 @@
 			atom_data:text_tag("summary", "Juliet just posted a new entry");
 			assert.same("Juliet just posted a new entry", xtemplate.render(atom_tmpl, atom_data));
 
-			atom_data.attr["xmlns:pubsub"] = "http://jabber.org/protocol/pubsub";
-			atom_data.attr["pubsub:title"] = "Juliets musings";
+			atom_data.attr["http://jabber.org/protocol/pubsub\1title"] = "Juliets musings";
 			assert.same("*Juliets musings*\n\nJuliet just posted a new entry", xtemplate.render(atom_tmpl, atom_data));
 		end)
 		it("can strip surrounding whitespace", function ()
--- a/util/stanza.lua	Sat Nov 16 09:20:29 2024 +0100
+++ b/util/stanza.lua	Sun Nov 17 12:35:51 2024 +0100
@@ -277,6 +277,10 @@
 		local xmlns, name, text;
 		local char = s_sub(path, pos, pos);
 		if char == "@" then
+			local prefix, attr = s_match(path, "^([^:]+):(.*)", pos+1);
+			if prefix and self.namespaces and self.namespaces[prefix] then
+				return self.attr[self.namespaces[prefix] .. "\1" .. attr];
+			end
 			return self.attr[s_sub(path, pos + 1)];
 		elseif char == "{" then
 			xmlns, pos = s_match(path, "^([^}]+)}()", pos + 1);