Skip to content

Commit

Permalink
BEMHTML: fix position with replace() (#394 fixed)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Dec 10, 2016
1 parent e3221e9 commit eaa2728
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/bemxjst/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ ReplaceMatch.prototype.wrapBody = function wrapBody(body) {

if (typeof body !== 'function') {
return function inlineAdaptor() {
return applyCtx(body);
return applyCtx(body, { position: this.position - 1 });
};
}

return function replaceAdaptor() {
return applyCtx(body.call(this, this, this.ctx));
return applyCtx(body.call(this, this, this.ctx),
{ position: this.position - 1 });
};
};

Expand Down
16 changes: 16 additions & 0 deletions test/bemcontext-position-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,20 @@ describe('BEMContext this.position', function() {
{ block: 'a1', content: { block: 'a2', content: { block: 'a3' } } },
'<div class="a1 1"><div class="a2 1"><div class="a3 1"></div></div></div>');
});

it('should calc position with replace()', function() {
test(function() {
block('a').replace()({ block: 'b' });
block('b')
.match(function(self) { return self.isFirst(); })
.addMods()({ first: 'yes' });

block('b')
.match(function(self) { return self.isLast(); })
.addMods()({ last: 'yes' });
},
[ { block: 'a' }, { block: 'a' }, { block: 'a' } ],
'<div class="b b_first_yes"></div><div class="b"></div>' +
'<div class="b b_last_yes"></div>');
});
});

0 comments on commit eaa2728

Please sign in to comment.