Skip to content

Commit db06ebb

Browse files
authored
Fix strict mode .length lookup on string primitives (#2165)
Resolves #2164
1 parent ad6397a commit db06ebb

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

lib/handlebars/runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export function template(templateSpec, env) {
109109
// Just add water
110110
let container = {
111111
strict: function (obj, name, loc) {
112-
if (!obj || !(name in obj)) {
112+
if (obj == null || !(name in Object(obj))) {
113113
throw new Exception('"' + name + '" not defined in ' + obj, {
114114
loc: loc,
115115
});

spec/strict.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ describe('strict', function () {
2727
.toCompileTo('');
2828
});
2929

30+
it('should handle array length', function () {
31+
expectTemplate('{{hello.length}}')
32+
.withCompileOptions({ strict: true })
33+
.withInput({ hello: [1, 2, 3] })
34+
.toCompileTo('3');
35+
});
36+
37+
it('should handle string length', function () {
38+
expectTemplate('{{hello.length}}')
39+
.withCompileOptions({ strict: true })
40+
.withInput({ hello: 'world' })
41+
.toCompileTo('5');
42+
43+
expectTemplate('{{hello.length}}')
44+
.withCompileOptions({ strict: true })
45+
.withInput({ hello: '' })
46+
.toCompileTo('0');
47+
});
48+
3049
it('should error on missing property lookup in known helpers mode', function () {
3150
expectTemplate('{{hello}}')
3251
.withCompileOptions({

0 commit comments

Comments
 (0)