vendor: 5,621 bytes, lines 1-237
1/* 2 * classList.js: Cross-browser full element.classList implementation. 3 * 2014-12-13 4 * 5 * By Eli Grey, http://eligrey.com 6 * Public Domain. 7 * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. 8 */ 9 10/*global self, document, DOMException */ 11 12/*! @source http://purl.eligrey.com/github/classList.js/blob/master/classList.js */ 13 14if ("document" in self) { 15 16// Full polyfill for browsers with no classList support 17if (!("classList" in document.createElement("_"))) { 18 19(function (view) { 20 21"use strict"; 22 23if (!('Element' in view)) return; 24 25var 26 classListProp = "classList" 27 , protoProp = "prototype" 28 , elemCtrProto = view.Element[protoProp] 29 , objCtr = Object 30 , strTrim = String[protoProp].trim || function () { 31 return this.replace(/^\s+|\s+$/g, ""); 32 } 33 , arrIndexOf = Array[protoProp].indexOf || function (item) { 34 var 35 i = 0 36 , len = this.length 37 ; 38 for (; i < len; i++) { 39 if (i in this && this[i] === item) { 40 return i; 41 } 42 } 43 return -1; 44 } 45 // Vendors: please allow content code to instantiate DOMExceptions 46 , DOMEx = function (type, message) { 47 this.name = type; 48 this.code = DOMException[type]; 49 this.message = message; 50 } 51 , checkTokenAndGetIndex = function (classList, token) { 52 if (token === "") { 53 throw new DOMEx( 54 "SYNTAX_ERR" 55 , "An invalid or illegal string was specified" 56 ); 57 } 58 if (/\s/.test(token)) { 59 throw new DOMEx( 60 "INVALID_CHARACTER_ERR" 61 , "String contains an invalid character" 62 ); 63 } 64 return arrIndexOf.call(classList, token); 65 } 66 , ClassList = function (elem) { 67 var 68 trimmedClasses = strTrim.call(elem.getAttribute("class") || "") 69 , classes = trimmedClasses ? trimmedClasses.split(/\s+/) : [] 70 , i = 0 71 , len = classes.length 72 ; 73 for (; i < len; i++) { 74 this.push(classes[i]); 75 } 76 this._updateClassName = function () { 77 elem.setAttribute("class", this.toString()); 78 }; 79 } 80 , classListProto = ClassList[protoProp] = [] 81 , classListGetter = function () { 82 return new ClassList(this); 83 } 84; 85// Most DOMException implementations don't allow calling DOMException's toString() 86// on non-DOMExceptions. Error's toString() is sufficient here. 87DOMEx[protoProp] = Error[protoProp]; 88classListProto.item = function (i) { 89 return this[i] || null; 90}; 91classListProto.contains = function (token) { 92 token += ""; 93 return checkTokenAndGetIndex(this, token) !== -1; 94}; 95classListProto.add = function () { 96 var 97 tokens = arguments 98 , i = 0 99 , l = tokens.length 100 , token 101 , updated = false 102 ; 103 do { 104 token = tokens[i] + ""; 105 if (checkTokenAndGetIndex(this, token) === -1) { 106 this.push(token); 107 updated = true; 108 } 109 } 110 while (++i < l); 111 112 if (updated) { 113 this._updateClassName(); 114 } 115}; 116classListProto.remove = function () { 117 var 118 tokens = arguments 119 , i = 0 120 , l = tokens.length 121 , token 122 , updated = false 123 , index 124 ; 125 do { 126 token = tokens[i] + ""; 127 index = checkTokenAndGetIndex(this, token); 128 while (index !== -1) { 129 this.splice(index, 1); 130 updated = true; 131 index = checkTokenAndGetIndex(this, token); 132 } 133 } 134 while (++i < l); 135 136 if (updated) { 137 this._updateClassName(); 138 } 139}; 140classListProto.toggle = function (token, force) { 141 token += ""; 142 143 var 144 result = this.contains(token) 145 , method = result ? 146 force !== true && "remove" 147 : 148 force !== false && "add" 149 ; 150 151 if (method) { 152 this[method](token); 153 } 154 155 if (force === true || force === false) { 156 return force; 157 } else { 158 return !result; 159 } 160}; 161classListProto.toString = function () { 162 return this.join(" "); 163}; 164 165if (objCtr.defineProperty) { 166 var classListPropDesc = { 167 get: classListGetter 168 , enumerable: true 169 , configurable: true 170 }; 171 try { 172 objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc); 173 } catch (ex) { // IE 8 doesn't support enumerable:true 174 if (ex.number === -0x7FF5EC54) { 175 classListPropDesc.enumerable = false; 176 objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc); 177 } 178 } 179} else if (objCtr[protoProp].__defineGetter__) { 180 elemCtrProto.__defineGetter__(classListProp, classListGetter); 181} 182 183}(self)); 184 185} else { 186// There is full or partial native classList support, so just check if we need 187// to normalize the add/remove and toggle APIs. 188 189(function () { 190 "use strict"; 191 192 var testElement = document.createElement("_"); 193 194 testElement.classList.add("c1", "c2"); 195 196 // Polyfill for IE 10/11 and Firefox <26, where classList.add and 197 // classList.remove exist but support only one argument at a time. 198 if (!testElement.classList.contains("c2")) { 199 var createMethod = function(method) { 200 var original = DOMTokenList.prototype[method]; 201 202 DOMTokenList.prototype[method] = function(token) { 203 var i, len = arguments.length; 204 205 for (i = 0; i < len; i++) { 206 token = arguments[i]; 207 original.call(this, token); 208 } 209 }; 210 }; 211 createMethod('add'); 212 createMethod('remove'); 213 } 214 215 testElement.classList.toggle("c3", false); 216 217 // Polyfill for IE 10 and Firefox <24, where classList.toggle does not 218 // support the second argument. 219 if (testElement.classList.contains("c3")) { 220 var _toggle = DOMTokenList.prototype.toggle; 221 222 DOMTokenList.prototype.toggle = function(token, force) { 223 if (1 in arguments && !this.contains(token) === !force) { 224 return force; 225 } else { 226 return _toggle.call(this, token); 227 } 228 }; 229 230 } 231 232 testElement = null; 233}()); 234 235} 236 237}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.