-
Notifications
You must be signed in to change notification settings - Fork 4
/
classlist.js
106 lines (82 loc) · 2.05 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
module.exports = ClassList
var arr = Array.prototype
function ClassList (elem) {
if (!(this instanceof ClassList)) {
return new ClassList(elem)
}
// Use RegExp instead of String#trim as the latter doesn't exist in IE <= 8
var className = elem.className.replace(/^\s+|\s+$/g, '')
var classes = className.split(/\s+/)
this._elem = elem
this.length = 0
if (!className) return
for (var i = 0; i < classes.length; i += 1) {
arr.push.call(this, classes[i])
}
}
ClassList.prototype.item = function (index) {
if (index >= this.length) {
return null
}
return this[index]
}
ClassList.prototype.add = function () {
for (var i = 0; i < arguments.length; i += 1) {
var token = String(arguments[i])
if (indexOf(this, token) >= 0) {
continue
}
arr.push.call(this, token)
}
this._elem.className = this.toString()
}
ClassList.prototype.remove = function () {
for (var i = 0; i < arguments.length; i += 1) {
var token = String(arguments[i])
var index = indexOf(this, token)
if (index < 0) continue
arr.splice.call(this, index, 1)
}
this._elem.className = this.toString()
}
ClassList.prototype.contains = function (token) {
return indexOf(this, String(token)) >= 0
}
ClassList.prototype.toggle = function (token, force) {
if (force !== undefined) {
if (force) {
this.add(token)
} else {
this.remove(token)
}
} else {
if (this.contains(token)) {
this.remove(token)
} else {
this.add(token)
}
}
return this.contains(token)
}
ClassList.prototype.replace = function (token, newToken) {
var index = indexOf(this, token)
if (index < 0) {
return false
}
arr.splice.call(this, index, 1, newToken)
this._elem.className = this.toString()
return true
}
ClassList.prototype.toString = function () {
return arr.join.call(this, ' ')
}
// IE <= 8 doesn't have a native Array#indexOf
function indexOf (array, item) {
var len = array.length
for (var i = 0; i < len; i += 1) {
if (array[i] === item) {
return i
}
}
return -1
}