From 2af727596816cc2b87c46759bda49d5b5c7720b7 Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Mon, 11 Apr 2022 12:21:23 +0700 Subject: [PATCH] Fix template condition evaluating to true for empty string from function Fixes #2529 --- chrome/content/zotero/xpcom/utilities_internal.js | 11 ++++++++++- test/tests/utilities_internalTest.js | 7 ++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index de9c2b45a0..9abbe61a2f 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -2230,11 +2230,20 @@ Zotero.Utilities.Internal = { if (!level.executed) { level.condition = level.parentCondition && ( args[2] + // If string variable is equal to the provided string ? vars[args[0]].toLowerCase() == args[2].toLowerCase() : ( Array.isArray(vars[args[0]]) + // Is array non empty ? !!vars[args[0]].length - : !!vars[args[0]] + : ( + typeof vars[args[0]] === 'function' + // If function returns a value (only string is supported) + // Note: To keep things simple, this doesn't support function attributes + ? !!vars[args[0]]() + // If string variable exists + : !!vars[args[0]] + ) ) ); level.executed = level.condition; diff --git a/test/tests/utilities_internalTest.js b/test/tests/utilities_internalTest.js index 1e5028b4ad..d6e44e92ed 100644 --- a/test/tests/utilities_internalTest.js +++ b/test/tests/utilities_internalTest.js @@ -528,13 +528,14 @@ describe("Zotero.Utilities.Internal", function () { var vars = { v1: '1', v2: (pars) => pars.a1 + pars.a2 + pars.a3, - v3: () => undefined, + v3: () => '', + v5: () => 'something', ar1: [], ar2: [1, 2] }; - var template = `{{ v1}}{{v2 a1= 1 a2 =' 2' a3 = "3 "}}{{v3}}{{v4}}{{if ar1}}ar1{{endif}}{{if ar2}}{{ar2}}{{endif}}`; + var template = `{{ v1}}{{v2 a1= 1 a2 =' 2' a3 = "3 "}}{{v3}}{{v4}}{{if ar1}}ar1{{endif}}{{if ar2}}{{ar2}}{{endif}}{{if v5}}yes{{endif}}{{if v3}}no{{endif}}`; var html = Zotero.Utilities.Internal.generateHTMLFromTemplate(template, vars); - assert.equal(html, '11 23 1,2'); + assert.equal(html, '11 23 1,2yes'); }); it("should support nested 'if' statements", function () {