From b5985fc80a1a499b930783614f6a3dd0a5596292 Mon Sep 17 00:00:00 2001 From: Theodore Brown Date: Sat, 6 Jun 2026 23:16:23 -0500 Subject: [PATCH] Fix strict mode .length lookup on string primitives Resolves #2164 --- lib/handlebars/runtime.js | 2 +- spec/strict.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index 6aef63bf..869c4f86 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -109,7 +109,7 @@ 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, }); diff --git a/spec/strict.js b/spec/strict.js index bfcb0a9a..9bfb7f1d 100644 --- a/spec/strict.js +++ b/spec/strict.js @@ -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({