Skip to content

Commit

Permalink
Iterate only own properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ogonkov committed Jul 10, 2020
1 parent 4ebee6f commit ec6ef6d
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions nunjucks/src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,29 @@ function getAttrGetter(attribute, postprocess, defaultValue) {
return function attrGetter(item) {
let _item = item;

function rollback() {
_item = undefined;
if (defaultValue) {
_item = defaultValue;
}
}

for (let i = 0; i < parts.length; i++) {
const part = parts[i];

// If item is not an object, and we still got parts to handle, it means
// that something goes wrong. Just roll out to undefined or default value
// in that case.
try {
_item = _item[part];
} catch (e) {
_item = undefined;
if (defaultValue) {
_item = defaultValue;
if (hasOwnProp(_item, part)) {
_item = _item[part];
} else {
rollback();

break;
}
} catch (e) {
rollback();

break;
}
Expand Down

0 comments on commit ec6ef6d

Please sign in to comment.