summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2015-08-04 12:40:33 -0500
committerkpdecker <kpdecker@gmail.com>2015-08-04 12:40:33 -0500
commit06d515a89d18b50805a5fe4eec8f1156bbe92d45 (patch)
treeec7902657fbfe262ab0792364a894411176abb72
parent0de8dac702f7b06161a0c5464a80bd327d708258 (diff)
downloadhandlebars.js-06d515a89d18b50805a5fe4eec8f1156bbe92d45.zip
handlebars.js-06d515a89d18b50805a5fe4eec8f1156bbe92d45.tar.gz
handlebars.js-06d515a89d18b50805a5fe4eec8f1156bbe92d45.tar.bz2
Ignore empty when iterating on sparse arrays
Fixes #1065
-rw-r--r--lib/handlebars/helpers/each.js6
-rw-r--r--spec/regressions.js7
2 files changed, 13 insertions, 0 deletions
diff --git a/lib/handlebars/helpers/each.js b/lib/handlebars/helpers/each.js
index 9fc5a09..d39a300 100644
--- a/lib/handlebars/helpers/each.js
+++ b/lib/handlebars/helpers/each.js
@@ -25,6 +25,12 @@ export default function(instance) {
}
function execIteration(field, index, last) {
+ // Don't iterate over undefined values since we can't execute blocks against them
+ // in non-strict (js) mode.
+ if (context[field] == null) {
+ return;
+ }
+
if (data) {
data.key = field;
data.index = index;
diff --git a/spec/regressions.js b/spec/regressions.js
index 009fec9..e8942a4 100644
--- a/spec/regressions.js
+++ b/spec/regressions.js
@@ -196,4 +196,11 @@ describe('Regressions', function() {
shouldCompileToWithPartials(root, [{}, helpers, partials], true, '<partial>');
});
+
+ it('GH-1065: Sparse arrays', function() {
+ var array = [];
+ array[1] = 'foo';
+ array[3] = 'bar';
+ shouldCompileTo('{{#each array}}{{@index}}{{.}}{{/each}}', {array: array}, '1foo3bar');
+ });
});