Skip to content

Commit

Permalink
Merge pull request #893 from /issues/890@v2
Browse files Browse the repository at this point in the history
i-bem: setMod/hasMod convert non-boolean values to string
  • Loading branch information
Vladimir Varankin committed Mar 7, 2015
2 parents 765ef4f + d26aa88 commit 2ee4ce6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
21 changes: 21 additions & 0 deletions common.blocks/i-bem/i-bem.spec.js
Expand Up @@ -150,6 +150,12 @@ describe('i-bem', function() {
.setMod('mod1')
.getMod('mod1').should.be.true;
});

it('should cast non-boolean mod value to string', function() {
block
.setMod('mod1', 1)
.getMod('mod1').should.be.equal('1');
});
});

describe('delMod', function() {
Expand Down Expand Up @@ -185,6 +191,21 @@ describe('i-bem', function() {
.should.be.false;
});

it('should treat defined non-boolean mod value as a string', function() {
block
.setMod('mod1', 1)
.hasMod('mod1', 1)
.should.be.true;

block.hasMod('mod1', '1')
.should.be.true;

block
.setMod('mod1', '2')
.hasMod('mod1', 2)
.should.be.true;
});

it('in short form should return true for undefined mod', function() {
block.hasMod('mod4').should.be.false;
});
Expand Down
15 changes: 12 additions & 3 deletions common.blocks/i-bem/i-bem.vanilla.js
Expand Up @@ -260,7 +260,7 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
* Checks whether a block or nested element has a modifier
* @param {Object} [elem] Nested element
* @param {String} modName Modifier name
* @param {String} [modVal] Modifier value
* @param {String|Boolean} [modVal] Modifier value. If defined and not of type String or Boolean, it is casted to String
* @returns {Boolean}
*/
hasMod : function(elem, modName, modVal) {
Expand All @@ -283,6 +283,11 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
}
}

var typeModVal = typeof modVal;
typeModVal === 'string' ||
typeModVal === 'boolean' ||
typeModVal === 'undefined' || (modVal = modVal.toString());

var res = this.getMod(elem, modName) === modVal;
return invert? !res : res;
},
Expand Down Expand Up @@ -344,7 +349,7 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
* Sets the modifier for a block/nested element
* @param {Object} [elem] Nested element
* @param {String} modName Modifier name
* @param {String} modVal Modifier value
* @param {String|Boolean} [modVal=true] Modifier value. If not of type String or Boolean, it is casted to String
* @returns {BEM} this
*/
setMod : function(elem, modName, modVal) {
Expand All @@ -361,7 +366,11 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
}

if(!elem || elem[0]) {
modVal === false && (modVal = '');
if(modVal === false) {
modVal = '';
} else if(typeof modVal !== 'boolean') {
modVal = modVal.toString();
}

var modId = (elem && elem[0]? identify(elem[0]) : '') + '_' + modName;

Expand Down

0 comments on commit 2ee4ce6

Please sign in to comment.