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 Oct 30, 2017
1 parent ba102b7 commit 073a7c2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
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 073a7c2

Please sign in to comment.