142
St
startsInvasion / files
startsInvasion / libs / node_modules / qwery / src / qwery.js
9.2 KB
1!function (context, doc) { 2 3 var c, i, j, k, l, m, o, p, r, v, 4 el, node, len, found, classes, item, items, token, 5 html = doc.documentElement, 6 id = /#([\w\-]+)/, 7 clas = /\.[\w\-]+/g, 8 idOnly = /^#([\w\-]+$)/, 9 classOnly = /^\.([\w\-]+)$/, 10 tagOnly = /^([\w\-]+)$/, 11 tagAndOrClass = /^([\w]+)?\.([\w\-]+)$/, 12 normalizr = /\s*([\s\+\~>])\s*/g, 13 splitters = /[\s\>\+\~]/, 14 splittersMore = /(?![\s\w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^'"]*\])/, 15 dividers = new RegExp('(' + splitters.source + ')' + splittersMore.source, 'g'), 16 tokenizr = new RegExp(splitters.source + splittersMore.source), 17 specialChars = /([.*+?\^=!:${}()|\[\]\/\\])/g, 18 simple = /^([a-z0-9]+)?(?:([\.\#]+[\w\-\.#]+)?)/, 19 attr = /\[([\w\-]+)(?:([\|\^\$\*\~]?\=)['"]?([ \w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^]+)["']?)?\]/, 20 pseudo = /:([\w\-]+)(\(['"]?(\w+)['"]?\))?/, 21 chunker = new RegExp(simple.source + '(' + attr.source + ')?' + '(' + pseudo.source + ')?'), 22 walker = { 23 ' ': function (node) { 24 return node && node !== html && node.parentNode 25 }, 26 '>': function (node, contestant) { 27 return node && node.parentNode == contestant.parentNode && node.parentNode; 28 }, 29 '~': function (node) { 30 return node && node.previousSibling; 31 }, 32 '+': function (node, contestant, p1, p2) { 33 if (!node) { 34 return false; 35 } 36 p1 = previous(node); 37 p2 = previous(contestant); 38 return p1 && p2 && p1 == p2 && p1; 39 } 40 }; 41 function cache() { 42 this.c = {}; 43 } 44 cache.prototype = { 45 g: function (k) { 46 return this.c[k] || undefined; 47 }, 48 s: function (k, v) { 49 this.c[k] = v; 50 return v; 51 } 52 }; 53 54 var classCache = new cache(), 55 cleanCache = new cache(), 56 attrCache = new cache(), 57 tokenCache = new cache(); 58 59 function array(ar) { 60 r = []; 61 for (i = 0, len = ar.length; i < len; i++) { 62 r[i] = ar[i]; 63 } 64 return r; 65 } 66 67 function previous(n) { 68 while (n = n.previousSibling) { 69 if (n.nodeType == 1) { 70 break; 71 } 72 } 73 return n 74 } 75 76 function q(query) { 77 return query.match(chunker); 78 } 79 80 // this next method expect at most these args 81 // given => div.hello[title="world"]:foo('bar') 82 83 // div.hello[title="world"]:foo('bar'), div, .hello, [title="world"], title, =, world, :foo('bar'), foo, ('bar'), bar] 84 85 function interpret(whole, tag, idsAndClasses, wholeAttribute, attribute, qualifier, value, wholePseudo, pseudo, wholePseudoVal, pseudoVal) { 86 var m, c, k; 87 if (tag && this.tagName.toLowerCase() !== tag) { 88 return false; 89 } 90 if (idsAndClasses && (m = idsAndClasses.match(id)) && m[1] !== this.id) { 91 return false; 92 } 93 if (idsAndClasses && (classes = idsAndClasses.match(clas))) { 94 for (i = classes.length; i--;) { 95 c = classes[i].slice(1); 96 if (!(classCache.g(c) || classCache.s(c, new RegExp('(^|\\s+)' + c + '(\\s+|$)'))).test(this.className)) { 97 return false; 98 } 99 } 100 } 101 if (pseudo && qwery.pseudos[pseudo] && !qwery.pseudos[pseudo](this, pseudoVal)) { 102 return false; 103 } 104 if (wholeAttribute && !value) { 105 o = this.attributes; 106 for (k in o) { 107 if (Object.prototype.hasOwnProperty.call(o, k) && (o[k].name || k) == attribute) { 108 return this; 109 } 110 } 111 } 112 if (wholeAttribute && !checkAttr(qualifier, this.getAttribute(attribute) || '', value)) { 113 return false; 114 } 115 return this; 116 } 117 118 function clean(s) { 119 return cleanCache.g(s) || cleanCache.s(s, s.replace(specialChars, '\\$1')); 120 } 121 122 function checkAttr(qualify, actual, val) { 123 switch (qualify) { 124 case '=': 125 return actual == val; 126 case '^=': 127 return actual.match(attrCache.g('^=' + val) || attrCache.s('^=' + val, new RegExp('^' + clean(val)))); 128 case '$=': 129 return actual.match(attrCache.g('$=' + val) || attrCache.s('$=' + val, new RegExp(clean(val) + '$'))); 130 case '*=': 131 return actual.match(attrCache.g(val) || attrCache.s(val, new RegExp(clean(val)))); 132 case '~=': 133 return actual.match(attrCache.g('~=' + val) || attrCache.s('~=' + val, new RegExp('(?:^|\\s+)' + clean(val) + '(?:\\s+|$)'))); 134 case '|=': 135 return actual.match(attrCache.g('|=' + val) || attrCache.s('|=' + val, new RegExp('^' + clean(val) + '(-|$)'))); 136 } 137 return 0; 138 } 139 140 function _qwery(selector) { 141 var r = [], ret = [], i, j = 0, k, l, m, p, token, tag, els, root, intr, item, children, 142 tokens = tokenCache.g(selector) || tokenCache.s(selector, selector.split(tokenizr)), 143 dividedTokens = selector.match(dividers), dividedToken; 144 tokens = tokens.slice(0); // this makes a copy of the array so the cached original is not effected 145 if (!tokens.length) { 146 return r; 147 } 148 149 token = tokens.pop(); 150 root = tokens.length && (m = tokens[tokens.length - 1].match(idOnly)) ? doc.getElementById(m[1]) : doc; 151 if (!root) { 152 return r; 153 } 154 intr = q(token); 155 els = dividedTokens && /^[+~]$/.test(dividedTokens[dividedTokens.length - 1]) ? function (r) { 156 while (root = root.nextSibling) { 157 root.nodeType == 1 && (intr[1] ? intr[1] == root.tagName.toLowerCase() : 1) && r.push(root) 158 } 159 return r 160 }([]) : 161 root.getElementsByTagName(intr[1] || '*'); 162 for (i = 0, l = els.length; i < l; i++) { 163 if (item = interpret.apply(els[i], intr)) { 164 r[j++] = item; 165 } 166 } 167 if (!tokens.length) { 168 return r; 169 } 170 171 // loop through all descendent tokens 172 for (j = 0, l = r.length, k = 0; j < l; j++) { 173 p = r[j]; 174 // loop through each token backwards crawling up tree 175 for (i = tokens.length; i--;) { 176 // loop through parent nodes 177 while (p = walker[dividedTokens[i]](p, r[j])) { 178 if (found = interpret.apply(p, q(tokens[i]))) { 179 break; 180 } 181 } 182 } 183 found && (ret[k++] = r[j]); 184 } 185 return ret; 186 } 187 188 function boilerPlate(selector, _root, fn) { 189 var root = (typeof _root == 'string') ? fn(_root)[0] : (_root || doc); 190 if (selector === window || isNode(selector)) { 191 return !_root || (selector !== window && isNode(root) && isAncestor(selector, root)) ? [selector] : []; 192 } 193 if (selector && typeof selector === 'object' && isFinite(selector.length)) { 194 return array(selector); 195 } 196 if (m = selector.match(idOnly)) { 197 return (el = doc.getElementById(m[1])) ? [el] : []; 198 } 199 if (m = selector.match(tagOnly)) { 200 return array(root.getElementsByTagName(m[1])); 201 } 202 return false; 203 } 204 205 function isNode(el) { 206 return (el && el.nodeType && (el.nodeType == 1 || el.nodeType == 9)); 207 } 208 209 function uniq(ar) { 210 var a = [], i, j; 211 label: 212 for (i = 0; i < ar.length; i++) { 213 for (j = 0; j < a.length; j++) { 214 if (a[j] == ar[i]) { 215 continue label; 216 } 217 } 218 a[a.length] = ar[i]; 219 } 220 return a; 221 } 222 223 function qwery(selector, _root) { 224 var root = (typeof _root == 'string') ? qwery(_root)[0] : (_root || doc); 225 if (!root || !selector) { 226 return []; 227 } 228 if (m = boilerPlate(selector, _root, qwery)) { 229 return m; 230 } 231 return select(selector, root); 232 } 233 234 var isAncestor = 'compareDocumentPosition' in html ? 235 function (element, container) { 236 return (container.compareDocumentPosition(element) & 16) == 16; 237 } : 'contains' in html ? 238 function (element, container) { 239 container = container == doc || container == window ? html : container; 240 return container !== element && container.contains(element); 241 } : 242 function (element, container) { 243 while (element = element.parentNode) { 244 if (element === container) { 245 return 1; 246 } 247 } 248 return 0; 249 }, 250 251 select = (doc.querySelector && doc.querySelectorAll) ? 252 function (selector, root) { 253 if (doc.getElementsByClassName && (m = selector.match(classOnly))) { 254 return array((root).getElementsByClassName(m[1])); 255 } 256 return array((root).querySelectorAll(selector)); 257 } : 258 function (selector, root) { 259 selector = selector.replace(normalizr, '$1'); 260 var result = [], collection, collections = [], i; 261 if (m = selector.match(tagAndOrClass)) { 262 items = root.getElementsByTagName(m[1] || '*'); 263 r = classCache.g(m[2]) || classCache.s(m[2], new RegExp('(^|\\s+)' + m[2] + '(\\s+|$)')); 264 for (i = 0, l = items.length, j = 0; i < l; i++) { 265 r.test(items[i].className) && (result[j++] = items[i]); 266 } 267 return result; 268 } 269 for (i = 0, items = selector.split(','), l = items.length; i < l; i++) { 270 collections[i] = _qwery(items[i]); 271 } 272 for (i = 0, l = collections.length; i < l && (collection = collections[i]); i++) { 273 var ret = collection; 274 if (root !== doc) { 275 ret = []; 276 for (j = 0, m = collection.length; j < m && (element = collection[j]); j++) { 277 // make sure element is a descendent of root 278 isAncestor(element, root) && ret.push(element); 279 } 280 } 281 result = result.concat(ret); 282 } 283 return uniq(result); 284 }; 285 286 qwery.uniq = uniq; 287 qwery.pseudos = {}; 288 289 var oldQwery = context.qwery; 290 qwery.noConflict = function () { 291 context.qwery = oldQwery; 292 return this; 293 }; 294 context['qwery'] = qwery; 295 296}(this, document);