diff --git a/lib/bemxjst/context.js b/lib/bemxjst/context.js index e4807052..214343ae 100644 --- a/lib/bemxjst/context.js +++ b/lib/bemxjst/context.js @@ -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 = {}; @@ -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; +}; diff --git a/lib/bemxjst/match.js b/lib/bemxjst/match.js index 1a6c02bc..659ab123 100644 --- a/lib/bemxjst/match.js +++ b/lib/bemxjst/match.js @@ -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 diff --git a/test/bemcontext-trace-test.js b/test/bemcontext-trace-test.js new file mode 100644 index 00000000..94be25eb --- /dev/null +++ b/test/bemcontext-trace-test.js @@ -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' }, + '
' + + '
[]
'); + }); + + 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' }, + '
' + + '
[' + + '{"mode":"content","block":"b1","mods":{},"elemMods":{}},' + + '{"mode":"content","block":"b2","mods":{},"elemMods":{}}' + + ']
', + { trace: true }); + }); +});