From 7041b3a8695090820fd39b7c5807f3a92192c872 Mon Sep 17 00:00:00 2001 From: Alan Johnson Date: Tue, 12 Oct 2010 21:43:48 -0400 Subject: [PATCH] Got zero handling cleaned up a bit. --- lib/handlebars.js | 8 +++++--- test/handlebars.js | 7 +++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/handlebars.js b/lib/handlebars.js index 15f6c7c8..22137012 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -154,8 +154,10 @@ var Handlebars = { isEmpty: function(value) { if (typeof value === "undefined") { return true; - } else if (!value) { + } else if (value === null) { return true; + } else if (value === false) { + return true; } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length == 0) { return true; } else { @@ -355,9 +357,9 @@ Handlebars.Compiler.prototype = { if (depth > 0 || parts.length > 1) { return "(Handlebars.evalExpression('" + param + "', context, stack))"; } else if (parts.length == 1) { - return "(context['" + parts[0] + "'] || fallback['" + parts[0] + "'])"; + return "(!Handlebars.isEmpty(context['" + parts[0] + "']) ? context['" + parts[0] + "'] : fallback['" + parts[0] + "'])"; } else { - return "(context || fallback)"; + return "(!Handlebars.isEmpty(context) ? context : fallback)"; } }, diff --git a/test/handlebars.js b/test/handlebars.js index db9b778c..d329e4a1 100644 --- a/test/handlebars.js +++ b/test/handlebars.js @@ -40,6 +40,13 @@ test("boolean", function() { "booleans do not show the contents when false"); }); +test("zeros", function() { + shouldCompileTo("num1: {{num1}}, num2: {{num2}}", {num1: 42, num2: 0}, + "num1: 42, num2: 0"); + shouldCompileTo("num: {{.}}", 0, "num: 0"); + shouldCompileTo("num: {{num1/num2}}", {num1: {num2: 0}}, "num: 0"); +}); + test("newlines", function() { shouldCompileTo("Alan's\nTest", {}, "Alan's\nTest"); shouldCompileTo("Alan's\rTest", {}, "Alan's\rTest");