Skip to content

Commit

Permalink
key lookup when not in root context with undefined/null value fails,
Browse files Browse the repository at this point in the history
fixes janl#397, fixes janl#409
  • Loading branch information
dasilvacontin committed Dec 19, 2014
1 parent a463adc commit 3a10fb2
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 3 deletions.
11 changes: 8 additions & 3 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 Down Expand Up @@ -368,11 +368,16 @@

while (value != null && index < names.length)
value = value[names[index++]];
} else if (typeof context.view == 'object') {
} else if (context.view != null && typeof context.view === 'object') {
value = context.view[name];
}

if (value != null)
// TO-DO: auxiliar function instead of this beautiful if
if (value !== undefined ||
(value === undefined &&
(context.view === undefined) || (
typeof context.view === 'object' &&
context.view.hasOwnProperty(name))))
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 3a10fb2

Please sign in to comment.