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