diff --git a/mustache.js b/mustache.js index dbc982319..94aa90c6e 100644 --- a/mustache.js +++ b/mustache.js @@ -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; } @@ -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; diff --git a/test/_files/falsy_array.js b/test/_files/falsy_array.js new file mode 100644 index 000000000..f26530878 --- /dev/null +++ b/test/_files/falsy_array.js @@ -0,0 +1,10 @@ +({ + "list": [ + ["", "emptyString"], + [[], "emptyArray"], + [0, "zero"], + [null, "null"], + [undefined, "undefined"], + [0/0, "NaN"] + ] +}) \ No newline at end of file diff --git a/test/_files/falsy_array.mustache b/test/_files/falsy_array.mustache new file mode 100644 index 000000000..2be7b375c --- /dev/null +++ b/test/_files/falsy_array.mustache @@ -0,0 +1,3 @@ +{{#list}} +{{#.}}{{#.}}{{.}}{{/.}}{{^.}}inverted {{/.}}{{/.}} +{{/list}} \ No newline at end of file diff --git a/test/_files/falsy_array.txt b/test/_files/falsy_array.txt new file mode 100644 index 000000000..6f2452995 --- /dev/null +++ b/test/_files/falsy_array.txt @@ -0,0 +1,6 @@ +inverted emptyString +inverted emptyArray +inverted zero +inverted null +inverted undefined +inverted NaN diff --git a/test/_files/null_lookup_array.js b/test/_files/null_lookup_array.js new file mode 100644 index 000000000..9ebf56a22 --- /dev/null +++ b/test/_files/null_lookup_array.js @@ -0,0 +1,9 @@ +({ + "name": "David", + "twitter": "@dasilvacontin", + "farray": [ + ["Flor", "@florrts"], + ["Miquel", null], + ["Chris", undefined] + ] +}) diff --git a/test/_files/null_lookup_array.mustache b/test/_files/null_lookup_array.mustache new file mode 100644 index 000000000..0543895aa --- /dev/null +++ b/test/_files/null_lookup_array.mustache @@ -0,0 +1,3 @@ +{{#farray}} +{{#.}}{{#.}}{{.}} {{/.}}{{^.}}no twitter{{/.}}{{/.}} +{{/farray}} diff --git a/test/_files/null_lookup_array.txt b/test/_files/null_lookup_array.txt new file mode 100644 index 000000000..94edf99b0 --- /dev/null +++ b/test/_files/null_lookup_array.txt @@ -0,0 +1,3 @@ +Flor @florrts +Miquel no twitter +Chris no twitter diff --git a/test/_files/null_lookup_object.js b/test/_files/null_lookup_object.js new file mode 100644 index 000000000..2f758a545 --- /dev/null +++ b/test/_files/null_lookup_object.js @@ -0,0 +1,18 @@ +({ + "name": "David", + "twitter": "@dasilvacontin", + "fobject": [ + { + "name": "Flor", + "twitter": "@florrts" + }, + { + "name": "Miquel", + "twitter": null + }, + { + "name": "Chris", + "twitter": undefined + } + ] +}) diff --git a/test/_files/null_lookup_object.mustache b/test/_files/null_lookup_object.mustache new file mode 100644 index 000000000..e709ae44b --- /dev/null +++ b/test/_files/null_lookup_object.mustache @@ -0,0 +1,3 @@ +{{#fobject}} +{{name}}'s twitter: {{#twitter}}{{.}}{{/twitter}}{{^twitter}}unknown{{/twitter}}. +{{/fobject}} diff --git a/test/_files/null_lookup_object.txt b/test/_files/null_lookup_object.txt new file mode 100644 index 000000000..c1c727bf7 --- /dev/null +++ b/test/_files/null_lookup_object.txt @@ -0,0 +1,3 @@ +Flor's twitter: @florrts. +Miquel's twitter: unknown. +Chris's twitter: unknown.