Skip to content

Commit

Permalink
Code clean up, reduction and remove unnecessary Array.indexOf polyfil…
Browse files Browse the repository at this point in the history
…l that incidentally interferes with IE8's fireEvent, there's es5-shim for that if needed
  • Loading branch information
wyuenho committed Feb 15, 2014
1 parent b463676 commit 898b546
Showing 1 changed file with 22 additions and 45 deletions.
67 changes: 22 additions & 45 deletions classList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,40 @@

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 DOMTokenList(el) {
this.el = el;
// The className needs to be trimmed and split on whitespace
// to retrieve a list of classes.
var classes = el.className.replace(/^\s+|\s+$/g,'').split(/\s+/);
for (var 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));
this.el.className = this.toString();
},
contains: function(token) {
return indexOf.call(this, token) !== -1;
return this.el.className.indexOf(token) != -1;
},
item: function(index) {
return this[index] || null;
},
remove: function(token) {
var i = indexOf.call(this, token);
if (i === -1) {
return;
}
if (!this.contains(token)) return;
for (var i = 0; i < this.length; i++) {
if (this[i] == token) break;
}
splice.call(this, i, 1);
setToClassName(this._element, slice.call(this, 0));
this.el.className = this.toString();
},
toString: function() {
return join.call(this, ' ');
Expand All @@ -77,17 +54,17 @@ DOMTokenList.prototype = {
window.DOMTokenList = DOMTokenList;

function defineElementGetter (obj, prop, getter) {
if (Object.defineProperty) {
Object.defineProperty(obj, prop,{
get : getter
})
} else {
obj.__defineGetter__(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);
return new DOMTokenList(this);
});

})();

0 comments on commit 898b546

Please sign in to comment.