aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2024-07-11 15:04:29 +0200
committerKim Alvefur <zash@zash.se>2024-07-11 15:04:29 +0200
commit8a96854733732892aba66cd153e74d24f5e6bf95 (patch)
tree41bba794f8d21f0b80710986bed7e32179df5703
parentb522fd0e05988adcef50c196be786591ac038c30 (diff)
downloadprosody-8a96854733732892aba66cd153e74d24f5e6bf95.tar.gz
prosody-8a96854733732892aba66cd153e74d24f5e6bf95.zip
util.xtemplate: Fix error on applying each() to zero stanzas
-rw-r--r--spec/util_xtemplate_spec.lua4
-rw-r--r--teal-src/prosody/util/xtemplate.tl5
-rw-r--r--util/xtemplate.lua3
3 files changed, 10 insertions, 2 deletions
diff --git a/spec/util_xtemplate_spec.lua b/spec/util_xtemplate_spec.lua
index 4561378e..421be43f 100644
--- a/spec/util_xtemplate_spec.lua
+++ b/spec/util_xtemplate_spec.lua
@@ -38,6 +38,10 @@ describe("util.xtemplate", function ()
x:reset();
assert.same("12345", xtemplate.render("{foo/bar|each(i){{#}}}", x));
end)
+ it("handles missing inputs", function ()
+ local x = st.stanza("root");
+ assert.same("", xtemplate.render("{foo/bar|each(i){{#}}}", x));
+ end)
end)
end)
end)
diff --git a/teal-src/prosody/util/xtemplate.tl b/teal-src/prosody/util/xtemplate.tl
index 9b6b678b..84003051 100644
--- a/teal-src/prosody/util/xtemplate.tl
+++ b/teal-src/prosody/util/xtemplate.tl
@@ -54,7 +54,10 @@ local function render(template : string, root : st.stanza_t, escape : escape_t,
if tmpl then tmpl = s_sub(tmpl, 2, -2); end
if args then args = s_sub(args, 2, -2); end
- if func == "each" and tmpl and st.is_stanza(value) then
+ if func == "each" and tmpl then
+ if not st.is_stanza(value) then
+ return pre_blank..post_blank;
+ end
if not args then value, args = root, path; end
local ns, name = s_match(args, "^(%b{})(.*)$");
if ns then ns = s_sub(ns, 2, -2); else name, ns = args, nil; end
diff --git a/util/xtemplate.lua b/util/xtemplate.lua
index 56413012..e23b1a01 100644
--- a/util/xtemplate.lua
+++ b/util/xtemplate.lua
@@ -39,7 +39,8 @@ local function render(template, root, escape, filters)
if tmpl then tmpl = s_sub(tmpl, 2, -2); end
if args then args = s_sub(args, 2, -2); end
- if func == "each" and tmpl and st.is_stanza(value) then
+ if func == "each" and tmpl then
+ if not st.is_stanza(value) then return pre_blank .. post_blank end
if not args then value, args = root, path; end
local ns, name = s_match(args, "^(%b{})(.*)$");
if ns then