Skip to content

Commit

Permalink
Render false literal as “false”
Browse files Browse the repository at this point in the history
Fixes #827
  • Loading branch information
kpdecker committed Aug 26, 2014
1 parent 3c86986 commit 4f01f65
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/handlebars/compiler/javascript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ JavaScriptCompiler.prototype = {
// when we examine local
this.flushInline();
var local = this.popStack();
this.pushSource("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
this.pushSource('if (' + local + ' != null) { ' + this.appendToBuffer(local) + ' }');
if (this.environment.isSimple) {
this.pushSource("else { " + this.appendToBuffer("''") + " }");
}
Expand Down
4 changes: 3 additions & 1 deletion lib/handlebars/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ export function escapeExpression(string) {
// don't escape SafeStrings, since they're already safe
if (string instanceof SafeString) {
return string.toString();
} else if (!string && string !== 0) {
} else if (string == null) {
return "";
} else if (!string) {
return string + '';
}

// Force a string conversion as this will be done by the append regardless and
Expand Down
8 changes: 8 additions & 0 deletions spec/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ describe("basic context", function() {
shouldCompileTo("num: {{.}}", 0, "num: 0");
shouldCompileTo("num: {{num1/num2}}", {num1: {num2: 0}}, "num: 0");
});
it('false', function() {
shouldCompileTo('val1: {{val1}}, val2: {{val2}}', {val1: false, val2: new Boolean(false)}, 'val1: false, val2: false');
shouldCompileTo('val: {{.}}', false, 'val: false');
shouldCompileTo('val: {{val1/val2}}', {val1: {val2: false}}, 'val: false');

shouldCompileTo('val1: {{{val1}}}, val2: {{{val2}}}', {val1: false, val2: new Boolean(false)}, 'val1: false, val2: false');
shouldCompileTo('val: {{{val1/val2}}}', {val1: {val2: false}}, 'val: false');
});

it("newlines", function() {
shouldCompileTo("Alan's\nTest", {}, "Alan's\nTest");
Expand Down
2 changes: 1 addition & 1 deletion spec/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ describe('utils', function() {
equals(Handlebars.Utils.escapeExpression(''), '');
equals(Handlebars.Utils.escapeExpression(undefined), '');
equals(Handlebars.Utils.escapeExpression(null), '');
equals(Handlebars.Utils.escapeExpression(false), '');

equals(Handlebars.Utils.escapeExpression(false), 'false');
equals(Handlebars.Utils.escapeExpression(0), '0');
});
it('should handle empty objects', function() {
Expand Down

0 comments on commit 4f01f65

Please sign in to comment.