Skip to content

Commit

Permalink
BEMXJST: production option (fix for #298)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Oct 10, 2016
1 parent d9eed9e commit cb6f9df
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
20 changes: 19 additions & 1 deletion lib/bemxjst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,10 @@ BEMXJST.prototype.runOne = function runOne(json) {
ent.init(block, elem);
}

var res = ent.run(context);
var res = this.options.production === true ?
this.tryRun(context, ent) :
ent.run(context);

context.ctx = oldCtx;
context.block = oldBlock;
context.elem = oldElem;
Expand All @@ -415,6 +418,21 @@ BEMXJST.prototype.runOne = function runOne(json) {
return res;
};

BEMXJST.prototype.tryRun = function tryRun(context, ent) {
try {
return ent.run(context);
} catch (e) {
console.error('BEMXJST ERROR: cannot render ' +
[
'block ' + context.block,
'elem ' + context.elem,
'mods ' + JSON.stringify(context.mods),
'elemMods ' + JSON.stringify(context.elemMods)
].join(', '), e);
return '';
}
};

BEMXJST.prototype.renderContent = function renderContent(content, isBEM) {
var context = this.context;
var oldPos = context.position;
Expand Down
21 changes: 5 additions & 16 deletions test/api-apply-test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
var assert = require('assert');
var bemxjst = require('../').bemhtml;
var bemhtml = require('../').bemhtml;

describe('API apply', function() {
it('should throw error with one apply', function() {
// There is no bemxjst.compile()
assert.throws(function() {
bemxjst.apply({ block: 'b' });
});
});

it('should throw errors with many applies', function() {
// There is no bemxjst.compile()
assert.throws(function() {
bemxjst.apply({ block: 'b' });
});
assert.throws(function() {
bemxjst.apply({ block: 'b' });
});
it('should support apply', function() {
var templates = bemhtml.compile();
var html = templates.apply({ block: 'b' });
assert.equal(html, '<div class="b"></div>');
});
});
31 changes: 31 additions & 0 deletions test/api-compile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,35 @@ describe('API compile', function() {
assert.equal(template.apply({ block: 'a' }),
'<a class="a"></a>');
});

describe('Production mode', function() {
it('should render even if error in one block', function() {
var template = bemxjst.compile(function() {
block('b1').attrs()(function() {
var attrs = applyNext();
attrs.undef.foo = 'bar';
return attrs;
});
}, { production: true });

assert.equal(template.apply({
block: 'page',
content: { block: 'b1' }
}), '<div class="page"></div>');
});

it('should throw error with one apply if production mode off', function() {
var template = bemxjst.compile(function() {
block('b1').attrs()(function() {
var attrs = applyNext();
attrs.foo = 'bar';
return attrs;
});
});

assert.throws(function() {
template.apply({ block: 'b1' });
});
});
});
});

0 comments on commit cb6f9df

Please sign in to comment.