diff --git a/common.blocks/i-bem/i-bem.spec.js b/common.blocks/i-bem/i-bem.spec.js index e941a1fd9..8e3c95ecb 100644 --- a/common.blocks/i-bem/i-bem.spec.js +++ b/common.blocks/i-bem/i-bem.spec.js @@ -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() { @@ -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; }); diff --git a/common.blocks/i-bem/i-bem.vanilla.js b/common.blocks/i-bem/i-bem.vanilla.js index 3db290492..e9dedd636 100644 --- a/common.blocks/i-bem/i-bem.vanilla.js +++ b/common.blocks/i-bem/i-bem.vanilla.js @@ -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) { @@ -283,7 +283,12 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ { } } - var res = this.getMod(elem, modName) === modVal; + var typeModVal = typeof modVal; + typeModVal === 'string' || + typeModVal === 'boolean' || + typeModVal === 'undefined' || (modVal = modVal.toString()); + + var res = this.getMod(elem, modName) === modVal; return invert? !res : res; }, @@ -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) { @@ -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;