Skip to content

Commit

Permalink
Merge pull request #407 from dasilvacontin/issue/397
Browse files Browse the repository at this point in the history
fixing null and undefined lookup
  • Loading branch information
dasilvacontin committed Mar 26, 2015
2 parents 8c25ec5 + a020430 commit 00fedc5
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 5 deletions.
26 changes: 21 additions & 5 deletions mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
* maintaining a reference to the parent context.
*/
function Context(view, parentContext) {
this.view = view == null ? {} : view;
this.view = view;
this.cache = { '.': this.view };
this.parent = parentContext;
}
Expand All @@ -358,21 +358,37 @@
if (name in cache) {
value = cache[name];
} else {
var context = this, names, index;
var context = this, names, index, lookupHit = false;

while (context) {
if (name.indexOf('.') > 0) {
value = context.view;
names = name.split('.');
index = 0;

while (value != null && index < names.length)
/**
* Using the dot notion path in `name`, we descend through the
* nested objects.
*
* To be certain that the lookup has been successful, we have to
* check if the last object in the path actually has the property
* we are looking for. We store the result in `lookupHit`.
*
* 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) {
if (index === names.length - 1 && value != null)
lookupHit = (typeof value === 'object') &&
value.hasOwnProperty(names[index]);
value = value[names[index++]];
} else if (typeof context.view == 'object') {
}
} else if (context.view != null && typeof context.view === 'object') {
value = context.view[name];
lookupHit = context.view.hasOwnProperty(name);
}

if (value != null)
if (lookupHit)
break;

context = context.parent;
Expand Down
10 changes: 10 additions & 0 deletions test/_files/falsy_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
({
"list": [
["", "emptyString"],
[[], "emptyArray"],
[0, "zero"],
[null, "null"],
[undefined, "undefined"],
[0/0, "NaN"]
]
})
3 changes: 3 additions & 0 deletions test/_files/falsy_array.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#list}}
{{#.}}{{#.}}{{.}}{{/.}}{{^.}}inverted {{/.}}{{/.}}
{{/list}}
6 changes: 6 additions & 0 deletions test/_files/falsy_array.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
inverted emptyString
inverted emptyArray
inverted zero
inverted null
inverted undefined
inverted NaN
9 changes: 9 additions & 0 deletions test/_files/null_lookup_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
({
"name": "David",
"twitter": "@dasilvacontin",
"farray": [
["Flor", "@florrts"],
["Miquel", null],
["Chris", undefined]
]
})
3 changes: 3 additions & 0 deletions test/_files/null_lookup_array.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#farray}}
{{#.}}{{#.}}{{.}} {{/.}}{{^.}}no twitter{{/.}}{{/.}}
{{/farray}}
3 changes: 3 additions & 0 deletions test/_files/null_lookup_array.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flor @florrts
Miquel no twitter
Chris no twitter
18 changes: 18 additions & 0 deletions test/_files/null_lookup_object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
({
"name": "David",
"twitter": "@dasilvacontin",
"fobject": [
{
"name": "Flor",
"twitter": "@florrts"
},
{
"name": "Miquel",
"twitter": null
},
{
"name": "Chris",
"twitter": undefined
}
]
})
3 changes: 3 additions & 0 deletions test/_files/null_lookup_object.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#fobject}}
{{name}}'s twitter: {{#twitter}}{{.}}{{/twitter}}{{^twitter}}unknown{{/twitter}}.
{{/fobject}}
3 changes: 3 additions & 0 deletions test/_files/null_lookup_object.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flor's twitter: @florrts.
Miquel's twitter: unknown.
Chris's twitter: unknown.

0 comments on commit 00fedc5

Please sign in to comment.