From 78ed7a0839c70f248d898e009ddb919c06a6bd00 Mon Sep 17 00:00:00 2001 From: "raymond.lam" Date: Wed, 4 Jan 2017 18:23:52 -0500 Subject: [PATCH] prevent value from being returned by Context.prototype.lookup if lookupHit is false --- mustache.js | 16 +++++++++------- test/_files/dot_notation.mustache | 1 + test/_files/dot_notation.txt | 1 + 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/mustache.js b/mustache.js index 413b6f2..56d6d6f 100644 --- a/mustache.js +++ b/mustache.js @@ -377,11 +377,11 @@ if (cache.hasOwnProperty(name)) { value = cache[name]; } else { - var context = this, names, index, lookupHit = false; + var context = this, intermediateValue, names, index, lookupHit = false; while (context) { if (name.indexOf('.') > 0) { - value = context.view; + intermediateValue = context.view; names = name.split('.'); index = 0; @@ -396,19 +396,21 @@ * This is specially necessary for when the value has been set to * `undefined` and we want to avoid looking up parent contexts. **/ - while (value != null && index < names.length) { + while (intermediateValue != null && index < names.length) { if (index === names.length - 1) - lookupHit = hasProperty(value, names[index]); + lookupHit = hasProperty(intermediateValue, names[index]); - value = value[names[index++]]; + intermediateValue = intermediateValue[names[index++]]; } } else { - value = context.view[name]; + intermediateValue = context.view[name]; lookupHit = hasProperty(context.view, name); } - if (lookupHit) + if (lookupHit) { + value = intermediateValue; break; + } context = context.parent; } diff --git a/test/_files/dot_notation.mustache b/test/_files/dot_notation.mustache index f89d70b..2640514 100644 --- a/test/_files/dot_notation.mustache +++ b/test/_files/dot_notation.mustache @@ -7,3 +7,4 @@

Test truthy false values:

Zero: {{truthy.zero}}

False: {{truthy.notTrue}}

+

length of string should not be rendered: {{price.currency.name.length}}

diff --git a/test/_files/dot_notation.txt b/test/_files/dot_notation.txt index 08afa05..eeb346d 100644 --- a/test/_files/dot_notation.txt +++ b/test/_files/dot_notation.txt @@ -7,3 +7,4 @@

Test truthy false values:

Zero: 0

False: false

+

length of string should not be rendered: