xonecas.io
142
St

startsInvasion / files

startsInvasion / libs / node_modules / bonzo / build / qwery / qwery.js

7.7 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, l,
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 = 0, l = r.length, k = 0; j < l; j++) {
134      node = r[j];
135      p = node;
136      // loop through each token
137      for (i = tokens.length; i--;) {
138        z: // loop through parent nodes
139        while (p !== html && (p = p.parentNode)) {
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, fn) {
167    var root = (typeof _root == 'string') ? fn(_root)[0] : (_root || doc);
168    if (isNode(selector)) {
169      return !_root || (isNode(root) && isAncestor(selector, root)) ? [selector] : [];
170    }
171    if (selector && typeof selector === 'object' && isFinite(selector.length)) {
172      return array(selector);
173    }
174    if (m = selector.match(idOnly)) {
175      return (el = doc.getElementById(m[1])) ? [el] : [];
176    }
177    if (m = selector.match(tagOnly)) {
178      return array(root.getElementsByTagName(m[1]));
179    }
180    return false;
181  }
182
183  function isNode(el) {
184    return (el === window || el && el.nodeType && el.nodeType.toString().match(/[19]/));
185  }
186
187  function qsa(selector, _root) {
188    var root = (typeof _root == 'string') ? qsa(_root)[0] : (_root || doc);
189    if (!root || !selector) {
190      return [];
191    }
192    if (m = boilerPlate(selector, _root, qsa)) {
193      return m;
194    }
195    if (doc.getElementsByClassName && (m = selector.match(classOnly))) {
196      return array((root).getElementsByClassName(m[1]));
197    }
198    return array((root).querySelectorAll(selector));
199  }
200
201  function uniq(ar) {
202    var a = [], i, j;
203    label:
204    for (i = 0; i < ar.length; i++) {
205      for (j = 0; j < a.length; j++) {
206        if (a[j] == ar[i]) {
207          continue label;
208        }
209      }
210      a[a.length] = ar[i];
211    }
212    return a;
213  }
214
215  var qwery = function () {
216    // return fast. boosh.
217    if (doc.querySelector && doc.querySelectorAll) {
218      return qsa;
219    }
220    return function (selector, _root) {
221      var root = (typeof _root == 'string') ? qwery(_root)[0] : (_root || doc);
222      if (!root || !selector) {
223        return [];
224      }
225      var i, l, result = [], collections = [], element;
226      if (m = boilerPlate(selector, _root, qwery)) {
227        return m;
228      }
229      if (m = selector.match(tagAndOrClass)) {
230        items = root.getElementsByTagName(m[1] || '*');
231        r = classCache.g(m[2]) || classCache.s(m[2], new RegExp('(^|\\s+)' + m[2] + '(\\s+|$)'));
232        for (i = 0, l = items.length, j = 0; i < l; i++) {
233          r.test(items[i].className) && (result[j++] = items[i]);
234        }
235        return result;
236      }
237      for (i = 0, items = selector.split(','), l = items.length; i < l; i++) {
238        collections[i] = _qwery(items[i]);
239      }
240      for (i = 0, l = collections.length; i < l && (collection = collections[i]); i++) {
241        var ret = collection;
242        if (root !== doc) {
243          ret = [];
244          for (j = 0, m = collection.length; j < m && (element = collection[j]); j++) {
245            // make sure element is a descendent of root
246            isAncestor(element, root) && ret.push(element);
247          }
248        }
249        result = result.concat(ret);
250      }
251      return uniq(result);
252    };
253  }();
254
255  qwery.uniq = uniq;
256  var oldQwery = context.qwery;
257  qwery.noConflict = function () {
258    context.qwery = oldQwery;
259    return this;
260  };
261  context.qwery = qwery;
262
263}(this, document);