Software /
code /
prosody
Changeset
11786:39164ea2ab9e
util.stanza: Add :get_child_with_attr() + tests
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 12 Sep 2021 10:31:02 +0100 |
parents | 11785:b1381e302cab |
children | 11787:3ae6fa901a8b |
files | spec/util_stanza_spec.lua util/stanza.lua |
diffstat | 2 files changed, 28 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/spec/util_stanza_spec.lua Sat Sep 11 13:59:35 2021 +0100 +++ b/spec/util_stanza_spec.lua Sun Sep 12 10:31:02 2021 +0100 @@ -455,6 +455,26 @@ end); end); + describe("get_child_with_attr", function () + local s = st.message({ type = "chat" }) + :text_tag("body", "Hello world", { ["xml:lang"] = "en" }) + :text_tag("body", "Bonjour le monde", { ["xml:lang"] = "fr" }) + :text_tag("body", "Hallo Welt", { ["xml:lang"] = "de" }) + + it("works", function () + assert.equal(s:get_child_with_attr("body", nil, "xml:lang", "en"):get_text(), "Hello world"); + assert.equal(s:get_child_with_attr("body", nil, "xml:lang", "de"):get_text(), "Hallo Welt"); + assert.equal(s:get_child_with_attr("body", nil, "xml:lang", "fr"):get_text(), "Bonjour le monde"); + assert.is_nil(s:get_child_with_attr("body", nil, "xml:lang", "FR")); + assert.is_nil(s:get_child_with_attr("body", nil, "xml:lang", "es")); + end); + + it("supports normalization", function () + assert.equal(s:get_child_with_attr("body", nil, "xml:lang", "EN", string.upper):get_text(), "Hello world"); + assert.is_nil(s:get_child_with_attr("body", nil, "xml:lang", "ES", string.upper)); + end); + end); + describe("#clone", function () it("works", function () local s = st.message({type="chat"}, "Hello"):reset();
--- a/util/stanza.lua Sat Sep 11 13:59:35 2021 +0100 +++ b/util/stanza.lua Sun Sep 12 10:31:02 2021 +0100 @@ -191,6 +191,14 @@ end end +function stanza_mt:get_child_with_attr(name, xmlns, attr_name, attr_value, normalize) + for tag in self:childtags(name, xmlns) do + if (normalize and normalize(tag.attr[attr_name]) or tag.attr[attr_name]) == attr_value then + return tag; + end + end +end + function stanza_mt:children() local i = 0; return function (a)