diff --git a/lib/bemxjst/tree.js b/lib/bemxjst/tree.js
index 32fa6f95..a2ecacb2 100644
--- a/lib/bemxjst/tree.js
+++ b/lib/bemxjst/tree.js
@@ -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 });
};
};
diff --git a/test/bemcontext-position-test.js b/test/bemcontext-position-test.js
index a2e2ad12..75554ddb 100644
--- a/test/bemcontext-position-test.js
+++ b/test/bemcontext-position-test.js
@@ -113,4 +113,69 @@ describe('BEMContext this.position', function() {
{ block: 'a1', content: { block: 'a2', content: { block: 'a3' } } },
'
');
});
+
+ 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' } ],
+ '' +
+ '');
+ });
+
+
+ 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' } ] },
+ '' +
+ '
' +
+ '
' +
+ '
' +
+ '
');
+ });
+
+ 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' } ] },
+ '' +
+ '
' +
+ '
' +
+ '
' +
+ '
');
+ });
});