# HG changeset patch # User Kim Alvefur # Date 1720703069 -7200 # Node ID 1f93e4f78c53218e23a2468dc91f25b6be1911d6 # Parent aefbc76ac8d2192829db28059c1bcafe308f6c01 util.xtemplate: Fix error on applying each() to zero stanzas diff -r aefbc76ac8d2 -r 1f93e4f78c53 spec/util_xtemplate_spec.lua --- a/spec/util_xtemplate_spec.lua Sun Jul 07 18:40:25 2024 +0200 +++ b/spec/util_xtemplate_spec.lua Thu Jul 11 15:04:29 2024 +0200 @@ -38,6 +38,10 @@ 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 -r aefbc76ac8d2 -r 1f93e4f78c53 teal-src/prosody/util/xtemplate.tl --- a/teal-src/prosody/util/xtemplate.tl Sun Jul 07 18:40:25 2024 +0200 +++ b/teal-src/prosody/util/xtemplate.tl Thu Jul 11 15:04:29 2024 +0200 @@ -54,7 +54,10 @@ 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 -r aefbc76ac8d2 -r 1f93e4f78c53 util/xtemplate.lua --- a/util/xtemplate.lua Sun Jul 07 18:40:25 2024 +0200 +++ b/util/xtemplate.lua Thu Jul 11 15:04:29 2024 +0200 @@ -39,7 +39,8 @@ 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