Skip to content

Commit

Permalink
bem-xjst: introduce this.stack() and stack flag (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Nov 6, 2017
1 parent ba102b7 commit 2d5804e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/bemxjst/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ function Context(bemxjst) {
// Save current block until the next BEM entity
this._currBlock = '';

this.stackTrace = [];

this.elem = null;
this.mods = {};
this.elemMods = {};
Expand Down Expand Up @@ -57,3 +59,7 @@ Context.prototype.generateId = function() {
Context.prototype.reapply = function(ctx) {
return this._bemxjst.run(ctx);
};

Context.prototype.trace = function() {
return this.stackTrace;
};
11 changes: 11 additions & 0 deletions lib/bemxjst/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ Match.prototype.exec = function(context) {
this.thrownError = null;

var out;

if (this.bemxjst.options.trace) {
context.stackTrace.push({
mode: this.modeName,
block: context.block,
elem: context.elem,
mods: context.mods,
elemMods: context.elemMods
});
}

if (typeof template.body === 'function')
out = this.tryCatch(template.body, context);
else
Expand Down
30 changes: 30 additions & 0 deletions test/bemcontext-trace-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var fixtures = require('./fixtures')('bemhtml');
var test = fixtures.test;

describe('BEMContext this.trace()', function() {
it('should have proper this.trace() without option', function() {
test(function() {
block('b1').content()({ block: 'b2' });
block('b2').content()(function() {
return JSON.stringify(this.trace());
});
}, { block: 'b1' },
'<div class="b1">' +
'<div class="b2">[]</div></div>');
});

it('should have proper this.trace() with option', function() {
test(function() {
block('b1').content()({ block: 'b2' });
block('b2').content()(function() {
return JSON.stringify(this.trace());
});
}, { block: 'b1' },
'<div class="b1">' +
'<div class="b2">[' +
'{"mode":"content","block":"b1","mods":{},"elemMods":{}},' +
'{"mode":"content","block":"b2","mods":{},"elemMods":{}}' +
']</div></div>',
{ trace: true });
});
});

0 comments on commit 2d5804e

Please sign in to comment.