Selaa lähdekoodia

Bugfix for null/undefined when using dot notation

tags/v2.1.1
Phillip Johnsen 11 vuotta sitten
vanhempi
commit
8adacd1641
2 muutettua tiedostoa jossa 14 lisäystä ja 6 poistoa
  1. +12
    -4
      mustache.js
  2. +2
    -2
      test/render-helper.js

+ 12
- 4
mustache.js Näytä tiedosto

@@ -29,6 +29,14 @@
return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&'); 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 // Workaround for https://issues.apache.org/jira/browse/COUCHDB-577
// See https://github.com/janl/mustache.js/issues/189 // See https://github.com/janl/mustache.js/issues/189
var regExpTest = RegExp.prototype.test; var regExpTest = RegExp.prototype.test;
@@ -379,14 +387,14 @@
* `undefined` and we want to avoid looking up parent contexts. * `undefined` and we want to avoid looking up parent contexts.
**/ **/
while (value != null && index < names.length) { 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++]]; value = value[names[index++]];
} }
} else if (context.view != null && typeof context.view === 'object') {
} else {
value = context.view[name]; value = context.view[name];
lookupHit = context.view.hasOwnProperty(name) || Object.prototype[name] !== value;
lookupHit = hasProperty(context.view, name);
} }


if (lookupHit) if (lookupHit)


+ 2
- 2
test/render-helper.js Näytä tiedosto

@@ -26,12 +26,12 @@ function getPartial(testName) {
} }


// You can put the name of a specific test to run in the TEST environment // 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 testToRun = process.env.TEST;


var testNames; var testNames;
if (testToRun) { if (testToRun) {
testNames = [testToRun];
testNames = testToRun.split(',');
} else { } else {
testNames = fs.readdirSync(_files).filter(function (file) { testNames = fs.readdirSync(_files).filter(function (file) {
return (/\.js$/).test(file); return (/\.js$/).test(file);


Loading…
Peruuta
Tallenna