73
Ch
Chatroom / files
Chatroom / htdocs / js / libs / qwery.js
6.5 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.getElementsByTagName('html')[0], 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 32 var cache = function () { 33 this.c = {}; 34 }; 35 cache.prototype = { 36 g: function (k) { 37 return this.c[k] || undefined; 38 }, 39 s: function (k, v) { 40 this.c[k] = v; 41 return v; 42 } 43 }; 44 45 var classCache = new cache(), 46 cleanCache = new cache(), 47 attrCache = new cache(), 48 tokenCache = new cache(); 49 50 function q(query) { 51 return query.match(chunker); 52 } 53 54 function interpret(whole, tag, idsAndClasses, wholeAttribute, attribute, qualifier, value) { 55 var m, c, k; 56 if (tag && this.tagName.toLowerCase() !== tag) { 57 return false; 58 } 59 if (idsAndClasses && (m = idsAndClasses.match(id)) && m[1] !== this.id) { 60 return false; 61 } 62 if (idsAndClasses && (classes = idsAndClasses.match(clas))) { 63 for (i = classes.length; i--;) { 64 c = classes[i].slice(1); 65 if (!(classCache.g(c) || classCache.s(c, new RegExp('(^|\\s+)' + c + '(\\s+|$)'))).test(this.className)) { 66 return false; 67 } 68 } 69 } 70 if (wholeAttribute && !value) { 71 o = this.attributes; 72 for (k in o) { 73 if (Object.prototype.hasOwnProperty.call(o, k) && (o[k].name || k) == attribute) { 74 return this; 75 } 76 } 77 } 78 if (wholeAttribute && !checkAttr(qualifier, this.getAttribute(attribute) || '', value)) { 79 return false; 80 } 81 return this; 82 } 83 84 function loopAll(tokens) { 85 var r = [], token = tokens.pop(), intr = q(token), tag = intr[1] || '*', i, l; 86 var root = tokens.length && (m = tokens[0].match(idOnly)) ? doc.getElementById(m[1]) : doc, 87 els = root.getElementsByTagName(tag); 88 for (i = 0, l = els.length; i < l; i++) { 89 el = els[i]; 90 if (item = interpret.apply(el, intr)) { 91 r.push(item); 92 } 93 } 94 return r; 95 } 96 97 function clean(s) { 98 return cleanCache.g(s) || cleanCache.s(s, s.replace(/([\.\*\+\?\^\$\{\}\(\)\|\[\]\/\\])/g, '\\$1')); 99 } 100 101 function checkAttr(qualify, actual, val) { 102 switch (qualify) { 103 case '=': 104 return actual == 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('$' + clean(val)))); 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, 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 = r.length; j--;) { 128 node = r[j]; 129 p = node; 130 // loop through each token 131 for (i = tokens.length; i--;) { 132 parents: 133 while (p !== html && (p = p.parentNode)) { // loop through parent nodes 134 if (found = interpret.apply(p, q(tokens[i]))) { 135 break parents; 136 } 137 } 138 } 139 found && ret.push(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 true; 155 } 156 } 157 return false; 158 }; 159 160 var qwery = function () { 161 function qsa(selector, root) { 162 root = (typeof root == 'string') ? qsa(root)[0] : root; 163 if (m = selector.match(idOnly)) { 164 return [doc.getElementById(m[1])]; 165 } 166 if (doc.getElementsByClassName && (m = selector.match(classOnly))) { 167 return array((root || doc).getElementsByClassName(m[1]), 0); 168 } 169 return array((root || doc).querySelectorAll(selector), 0); 170 } 171 172 // return fast. boosh. 173 if (doc.querySelector && doc.querySelectorAll) { 174 return qsa; 175 } 176 177 return function (selector, root) { 178 root = (typeof root == 'string') ? qwery(root)[0] : (root || doc); 179 var i, result = [], collections = [], element; 180 if (m = selector.match(idOnly)) { 181 return [doc.getElementById(m[1])]; 182 } 183 if (m = selector.match(tagOnly)) { 184 return root.getElementsByTagName(m[1]); 185 } 186 if (m = selector.match(tagAndOrClass)) { 187 items = root.getElementsByTagName(m[1] || '*'); 188 r = classCache.g(m[2]) || classCache.s(m[2], new RegExp('(^|\\s+)' + m[2] + '(\\s+|$)')); 189 for (i = items.length; i--;) { 190 r.test(items[i].className) && result.push(items[i]); 191 } 192 return result; 193 } 194 // here we allow combinator selectors: $('div,span'); 195 for (items = selector.split(','), i = items.length; item = items[--i];) { 196 collections[i] = _qwery(item); 197 } 198 199 for (i = collections.length; collection = collections[--i];) { 200 var ret = collection; 201 if (root !== doc) { 202 ret = []; 203 for (j = collection.length; element = collection[--j];) { 204 // make sure element is a descendent of root 205 isAncestor(element, root) && ret.push(element); 206 } 207 } 208 result = result.concat(ret); 209 } 210 return result; 211 }; 212 }(); 213 214 // being nice 215 var oldQwery = context.qwery; 216 qwery.noConflict = function () { 217 context.qwery = oldQwery; 218 return this; 219 }; 220 context.qwery = qwery; 221 222}(this, document);