Skip to content

Commit

Permalink
Merge pull request #592 from bem/bemhtml-reentrant-bemcontext@support…
Browse files Browse the repository at this point in the history
…/2.x

bemhtml: re-entrant BEMContext
  • Loading branch information
tadatuta committed Dec 25, 2014
2 parents 2570b00 + efc0778 commit 1ab3de1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions blocks-common/i-bem/__html/i-bem__html.bemhtml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ function BEMContext(apply_) {
this._start = true;
this._listLength = 0;
this._notNewList = false;
this._inside = false;
this.position = 0;
};

Expand Down Expand Up @@ -266,11 +267,22 @@ BEMContext.prototype.generateId = function generateId() {
return this.identify(this.ctx);
};

function slowApply() {
var ctx = new BEMContext(apply);
ctx.reinit(this);
ctx.apply();
return ctx._buf.join('');
};

// Wrap xjst's apply and export our own
var ctx = new BEMContext(apply);
exports.apply = BEMContext.apply = function _apply() {
if (ctx._inside) return slowApply.call(this);

ctx._inside = true;
ctx.reinit(this);
ctx.apply();
ctx._inside = false;
return ctx._buf.join('');
};
}();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
block b1, content: {
return this.invalid || 'ok';
}

block b2, content: {
local(this.invalid = 'bye') {
throw new Error('Oh god');
}
}
8 changes: 8 additions & 0 deletions blocks-common/i-bem/__html/test/i-bem-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@ suite('i-bem block and others', function() {
unit('applyNext in content regression #289', 'gh-289');
unit('mods redefinition #550', 'gh-550');
unit('block with escaping this', 'escaping-this');

test('re-entrance', function() {
var t = bemhtml.compile(iBem + readFile('i-bem/re-entrant.bemhtml'));
assert.throws(function() {
t.call({ block: 'b2' });
});
assert.equal(t.call({ block: 'b1' }), '<div class="b1">ok</div>');
});
});

0 comments on commit 1ab3de1

Please sign in to comment.