From 5ab345eddf53f69d82019f2613daeced150c6b8a Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Fri, 31 Aug 2012 15:03:39 -0700 Subject: [PATCH] Use JavaScript's definition of falsy Fixes #186 --- mustache.js | 7 +++---- test/_files/falsy.js | 8 ++++++++ test/_files/falsy.mustache | 12 ++++++++++++ test/_files/falsy.txt | 12 ++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 test/_files/falsy.js create mode 100644 test/_files/falsy.mustache create mode 100644 test/_files/falsy.txt diff --git a/mustache.js b/mustache.js index deb079286..6528281ee 100644 --- a/mustache.js +++ b/mustache.js @@ -286,10 +286,9 @@ var Mustache; Renderer.prototype._inverted = function (name, context, callback) { var value = context.lookup(name); - // From the spec: inverted sections may render text once based on the - // inverse value of the key. That is, they will be rendered if the key - // doesn't exist, is false, or is an empty list. - if (value == null || value === false || (isArray(value) && value.length === 0)) { + // Use JavaScript's definition of falsy. Include empty arrays. + // See https://github.com/janl/mustache.js/issues/186 + if (!value || (isArray(value) && value.length === 0)) { return callback(context, this); } diff --git a/test/_files/falsy.js b/test/_files/falsy.js new file mode 100644 index 000000000..ae9b9bf25 --- /dev/null +++ b/test/_files/falsy.js @@ -0,0 +1,8 @@ +({ + "emptyString": "", + "emptyArray": [], + "zero": 0, + "null": null, + "undefined": undefined, + "NaN": 0/0 +}) \ No newline at end of file diff --git a/test/_files/falsy.mustache b/test/_files/falsy.mustache new file mode 100644 index 000000000..f3698da64 --- /dev/null +++ b/test/_files/falsy.mustache @@ -0,0 +1,12 @@ +{{#emptyString}}empty string{{/emptyString}} +{{^emptyString}}inverted empty string{{/emptyString}} +{{#emptyArray}}empty array{{/emptyArray}} +{{^emptyArray}}inverted empty array{{/emptyArray}} +{{#zero}}zero{{/zero}} +{{^zero}}inverted zero{{/zero}} +{{#null}}null{{/null}} +{{^null}}inverted null{{/null}} +{{#undefined}}undefined{{/undefined}} +{{^undefined}}inverted undefined{{/undefined}} +{{#NaN}}NaN{{/NaN}} +{{^NaN}}inverted NaN{{/NaN}} diff --git a/test/_files/falsy.txt b/test/_files/falsy.txt new file mode 100644 index 000000000..9b7cde39c --- /dev/null +++ b/test/_files/falsy.txt @@ -0,0 +1,12 @@ + +inverted empty string + +inverted empty array + +inverted zero + +inverted null + +inverted undefined + +inverted NaN