Skip to content

Commit

Permalink
Merge pull request #278 from thec0keman/fix-for-attr-bracket-syntax
Browse files Browse the repository at this point in the history
Added detection for empty block payloads
  • Loading branch information
John Ratcliff committed May 30, 2016
2 parents 322cc5e + b656081 commit 4809cbf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion lib/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@
});
}

function isArray(obj) {
return obj && obj.constructor === Array;
}

// Receives an array object and verifies it has content
// Useful for checking blocks to make sure there is actual data in the payload
function isArrayWithContent(obj) {
if (!isArray(obj))
return;

var hasItems = false;
var length = obj.length;

for (var i = 0; i < length; i++) {
var item = obj[i];

if (isArray(item)) {
if (item.length > 0)
hasItems = true;
} else if (!!item) {
hasItems = true;
}
}

return hasItems;
}

function createBlockOrMustache(mustacheTuple) {
var mustache = mustacheTuple[0];
var block = mustacheTuple[1] || {};
Expand Down Expand Up @@ -90,7 +117,7 @@
mustacheContent = 'if ' + mustacheContent;
}

if (blockTuple && blockTuple.length > 0) {
if (isArrayWithContent(blockTuple)) {
var block = builder.generateBlock(mustacheContent, escaped);
builder.enter(block);

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/mustaches-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ test('bracketed nested block params', function(){
' \'foo\'',
' something="false" ] as |foo|');
compilesTo(
emblem, '{{#sally \'foo\' something="false" as |foo|}}{{/sally}}');
emblem, '{{sally \'foo\' something="false" as |foo|}}');
});

test('bracketed with block params and block', function(){
Expand Down

0 comments on commit 4809cbf

Please sign in to comment.