Got zero handling cleaned up a bit.
This commit is contained in:
+5
-3
@@ -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)";
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user