Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/cupcake2048/js/classlist_polyfill.js
4626 views
1
(function () {
2
if (typeof window.Element === "undefined" ||
3
"classList" in document.documentElement) {
4
return;
5
}
6
7
var prototype = Array.prototype,
8
push = prototype.push,
9
splice = prototype.splice,
10
join = prototype.join;
11
12
function DOMTokenList(el) {
13
this.el = el;
14
// The className needs to be trimmed and split on whitespace
15
// to retrieve a list of classes.
16
var classes = el.className.replace(/^\s+|\s+$/g, '').split(/\s+/);
17
for (var i = 0; i < classes.length; i++) {
18
push.call(this, classes[i]);
19
}
20
}
21
22
DOMTokenList.prototype = {
23
add: function (token) {
24
if (this.contains(token)) return;
25
push.call(this, token);
26
this.el.className = this.toString();
27
},
28
contains: function (token) {
29
return this.el.className.indexOf(token) != -1;
30
},
31
item: function (index) {
32
return this[index] || null;
33
},
34
remove: function (token) {
35
if (!this.contains(token)) return;
36
for (var i = 0; i < this.length; i++) {
37
if (this[i] == token) break;
38
}
39
splice.call(this, i, 1);
40
this.el.className = this.toString();
41
},
42
toString: function () {
43
return join.call(this, ' ');
44
},
45
toggle: function (token) {
46
if (!this.contains(token)) {
47
this.add(token);
48
} else {
49
this.remove(token);
50
}
51
52
return this.contains(token);
53
}
54
};
55
56
window.DOMTokenList = DOMTokenList;
57
58
function defineElementGetter(obj, prop, getter) {
59
if (Object.defineProperty) {
60
Object.defineProperty(obj, prop, {
61
get: getter
62
});
63
} else {
64
obj.__defineGetter__(prop, getter);
65
}
66
}
67
68
defineElementGetter(HTMLElement.prototype, 'classList', function () {
69
return new DOMTokenList(this);
70
});
71
})();
72
73