Skip to content

Commit

Permalink
BEMXJST: modifier templates should apply before def() (fix #482)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Feb 5, 2018
1 parent 444f76c commit 12575ca
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/en/5-templates-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def()(value)

The `def` mode (short for "default") has a special status. It is responsible for generating the result as a whole. This mode defines the list of other modes and the order to go through them, as well as the build procedure for getting the final representation of the HTML element or BEMJSON from the parts generated in the other modes.

This is a special mode that shouldn’t be used unless truly necessary. A user-defined template that redefines `def` disables calls of the other modes by default.
This is a special mode that shouldn’t be used unless truly necessary. A user-defined template that redefines `def` disables calls of the other modes by default, except `mods`, `elemMods`, `addMods` and `addElemMods`.

#### tag

Expand Down
2 changes: 1 addition & 1 deletion docs/ru/5-templates-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def()(value)

Особый статус имеет режим `def` (сокращение от default), который отвечает за генерацию результата в целом. В рамках этого режима задан набор и порядок прохождения остальных режимов, а также определена процедура сборки финального представления HTML-элемента или BEMJSON из фрагментов, сгенерированных в остальных режимах.

Режим является особым и не стоит использовать его без особой надобности. Пользовательский шаблон, переопределяющий `def`, отключает вызовы остальных режимов по умолчанию.
Режим является особым и не стоит использовать его без особой надобности. Пользовательский шаблон, переопределяющий `def`, отключает вызовы остальных режимов по умолчанию, кроме `mods`, `elemMods`, `addMods` и `addElemMods`, которые выполняются до `def`.

#### tag

Expand Down
3 changes: 0 additions & 3 deletions lib/bemhtml/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ Entity.prototype._keys = {
};

Entity.prototype.defaultBody = function(context) {
context.mods = this.mods.exec(context);
if (context.ctx.elem) context.elemMods = this.elemMods.exec(context);

return this.bemxjst.render(context,
this,
this.tag.exec(context),
Expand Down
3 changes: 0 additions & 3 deletions lib/bemtree/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ inherits(Entity, BemxjstEntity);
exports.Entity = Entity;

Entity.prototype.defaultBody = function(context) {
context.mods = this.mods.exec(context);
if (context.ctx.elem) context.elemMods = this.elemMods.exec(context);

return this.bemxjst.render(context,
this,
this.content.exec(context),
Expand Down
8 changes: 8 additions & 0 deletions lib/bemxjst/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ Entity.prototype.prepend = function(other) {

// NOTE: This could be potentially compiled into inlined invokations
Entity.prototype.run = function(context) {
if (this.mods.count !== 0) {
context.mods = this.mods.exec(context);
}

if (context.ctx.elem && this.elemMods.count !== 0) {
context.elemMods = this.elemMods.exec(context);
}

if (this.def.count !== 0)
return this.def.exec(context);

Expand Down
42 changes: 42 additions & 0 deletions test/modes-def-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var assert = require('assert');
var bemhtml = require('./fixtures')('bemhtml');
var fixtures = require('./fixtures')('bemhtml');
var test = fixtures.test;

describe('Modes def', function() {
it('should throw error when args passed to def mode', function() {
Expand All @@ -9,4 +11,44 @@ describe('Modes def', function() {
});
});
});

it('mods() templates should apply', function() {
test(function() {
block('a').def()('NO');
block('a').mods()({ m: true });
block('a').mod('m').def()('YES');
},
{ block: 'a' },
'YES');
});

it('addMods() templates should apply', function() {
test(function() {
block('a').def()('NO');
block('a').addMods()({ m: true });
block('a').mod('m').def()('YES');
},
{ block: 'a' },
'YES');
});

it('elemMods() templates should apply', function() {
test(function() {
block('a').elem('e').def()('NO');
block('a').elem('e').elemMods()({ m: true });
block('a').elem('e').elemMod('m').def()('YES');
},
{ block: 'a', elem: 'e' },
'YES');
});

it('addEemMods() templates should apply', function() {
test(function() {
block('a').elem('e').def()('NO');
block('a').elem('e').addElemMods()({ m: true });
block('a').elem('e').elemMod('m').def()('YES');
},
{ block: 'a', elem: 'e' },
'YES');
});
});

0 comments on commit 12575ca

Please sign in to comment.