Skip to content

Commit

Permalink
Merge pull request #485 from bem/issue-484__wrap-bug
Browse files Browse the repository at this point in the history
bem-xjst: Wrap should save BEMContext
  • Loading branch information
miripiruni authored Oct 24, 2017
2 parents 81b9610 + 070701f commit 309a9d1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
11 changes: 11 additions & 0 deletions lib/bemxjst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ BEMXJST.prototype.compile = function(code) {
});
}

function _applyCtxWrap(ctx, changes) {
// Fast case
if (!changes)
return self.local({ ctx: ctx }, _applyCtx);

return self.local(changes, function() {
return self.local({ ctx: ctx }, applyCtx);
});
}

function apply(mode, changes) {
return self.applyMode(mode, changes);
}
Expand All @@ -91,6 +101,7 @@ BEMXJST.prototype.compile = function(code) {
var tree = new Tree({
refs: {
applyCtx: applyCtxWrap,
_applyCtx: _applyCtxWrap,
apply: apply
}
});
Expand Down
6 changes: 3 additions & 3 deletions lib/bemxjst/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ inherits(WrapMatch, MatchBase);
exports.WrapMatch = WrapMatch;

WrapMatch.prototype.wrapBody = function(body) {
var applyCtx = this.refs.applyCtx;
var _applyCtx = this.refs._applyCtx;

if (typeof body !== 'function') {
return function() {
return applyCtx(body);
return _applyCtx(body);
};
}

return function() {
return applyCtx(body.call(this, this, this.ctx));
return _applyCtx(body.call(this, this, this.ctx));
};
};

Expand Down
15 changes: 14 additions & 1 deletion test/modes-wrap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,25 @@ describe('Modes wrap', function() {
} ], '<div class="wrap"><a class="b1"></a></div>');
});

it('should protected from infinite loop', function () {
it('should protected from infinite loop', function() {
test(function() {
block('b1').wrap()(function() {
return { block: 'b2' };
});
block('b2').wrap()({ block: 'b1' });
}, { block: 'b1' }, '<div class="b1"></div>');
});


it('should use current context (with simple value)', function() {
test(function() {
block('page').wrap()([ { elem: 'head' } ]);
}, { block: 'page' }, '<div class="page__head"></div>');
});

it('should use current context (with function)', function() {
test(function() {
block('page').wrap()(function() { return [ { elem: 'head' } ]; });
}, { block: 'page' }, '<div class="page__head"></div>');
});
});

0 comments on commit 309a9d1

Please sign in to comment.