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