Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,18 @@ export function template(templateSpec, env) {
// Just add water
let container = {
strict: function (obj, name, loc) {
if (!obj || !(name in obj)) {
if (obj == null || !(name in Object(obj))) {
throw new Exception('"' + name + '" not defined in ' + obj, {
loc: loc,
});
}
return container.lookupProperty(obj, name);
},
strictLookup: function (depths, name, loc) {
const len = depths.length;
let depth;
for (let i = 0; i < len; i++) {
const d = depths[i];
if (
d &&
(typeof d === 'object' || typeof d === 'function') &&
name in d
) {
depth = d;
break;
}
}
return container.strict(depth, name, loc);
const result = container.lookup(depths, name);
return result !== undefined
? result
: container.strict(undefined, name, loc);
},
lookupProperty: function (parent, propertyName) {
if (Utils.isMap(parent)) {
Expand All @@ -153,9 +143,17 @@ export function template(templateSpec, env) {
lookup: function (depths, name) {
const len = depths.length;
for (let i = 0; i < len; i++) {
let result = depths[i] && container.lookupProperty(depths[i], name);
if (result != null) {
return depths[i][name];
const d = depths[i];
if (d == null) continue;
if (typeof d === 'object' || typeof d === 'function') {
if (Utils.isMap(d) ? d.has(name) : name in d) {
return container.lookupProperty(d, name);
}
} else {
const result = container.lookupProperty(d, name);
if (result != null) {
return result;
}
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions spec/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,18 @@ describe('basic context', function () {
.toCompileTo('Goodbye beautiful world!');
});

it('compat mode resolves Map values through depthed lookup', function () {
expectTemplate('{{#with nested}}{{value}}/{{constructor}}{{/with}}')
.withInput({
nested: new Map([
['value', 'map-value'],
['constructor', 'map-constructor'],
]),
})
.withCompileOptions({ compat: true })
.toCompileTo('map-value/map-constructor');
});

it('nested paths with empty string value', function () {
expectTemplate('Goodbye {{alan/expression}} world!')
.withInput({ alan: { expression: '' } })
Expand Down
7 changes: 7 additions & 0 deletions spec/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ describe('blocks', function () {
});

describe('compat mode', function () {
it('should directly return an explicitly null property', function () {
expectTemplate('{{#each items}}{{name}}{{/each}}')
.withCompileOptions({ compat: true })
.withInput({ name: 'root', items: [{ name: null }] })
.toCompileTo('');
});

it('block with deep recursive lookup lookup', function () {
expectTemplate(
'{{#outer}}Goodbye {{#inner}}cruel {{omg}}{{/inner}}{{/outer}}'
Expand Down
10 changes: 10 additions & 0 deletions spec/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ describe('security issues', function () {
})
.toCompileTo('abc');
});

it('should not cause recursive lookup if allowed through options(in "compat+strict" mode)', function () {
expectTemplate('{{#aString}}{{trim}}{{/aString}}')
.withInput({ aString: ' abc ', trim: 'trim' })
.withCompileOptions({ compat: true, strict: true })
.withRuntimeOptions({
allowedProtoMethods: { trim: true },
})
.toCompileTo('abc');
});
});

describe('control access to prototype non-methods via "allowedProtoProperties" and "allowProtoPropertiesByDefault', function () {
Expand Down
21 changes: 20 additions & 1 deletion spec/strict.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ describe('strict', function () {
.toCompileTo('');
});

it('should handle array length', function () {
expectTemplate('{{hello.length}}')
.withCompileOptions({ strict: true })
.withInput({ hello: [1, 2, 3] })
.toCompileTo('3');
});

it('should handle string length', function () {
expectTemplate('{{hello.length}}')
.withCompileOptions({ strict: true })
.withInput({ hello: 'world' })
.toCompileTo('5');

expectTemplate('{{hello.length}}')
.withCompileOptions({ strict: true })
.withInput({ hello: '' })
.toCompileTo('0');
});

it('should error on missing property lookup in known helpers mode', function () {
expectTemplate('{{hello}}')
.withCompileOptions({
Expand Down Expand Up @@ -166,7 +185,7 @@ describe('strict', function () {
it('should still perform recursive lookup with a multi-part path not in context', function () {
expectTemplate('{{#with child}}{{name.first}}{{/with}}')
.withCompileOptions({ strict: true, compat: true })
.withInput({ name: { first: 'root' }, child: { name: null } })
.withInput({ name: { first: 'root' }, child: { x: 'y' } })
.toCompileTo('root');
});

Expand Down