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