diff --git a/mustache.js b/mustache.js index 4fab12e..3cd0a13 100644 --- a/mustache.js +++ b/mustache.js @@ -29,6 +29,14 @@ return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&'); } + /** + * Null safe way of checking whether or not an object, + * including its prototype, has a given property + */ + function hasProperty (obj, propName) { + return obj != null && typeof obj === 'object' && (propName in obj); + } + // Workaround for https://issues.apache.org/jira/browse/COUCHDB-577 // See https://github.com/janl/mustache.js/issues/189 var regExpTest = RegExp.prototype.test; @@ -379,14 +387,14 @@ * `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[names[index]]; + if (index === names.length - 1) + lookupHit = hasProperty(value, names[index]); value = value[names[index++]]; } - } else if (context.view != null && typeof context.view === 'object') { + } else { value = context.view[name]; - lookupHit = context.view.hasOwnProperty(name) || Object.prototype[name] !== value; + lookupHit = hasProperty(context.view, name); } if (lookupHit) diff --git a/test/render-helper.js b/test/render-helper.js index aa39951..11e73c9 100644 --- a/test/render-helper.js +++ b/test/render-helper.js @@ -26,12 +26,12 @@ function getPartial(testName) { } // You can put the name of a specific test to run in the TEST environment -// variable (e.g. TEST=backslashes vows test/render-test.js) +// variable (e.g. TEST=backslashes mocha test/render-test.js) var testToRun = process.env.TEST; var testNames; if (testToRun) { - testNames = [testToRun]; + testNames = testToRun.split(','); } else { testNames = fs.readdirSync(_files).filter(function (file) { return (/\.js$/).test(file);