Got zero handling cleaned up a bit.

This commit is contained in:
Alan Johnson
2010-10-12 21:43:48 -04:00
parent f436c1d440
commit 7041b3a869
2 changed files with 12 additions and 3 deletions
+5 -3
View File
@@ -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)";
}
},
+7
View File
@@ -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");