ender-qwery.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*!
  2. * =======================================================
  3. * Ender: open module JavaScript framework
  4. * copyright Dustin Diaz & Jacob Thornton 2011 (@ded @fat)
  5. * https://ender.no.de
  6. * License MIT
  7. * Module's individual licenses still apply
  8. * Build: ender build qwery
  9. * =======================================================
  10. */
  11. /*!
  12. * Ender-JS: open module JavaScript framework (client-lib)
  13. * copyright Dustin Diaz & Jacob Thornton 2011 (@ded @fat)
  14. * https://ender.no.de
  15. * License MIT
  16. */
  17. !function (context) {
  18. // Implements simple module system
  19. // losely based on CommonJS Modules spec v1.1.1
  20. // ============================================
  21. var modules = {};
  22. function require (identifier) {
  23. var module = modules[identifier] || window[identifier];
  24. if (!module) throw new Error("Requested module has not been defined.");
  25. return module;
  26. }
  27. function provide (name, what) {
  28. return modules[name] = what;
  29. }
  30. context['provide'] = provide;
  31. context['require'] = require;
  32. // Implements Ender's $ global access object
  33. // =========================================
  34. function aug(o, o2) {
  35. for (var k in o2) {
  36. k != 'noConflict' && k != '_VERSION' && (o[k] = o2[k]);
  37. }
  38. return o;
  39. }
  40. function boosh(s, r, els) {
  41. // string || node || nodelist || window
  42. if (ender._select && (typeof s == 'string' || s.nodeName || s.length && 'item' in s || s == window)) {
  43. els = ender._select(s, r);
  44. els.selector = s;
  45. } else {
  46. els = isFinite(s.length) ? s : [s];
  47. }
  48. return aug(els, boosh);
  49. }
  50. function ender(s, r) {
  51. return boosh(s, r);
  52. }
  53. aug(ender, {
  54. _VERSION: '0.2.5',
  55. ender: function (o, chain) {
  56. aug(chain ? boosh : ender, o);
  57. },
  58. fn: context.$ && context.$.fn || {} // for easy compat to jQuery plugins
  59. });
  60. aug(boosh, {
  61. forEach: function (fn, scope, i) {
  62. // opt out of native forEach so we can intentionally call our own scope
  63. // defaulting to the current item and be able to return self
  64. for (i = 0, l = this.length; i < l; ++i) {
  65. i in this && fn.call(scope || this[i], this[i], i, this);
  66. }
  67. // return self for chaining
  68. return this;
  69. },
  70. $: ender // handy reference to self
  71. });
  72. var old = context.$;
  73. ender.noConflict = function () {
  74. context.$ = old;
  75. return this;
  76. };
  77. (typeof module !== 'undefined') && module.exports && (module.exports = ender);
  78. // use subscript notation as extern for Closure compilation
  79. context['ender'] = context['$'] = ender;
  80. }(this);
  81. !function () {
  82. var module = { exports: {} }, exports = module.exports;
  83. /*!
  84. * Qwery - A Blazing Fast query selector engine
  85. * https://github.com/ded/qwery
  86. * copyright Dustin Diaz & Jacob Thornton 2011
  87. * MIT License
  88. */
  89. !function (context, doc) {
  90. var c, i, j, k, l, m, o, p, r, v,
  91. el, node, len, found, classes, item, items, token,
  92. html = doc.documentElement,
  93. id = /#([\w\-]+)/,
  94. clas = /\.[\w\-]+/g,
  95. idOnly = /^#([\w\-]+$)/,
  96. classOnly = /^\.([\w\-]+)$/,
  97. tagOnly = /^([\w\-]+)$/,
  98. tagAndOrClass = /^([\w]+)?\.([\w\-]+)$/,
  99. normalizr = /\s*([\s\+\~>])\s*/g,
  100. splitters = /[\s\>\+\~]/,
  101. splittersMore = /(?![\s\w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^'"]*\])/,
  102. dividers = new RegExp('(' + splitters.source + ')' + splittersMore.source, 'g'),
  103. tokenizr = new RegExp(splitters.source + splittersMore.source),
  104. specialChars = /([.*+?\^=!:${}()|\[\]\/\\])/g,
  105. simple = /^([a-z0-9]+)?(?:([\.\#]+[\w\-\.#]+)?)/,
  106. attr = /\[([\w\-]+)(?:([\|\^\$\*\~]?\=)['"]?([ \w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^]+)["']?)?\]/,
  107. pseudo = /:([\w\-]+)(\(['"]?(\w+)['"]?\))?/,
  108. chunker = new RegExp(simple.source + '(' + attr.source + ')?' + '(' + pseudo.source + ')?'),
  109. walker = {
  110. ' ': function (node) {
  111. return node && node !== html && node.parentNode
  112. },
  113. '>': function (node, contestant) {
  114. return node && node.parentNode == contestant.parentNode && node.parentNode;
  115. },
  116. '~': function (node) {
  117. return node && node.previousSibling;
  118. },
  119. '+': function (node, contestant, p1, p2) {
  120. if (!node) {
  121. return false;
  122. }
  123. p1 = previous(node);
  124. p2 = previous(contestant);
  125. return p1 && p2 && p1 == p2 && p1;
  126. }
  127. };
  128. function cache() {
  129. this.c = {};
  130. }
  131. cache.prototype = {
  132. g: function (k) {
  133. return this.c[k] || undefined;
  134. },
  135. s: function (k, v) {
  136. this.c[k] = v;
  137. return v;
  138. }
  139. };
  140. var classCache = new cache(),
  141. cleanCache = new cache(),
  142. attrCache = new cache(),
  143. tokenCache = new cache();
  144. function array(ar) {
  145. r = [];
  146. for (i = 0, len = ar.length; i < len; i++) {
  147. r[i] = ar[i];
  148. }
  149. return r;
  150. }
  151. function previous(n) {
  152. while (n = n.previousSibling) {
  153. if (n.nodeType == 1) {
  154. break;
  155. }
  156. }
  157. return n
  158. }
  159. function q(query) {
  160. return query.match(chunker);
  161. }
  162. // this next method expect at most these args
  163. // given => div.hello[title="world"]:foo('bar')
  164. // div.hello[title="world"]:foo('bar'), div, .hello, [title="world"], title, =, world, :foo('bar'), foo, ('bar'), bar]
  165. function interpret(whole, tag, idsAndClasses, wholeAttribute, attribute, qualifier, value, wholePseudo, pseudo, wholePseudoVal, pseudoVal) {
  166. var m, c, k;
  167. if (tag && this.tagName.toLowerCase() !== tag) {
  168. return false;
  169. }
  170. if (idsAndClasses && (m = idsAndClasses.match(id)) && m[1] !== this.id) {
  171. return false;
  172. }
  173. if (idsAndClasses && (classes = idsAndClasses.match(clas))) {
  174. for (i = classes.length; i--;) {
  175. c = classes[i].slice(1);
  176. if (!(classCache.g(c) || classCache.s(c, new RegExp('(^|\\s+)' + c + '(\\s+|$)'))).test(this.className)) {
  177. return false;
  178. }
  179. }
  180. }
  181. if (pseudo && qwery.pseudos[pseudo] && !qwery.pseudos[pseudo](this, pseudoVal)) {
  182. return false;
  183. }
  184. if (wholeAttribute && !value) {
  185. o = this.attributes;
  186. for (k in o) {
  187. if (Object.prototype.hasOwnProperty.call(o, k) && (o[k].name || k) == attribute) {
  188. return this;
  189. }
  190. }
  191. }
  192. if (wholeAttribute && !checkAttr(qualifier, this.getAttribute(attribute) || '', value)) {
  193. return false;
  194. }
  195. return this;
  196. }
  197. function clean(s) {
  198. return cleanCache.g(s) || cleanCache.s(s, s.replace(specialChars, '\\$1'));
  199. }
  200. function checkAttr(qualify, actual, val) {
  201. switch (qualify) {
  202. case '=':
  203. return actual == val;
  204. case '^=':
  205. return actual.match(attrCache.g('^=' + val) || attrCache.s('^=' + val, new RegExp('^' + clean(val))));
  206. case '$=':
  207. return actual.match(attrCache.g('$=' + val) || attrCache.s('$=' + val, new RegExp(clean(val) + '$')));
  208. case '*=':
  209. return actual.match(attrCache.g(val) || attrCache.s(val, new RegExp(clean(val))));
  210. case '~=':
  211. return actual.match(attrCache.g('~=' + val) || attrCache.s('~=' + val, new RegExp('(?:^|\\s+)' + clean(val) + '(?:\\s+|$)')));
  212. case '|=':
  213. return actual.match(attrCache.g('|=' + val) || attrCache.s('|=' + val, new RegExp('^' + clean(val) + '(-|$)')));
  214. }
  215. return 0;
  216. }
  217. function _qwery(selector) {
  218. var r = [], ret = [], i, j = 0, k, l, m, p, token, tag, els, root, intr, item, children,
  219. tokens = tokenCache.g(selector) || tokenCache.s(selector, selector.split(tokenizr)),
  220. dividedTokens = selector.match(dividers), dividedToken;
  221. tokens = tokens.slice(0); // this makes a copy of the array so the cached original is not effected
  222. if (!tokens.length) {
  223. return r;
  224. }
  225. token = tokens.pop();
  226. root = tokens.length && (m = tokens[tokens.length - 1].match(idOnly)) ? doc.getElementById(m[1]) : doc;
  227. if (!root) {
  228. return r;
  229. }
  230. intr = q(token);
  231. els = dividedTokens && /^[+~]$/.test(dividedTokens[dividedTokens.length - 1]) ? function (r) {
  232. while (root = root.nextSibling) {
  233. root.nodeType == 1 && (intr[1] ? intr[1] == root.tagName.toLowerCase() : 1) && r.push(root)
  234. }
  235. return r
  236. }([]) :
  237. root.getElementsByTagName(intr[1] || '*');
  238. for (i = 0, l = els.length; i < l; i++) {
  239. if (item = interpret.apply(els[i], intr)) {
  240. r[j++] = item;
  241. }
  242. }
  243. if (!tokens.length) {
  244. return r;
  245. }
  246. // loop through all descendent tokens
  247. for (j = 0, l = r.length, k = 0; j < l; j++) {
  248. p = r[j];
  249. // loop through each token backwards crawling up tree
  250. for (i = tokens.length; i--;) {
  251. // loop through parent nodes
  252. while (p = walker[dividedTokens[i]](p, r[j])) {
  253. if (found = interpret.apply(p, q(tokens[i]))) {
  254. break;
  255. }
  256. }
  257. }
  258. found && (ret[k++] = r[j]);
  259. }
  260. return ret;
  261. }
  262. function boilerPlate(selector, _root, fn) {
  263. var root = (typeof _root == 'string') ? fn(_root)[0] : (_root || doc);
  264. if (selector === window || isNode(selector)) {
  265. return !_root || (selector !== window && isNode(root) && isAncestor(selector, root)) ? [selector] : [];
  266. }
  267. if (selector && typeof selector === 'object' && isFinite(selector.length)) {
  268. return array(selector);
  269. }
  270. if (m = selector.match(idOnly)) {
  271. return (el = doc.getElementById(m[1])) ? [el] : [];
  272. }
  273. if (m = selector.match(tagOnly)) {
  274. return array(root.getElementsByTagName(m[1]));
  275. }
  276. return false;
  277. }
  278. function isNode(el) {
  279. return (el && el.nodeType && (el.nodeType == 1 || el.nodeType == 9));
  280. }
  281. function uniq(ar) {
  282. var a = [], i, j;
  283. label:
  284. for (i = 0; i < ar.length; i++) {
  285. for (j = 0; j < a.length; j++) {
  286. if (a[j] == ar[i]) {
  287. continue label;
  288. }
  289. }
  290. a[a.length] = ar[i];
  291. }
  292. return a;
  293. }
  294. function qwery(selector, _root) {
  295. var root = (typeof _root == 'string') ? qwery(_root)[0] : (_root || doc);
  296. if (!root || !selector) {
  297. return [];
  298. }
  299. if (m = boilerPlate(selector, _root, qwery)) {
  300. return m;
  301. }
  302. return select(selector, root);
  303. }
  304. var isAncestor = 'compareDocumentPosition' in html ?
  305. function (element, container) {
  306. return (container.compareDocumentPosition(element) & 16) == 16;
  307. } : 'contains' in html ?
  308. function (element, container) {
  309. container = container == doc || container == window ? html : container;
  310. return container !== element && container.contains(element);
  311. } :
  312. function (element, container) {
  313. while (element = element.parentNode) {
  314. if (element === container) {
  315. return 1;
  316. }
  317. }
  318. return 0;
  319. },
  320. select = (doc.querySelector && doc.querySelectorAll) ?
  321. function (selector, root) {
  322. if (doc.getElementsByClassName && (m = selector.match(classOnly))) {
  323. return array((root).getElementsByClassName(m[1]));
  324. }
  325. return array((root).querySelectorAll(selector));
  326. } :
  327. function (selector, root) {
  328. selector = selector.replace(normalizr, '$1');
  329. var result = [], collection, collections = [], i;
  330. if (m = selector.match(tagAndOrClass)) {
  331. items = root.getElementsByTagName(m[1] || '*');
  332. r = classCache.g(m[2]) || classCache.s(m[2], new RegExp('(^|\\s+)' + m[2] + '(\\s+|$)'));
  333. for (i = 0, l = items.length, j = 0; i < l; i++) {
  334. r.test(items[i].className) && (result[j++] = items[i]);
  335. }
  336. return result;
  337. }
  338. for (i = 0, items = selector.split(','), l = items.length; i < l; i++) {
  339. collections[i] = _qwery(items[i]);
  340. }
  341. for (i = 0, l = collections.length; i < l && (collection = collections[i]); i++) {
  342. var ret = collection;
  343. if (root !== doc) {
  344. ret = [];
  345. for (j = 0, m = collection.length; j < m && (element = collection[j]); j++) {
  346. // make sure element is a descendent of root
  347. isAncestor(element, root) && ret.push(element);
  348. }
  349. }
  350. result = result.concat(ret);
  351. }
  352. return uniq(result);
  353. };
  354. qwery.uniq = uniq;
  355. qwery.pseudos = {};
  356. var oldQwery = context.qwery;
  357. qwery.noConflict = function () {
  358. context.qwery = oldQwery;
  359. return this;
  360. };
  361. context['qwery'] = qwery;
  362. }(this, document);
  363. provide("qwery", module.exports);
  364. !function (doc) {
  365. var q = qwery.noConflict();
  366. var table = 'table',
  367. nodeMap = {
  368. thead: table,
  369. tbody: table,
  370. tfoot: table,
  371. tr: 'tbody',
  372. th: 'tr',
  373. td: 'tr',
  374. fieldset: 'form',
  375. option: 'select'
  376. }
  377. function create(node, root) {
  378. var tag = /^<([^\s>]+)/.exec(node)[1]
  379. var el = (root || doc).createElement(nodeMap[tag] || 'div'), els = [];
  380. el.innerHTML = node;
  381. var nodes = el.childNodes;
  382. el = el.firstChild;
  383. els.push(el);
  384. while (el = el.nextSibling) {
  385. (el.nodeType == 1) && els.push(el);
  386. }
  387. return els;
  388. }
  389. $._select = function (s, r) {
  390. return /^\s*</.test(s) ? create(s, r) : q(s, r);
  391. };
  392. $.pseudos = q.pseudos;
  393. $.ender({
  394. find: function (s) {
  395. var r = [], i, l, j, k, els;
  396. for (i = 0, l = this.length; i < l; i++) {
  397. els = q(s, this[i]);
  398. for (j = 0, k = els.length; j < k; j++) {
  399. r.push(els[j]);
  400. }
  401. }
  402. return $(q.uniq(r));
  403. }
  404. , and: function (s) {
  405. var plus = $(s);
  406. for (var i = this.length, j = 0, l = this.length + plus.length; i < l; i++, j++) {
  407. this[i] = plus[j];
  408. }
  409. return this;
  410. }
  411. }, true);
  412. }(document);
  413. }();