(function () { if (typeof window.Element === "undefined" || "classList" in document.documentElement) return; // adds indexOf to Array prototype for IE support if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(obj, start) { for (var i = (start || 0), j = this.length; i < j; i++) { if (this[i] === obj) { return i; } } return -1; } } var prototype = Array.prototype, indexOf = prototype.indexOf, slice = prototype.slice, push = prototype.push, splice = prototype.splice, join = prototype.join; function DOMTokenList(el) { this._element = el; if (el.className != this._classCache) { this._classCache = el.className; if (!this._classCache) return; // The className needs to be trimmed and split on whitespace // to retrieve a list of classes. var classes = this._classCache.replace(/^\s+|\s+$/g,'').split(/\s+/), i; for (i = 0; i < classes.length; i++) { push.call(this, classes[i]); } } }; function setToClassName(el, classes) { el.className = classes.join(' '); } DOMTokenList.prototype = { add: function(token) { if(this.contains(token)) return; push.call(this, token); setToClassName(this._element, slice.call(this, 0)); }, contains: function(token) { return indexOf.call(this, token) !== -1; }, item: function(index) { return this[index] || null; }, remove: function(token) { var i = indexOf.call(this, token); if (i === -1) { return; } splice.call(this, i, 1); setToClassName(this._element, slice.call(this, 0)); }, toString: function() { return join.call(this, ' '); }, toggle: function(token) { if (!this.contains(token)) { this.add(token); } else { this.remove(token); } return this.contains(token); } }; window.DOMTokenList = DOMTokenList; function defineElementGetter (obj, prop, getter) { if (Object.defineProperty) { Object.defineProperty(obj, prop,{ get : getter }) } else { obj.__defineGetter__(prop, getter); } } defineElementGetter(Element.prototype, 'classList', function () { return new DOMTokenList(this); }); })();