forked from remy/polyfills
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classList.js
62 lines (53 loc) · 1.35 KB
/
classList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(function () {
if (typeof Element === "undefined" || Element.prototype.hasOwnProperty("classList")) return;
var indexOf = [].indexOf,
slice = [].slice,
push = [].push,
splice = [].splice,
join = [].join;
function DOMTokenList(el) {
this._element = el;
if (el.className != this.classCache) {
this._classCache = el.className;
var classes = this._classCache.split(' '),
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) {
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);
splice.call(this, i, 1);
setToClassName(this._element, slice.call(this, 0));
},
toString: function() {
return join.call(this, ' ');
},
toggle: function(token) {
if (indexOf.call(this, token) === -1) {
this.add(token);
} else {
this.remove(token);
}
}
};
window.DOMTokenList = DOMTokenList;
Element.prototype.__defineGetter__('classList', function () {
return new DOMTokenList(this);
});
})();