xonecas.io
142
St

startsInvasion / files

startsInvasion / libs / node_modules / bean / support / qwery / src / qwery.js

7.0 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,
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, k = 0; j--;) {
128      node = r[j];
129      p = node;
130      // loop through each token
131      for (i = tokens.length; i--;) {
132        z:
133        while (p !== html && (p = p.parentNode)) { // loop through parent nodes
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) {
161    if (m = selector.match(idOnly)) {
162      return (el = doc.getElementById(m[1])) ? [el] : [];
163    }
164    if (m = selector.match(tagOnly)) {
165      return array(root.getElementsByTagName(m[1]));
166    }
167    return false;
168  }
169
170  function qsa(selector, root) {
171    root = (typeof root == 'string') ? qsa(root)[0] : (root || doc);
172    if (!root) {
173      return [];
174    }
175    if (m = boilerPlate(selector, root)) {
176      return m;
177    }
178    if (doc.getElementsByClassName && (m = selector.match(classOnly))) {
179      return array((root).getElementsByClassName(m[1]));
180    }
181    return array((root).querySelectorAll(selector));
182  }
183
184  function uniq(ar) {
185    var a = [], i, j;
186    label:
187    for (i = 0; i < ar.length; i++) {
188      for (j = 0; j < a.length; j++) {
189        if (a[j] == ar[i]) {
190          continue label;
191        }
192      }
193      a[a.length] = ar[i];
194    }
195    return a;
196  }
197
198  var qwery = function () {
199    // return fast. boosh.
200    if (doc.querySelector && doc.querySelectorAll) {
201      return qsa;
202    }
203    return function (selector, root) {
204      root = (typeof root == 'string') ? qwery(root)[0] : (root || doc);
205      if (!root) {
206        return [];
207      }
208      var i, result = [], collections = [], element;
209      if (m = boilerPlate(selector, root)) {
210        return m;
211      }
212      if (m = selector.match(tagAndOrClass)) {
213        items = root.getElementsByTagName(m[1] || '*');
214        r = classCache.g(m[2]) || classCache.s(m[2], new RegExp('(^|\\s+)' + m[2] + '(\\s+|$)'));
215        for (i = items.length, j = 0; i--;) {
216          r.test(items[i].className) && (result[j++] = items[i]);
217        }
218        return result;
219      }
220      for (items = selector.split(','), i = items.length; i--;) {
221        collections[i] = _qwery(items[i]);
222      }
223      for (i = collections.length; collection = collections[--i];) {
224        var ret = collection;
225        if (root !== doc) {
226          ret = [];
227          for (j = collection.length; element = collection[--j];) {
228            // make sure element is a descendent of root
229            isAncestor(element, root) && ret.push(element);
230          }
231        }
232        result = result.concat(ret);
233      }
234      return uniq(result);
235    };
236  }();
237
238  // being nice
239  var oldQwery = context.qwery;
240  qwery.noConflict = function () {
241    context.qwery = oldQwery;
242    return this;
243  };
244  context.qwery = qwery;
245
246}(this, document);