diff --git a/nunjucks/src/lib.js b/nunjucks/src/lib.js index 6fda6eae..dec92068 100644 --- a/nunjucks/src/lib.js +++ b/nunjucks/src/lib.js @@ -196,6 +196,13 @@ 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]; @@ -203,12 +210,15 @@ function getAttrGetter(attribute, postprocess, defaultValue) { // 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; }