diff --git a/mustache.js b/mustache.js index deb0792..6528281 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 0000000..ae9b9bf --- /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 0000000..f3698da --- /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 0000000..9b7cde3 --- /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