From b257bc6b4b5b90ec112af183e52399905b157396 Mon Sep 17 00:00:00 2001 From: Brad Greenlee Date: Tue, 1 May 2012 16:58:38 -0700 Subject: [PATCH] Treat an empty string (or for that matter, any value that evaluates to false) as falsey in inverted sections. Fixes issue #186 (https://github.com/janl/mustache.js/issues/186) --- mustache.js | 2 +- spec/_files/inverted_section.js | 3 ++- spec/_files/inverted_section.mustache | 1 + spec/_files/inverted_section.txt | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/mustache.js b/mustache.js index 641cebd57..38dab6a10 100644 --- a/mustache.js +++ b/mustache.js @@ -175,7 +175,7 @@ var Mustache = (typeof module !== "undefined" && module.exports) || {}; // 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)) { + if (!value || (isArray(value) && value.length === 0)) { buffer += callback(); } } else if (isArray(value)) { diff --git a/spec/_files/inverted_section.js b/spec/_files/inverted_section.js index 2e07fc3a2..0bf7f4c06 100644 --- a/spec/_files/inverted_section.js +++ b/spec/_files/inverted_section.js @@ -1,3 +1,4 @@ var inverted_section = { - "repos": [] + "repos": [], + "empty_string": "" }; diff --git a/spec/_files/inverted_section.mustache b/spec/_files/inverted_section.mustache index b0a183b10..348e84c3a 100644 --- a/spec/_files/inverted_section.mustache +++ b/spec/_files/inverted_section.mustache @@ -1,3 +1,4 @@ {{#repos}}{{name}}{{/repos}} {{^repos}}No repos :({{/repos}} {{^nothin}}Hello!{{/nothin}} +{{^empty_string}}Empty string should be falsey{{/empty_string}} \ No newline at end of file diff --git a/spec/_files/inverted_section.txt b/spec/_files/inverted_section.txt index b421582e7..80f50a8da 100644 --- a/spec/_files/inverted_section.txt +++ b/spec/_files/inverted_section.txt @@ -1,3 +1,4 @@ No repos :( Hello! +Empty string should be falsey \ No newline at end of file