diff --git a/mustache.js b/mustache.js index 069c490..e45861f 100644 --- a/mustache.js +++ b/mustache.js @@ -42,9 +42,19 @@ * including its prototype, has a given property */ function hasProperty (obj, propName) { + return obj != null && typeof obj === 'object' && (propName in obj); + } + + /** + * Safe way of detecting whether or not the given thing is a primitive and + * whether it has the given property + */ + function primitiveHasOwnProperty (primitive, propName) { return ( - (obj != null && typeof obj === 'object' && (propName in obj)) - || (obj !== undefined && obj.hasOwnProperty(propName)) + typeof primitive !== 'object' + && primitive !== undefined + && primitive.hasOwnProperty + && primitive.hasOwnProperty(propName) ); } @@ -401,7 +411,10 @@ **/ while (intermediateValue != null && index < names.length) { if (index === names.length - 1) - lookupHit = hasProperty(intermediateValue, names[index]); + lookupHit = ( + hasProperty(intermediateValue, names[index]) + || primitiveHasOwnProperty(intermediateValue, names[index]) + ); intermediateValue = intermediateValue[names[index++]]; } diff --git a/test/_files/dot_notation.mustache b/test/_files/dot_notation.mustache index 8b6b2e9..f64e350 100644 --- a/test/_files/dot_notation.mustache +++ b/test/_files/dot_notation.mustache @@ -7,5 +7,5 @@

Test truthy false values:

Zero: {{truthy.zero}}

False: {{truthy.notTrue}}

-

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

+

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

length of an array should be rendered: {{authors.length}}

diff --git a/test/_files/dot_notation.txt b/test/_files/dot_notation.txt index 98d2e6a..f982a75 100644 --- a/test/_files/dot_notation.txt +++ b/test/_files/dot_notation.txt @@ -7,5 +7,5 @@

Test truthy false values:

Zero: 0

False: false

-

length of string should not be rendered:

+

length of string should be rendered: 3

length of an array should be rendered: 2