Skip to content

Commit

Permalink
Merge pull request #395 from bem/replace-position__issue-394
Browse files Browse the repository at this point in the history
BEMHTML: fix position with replace()
  • Loading branch information
miripiruni authored Dec 12, 2016
2 parents e3221e9 + b32094e commit 71cca07
Show file tree
Hide file tree
Showing 2 changed files with 68 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
65 changes: 65 additions & 0 deletions test/bemcontext-position-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,69 @@ 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>');
});


it('should calc position with appendContent()', function() {
test(function() {
block('a').appendContent()({ block: 'b', mix: 'added' });

block('b')(
match(function(self) { return self.isFirst(); })
.addMods()({ first: 'yes' }),

match(function(self) { return self.isLast(); })
.addMods()({ last: 'yes' }),

cls()(function() {
return 'p_' + this.position;
})
);
},
{ block: 'a', content: [ { block: 'b' }, { block: 'b' } ] },
'<div class="a">' +
'<div class="b b_first_yes p_1"></div>' +
'<div class="b p_2"></div>' +
'<div class="b b_last_yes added p_3"></div>' +
'</div>');
});

it('should calc position with prependContent()', function() {
test(function() {
block('a').prependContent()({ block: 'b', mix: 'added' });

block('b')(
match(function(self) { return self.isFirst(); })
.addMods()({ first: 'yes' }),

match(function(self) { return self.isLast(); })
.addMods()({ last: 'yes' }),

cls()(function() {
return 'p_' + this.position;
})
);
},
{ block: 'a', content: [ { block: 'b' }, { block: 'b' } ] },
'<div class="a">' +
'<div class="b b_first_yes added p_1"></div>' +
'<div class="b p_2"></div>' +
'<div class="b b_last_yes p_3"></div>' +
'</div>');
});
});

0 comments on commit 71cca07

Please sign in to comment.