| 1 |
"use strict"; |
| 2 |
|
| 3 |
var utils = require('./utils'); |
| 4 |
|
| 5 |
module.exports = DOMTokenList; |
| 6 |
|
| 7 |
function DOMTokenList(getter, setter) { |
| 8 |
this._getString = getter; |
| 9 |
this._setString = setter; |
| 10 |
this._length = 0; |
| 11 |
this._lastStringValue = ''; |
| 12 |
this._update(); |
| 13 |
} |
| 14 |
|
| 15 |
Object.defineProperties(DOMTokenList.prototype, { |
| 16 |
length: { get: function() { return this._length; } }, |
| 17 |
item: { value: function(index) { |
| 18 |
var list = getList(this); |
| 19 |
if (index < 0 || index >= list.length) { |
| 20 |
return null; |
| 21 |
} |
| 22 |
return list[index]; |
| 23 |
}}, |
| 24 |
|
| 25 |
contains: { value: function(token) { |
| 26 |
token = String(token); |
| 27 |
var list = getList(this); |
| 28 |
return list.indexOf(token) > -1; |
| 29 |
}}, |
| 30 |
|
| 31 |
add: { value: function() { |
| 32 |
var list = getList(this); |
| 33 |
for (var i = 0, len = arguments.length; i < len; i++) { |
| 34 |
var token = handleErrors(arguments[i]); |
| 35 |
if (list.indexOf(token) < 0) { |
| 36 |
list.push(token); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
this._update(list); |
| 44 |
}}, |
| 45 |
|
| 46 |
remove: { value: function() { |
| 47 |
var list = getList(this); |
| 48 |
for (var i = 0, len = arguments.length; i < len; i++) { |
| 49 |
var token = handleErrors(arguments[i]); |
| 50 |
var index = list.indexOf(token); |
| 51 |
if (index > -1) { |
| 52 |
list.splice(index, 1); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
this._update(list); |
| 60 |
}}, |
| 61 |
|
| 62 |
toggle: { value: function toggle(token, force) { |
| 63 |
token = handleErrors(token); |
| 64 |
if (this.contains(token)) { |
| 65 |
if (force === undefined || force === false) { |
| 66 |
this.remove(token); |
| 67 |
return false; |
| 68 |
} |
| 69 |
return true; |
| 70 |
} else { |
| 71 |
if (force === undefined || force === true) { |
| 72 |
this.add(token); |
| 73 |
return true; |
| 74 |
} |
| 75 |
return false; |
| 76 |
} |
| 77 |
}}, |
| 78 |
|
| 79 |
replace: { value: function replace(token, newToken) { |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
if (String(newToken)==='') { utils.SyntaxError(); } |
| 84 |
token = handleErrors(token); |
| 85 |
newToken = handleErrors(newToken); |
| 86 |
var list = getList(this); |
| 87 |
var idx = list.indexOf(token); |
| 88 |
if (idx < 0) { |
| 89 |
|
| 90 |
return false; |
| 91 |
} |
| 92 |
var idx2 = list.indexOf(newToken); |
| 93 |
if (idx2 < 0) { |
| 94 |
list[idx] = newToken; |
| 95 |
} else { |
| 96 |
|
| 97 |
|
| 98 |
if (idx < idx2) { |
| 99 |
list[idx] = newToken; |
| 100 |
list.splice(idx2, 1); |
| 101 |
} else { |
| 102 |
|
| 103 |
list.splice(idx, 1); |
| 104 |
} |
| 105 |
} |
| 106 |
this._update(list); |
| 107 |
return true; |
| 108 |
}}, |
| 109 |
|
| 110 |
toString: { value: function() { |
| 111 |
return this._getString(); |
| 112 |
}}, |
| 113 |
|
| 114 |
value: { |
| 115 |
get: function() { |
| 116 |
return this._getString(); |
| 117 |
}, |
| 118 |
set: function(v) { |
| 119 |
this._setString(v); |
| 120 |
this._update(); |
| 121 |
} |
| 122 |
}, |
| 123 |
|
| 124 |
|
| 125 |
_update: { value: function(list) { |
| 126 |
if (list) { |
| 127 |
fixIndex(this, list); |
| 128 |
this._setString(list.join(" ").trim()); |
| 129 |
} else { |
| 130 |
fixIndex(this, getList(this)); |
| 131 |
} |
| 132 |
this._lastStringValue = this._getString(); |
| 133 |
} }, |
| 134 |
}); |
| 135 |
|
| 136 |
function fixIndex(clist, list) { |
| 137 |
var oldLength = clist._length; |
| 138 |
var i; |
| 139 |
clist._length = list.length; |
| 140 |
for (i = 0; i < list.length; i++) { |
| 141 |
clist[i] = list[i]; |
| 142 |
} |
| 143 |
|
| 144 |
for (; i < oldLength; i++) { |
| 145 |
clist[i] = undefined; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
function handleErrors(token) { |
| 150 |
token = String(token); |
| 151 |
if (token === "") { |
| 152 |
utils.SyntaxError(); |
| 153 |
} |
| 154 |
if (/[ \t\r\n\f]/.test(token)) { |
| 155 |
utils.InvalidCharacterError(); |
| 156 |
} |
| 157 |
return token; |
| 158 |
} |
| 159 |
|
| 160 |
function toArray(clist) { |
| 161 |
var length = clist._length; |
| 162 |
var arr = Array(length); |
| 163 |
for (var i = 0; i < length; i++) { |
| 164 |
arr[i] = clist[i]; |
| 165 |
} |
| 166 |
return arr; |
| 167 |
} |
| 168 |
|
| 169 |
function getList(clist) { |
| 170 |
var strProp = clist._getString(); |
| 171 |
if (strProp === clist._lastStringValue) { |
| 172 |
return toArray(clist); |
| 173 |
} |
| 174 |
var str = strProp.replace(/(^[ \t\r\n\f]+)|([ \t\r\n\f]+$)/g, ''); |
| 175 |
if (str === "") { |
| 176 |
return []; |
| 177 |
} else { |
| 178 |
var seen = Object.create(null); |
| 179 |
return str.split(/[ \t\r\n\f]+/g).filter(function(n) { |
| 180 |
var key = '$' + n; |
| 181 |
if (seen[key]) { return false; } |
| 182 |
seen[key] = true; |
| 183 |
return true; |
| 184 |
}); |
| 185 |
} |
| 186 |
} |