Skip to content

Instantly share code, notes, and snippets.

@dshster
Last active August 29, 2015 14:02
Show Gist options
  • Save dshster/37feec87ce5320b799bb to your computer and use it in GitHub Desktop.
Save dshster/37feec87ce5320b799bb to your computer and use it in GitHub Desktop.
bem classes modifiers
(function() {
'use strict';
var delimiter = '--';
DOMTokenList.prototype.modify = function(mod) {
if (false === tag.classList.modified(mod)) {
this.add([this[0], delimiter, mod].join(''));
}
};
DOMTokenList.prototype.unmodify = function(mod) {
if (true === tag.classList.modified(mod)) {
this.remove([this[0], delimiter, mod].join(''));
}
};
DOMTokenList.prototype.modified = function(mod) {
return 'string' === typeof this[0] && this.contains([this[0], delimiter, mod].join(''));
};
})();
var tag = document.createElement('div');
tag.classList.add('baseclass');
console.log(tag, tag.classList.modified('mod'));
tag.classList.modify('mod');
console.log(tag, tag.classList.modified('mod'));
tag.classList.modify('foo');
console.log(tag, tag.classList.modified('foo'));
tag.classList.unmodify('mod');
console.log(tag, tag.classList.modified('mod'));
tag.classList.unmodify('foo');
console.log(tag, tag.classList.modified('foo'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment