From 03cadf8e76489b5d40b4f6f3ba1a637fd0da7c63 Mon Sep 17 00:00:00 2001 From: Jason Davies Date: Sun, 19 Sep 2010 05:49:38 +0800 Subject: [PATCH] Fix exception when context value is empty string An exception was being thrown due to the empty string '' being treated as a non-existent context value. When the empty string was encountered as a nested value in the context it caused the fallback to be accessed instead, and due to the non-existent nesting property it threw an exception. --- lib/handlebars.js | 2 +- test/handlebars.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/handlebars.js b/lib/handlebars.js index 65466f76..fd08b5db 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -340,7 +340,7 @@ Handlebars.Compiler.prototype = { if (depth > 0) { return "( stack[stack.length - " + depth + "]" + paramExpr + ")"; } else { - return "( context" + paramExpr + " || fallback" + paramExpr + " )"; + return "( context" + paramExpr + " != null ? context" + paramExpr + " : fallback" + paramExpr + " )"; } }, diff --git a/test/handlebars.js b/test/handlebars.js index f0f6c16d..33e7ddd5 100644 --- a/test/handlebars.js +++ b/test/handlebars.js @@ -84,6 +84,8 @@ test("functions with context argument", function() { test("nested paths", function() { shouldCompileTo("Goodbye {{alan/expression}} world!", {alan: {expression: "beautiful"}}, "Goodbye beautiful world!", "Nested paths access nested objects"); + shouldCompileTo("Goodbye {{alan/expression}} world!", {alan: {expression: ""}}, + "Goodbye world!", "Nested paths access nested objects with empty string"); }); test("bad idea nested paths", function() {