From eaa27280826e62cd9eadbf199cc6b926f2822a82 Mon Sep 17 00:00:00 2001 From: miripiruni Date: Sat, 10 Dec 2016 15:55:33 +0300 Subject: [PATCH 1/2] BEMHTML: fix position with replace() (#394 fixed) --- lib/bemxjst/tree.js | 5 +++-- test/bemcontext-position-test.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) 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..bd9ac612 100644 --- a/test/bemcontext-position-test.js +++ b/test/bemcontext-position-test.js @@ -113,4 +113,20 @@ 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' } ], + '
' + + '
'); + }); }); From b32094e75fae60f284c6bc5ce830642d1eb09c68 Mon Sep 17 00:00:00 2001 From: miripiruni Date: Sat, 10 Dec 2016 16:19:34 +0300 Subject: [PATCH 2/2] Test: position with appendContent()/prependContent() --- test/bemcontext-position-test.js | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/bemcontext-position-test.js b/test/bemcontext-position-test.js index bd9ac612..75554ddb 100644 --- a/test/bemcontext-position-test.js +++ b/test/bemcontext-position-test.js @@ -129,4 +129,53 @@ describe('BEMContext this.position', function() { '
' + '
'); }); + + + 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' } ] }, + '
' + + '
' + + '
' + + '
' + + '
'); + }); });