From d85577c940f52603fcf8b1e7e01228addc1279fe Mon Sep 17 00:00:00 2001 From: Dustin Diaz Date: Thu, 24 Mar 2011 09:29:09 -0700 Subject: [PATCH] pass lint options --- qwery.js | 90 ++++++++++++++++++++------------------ qwery.min.js | 2 +- src/qwery.js | 2 +- src/sizzlr.js | 118 -------------------------------------------------- 4 files changed, 49 insertions(+), 163 deletions(-) delete mode 100644 src/sizzlr.js diff --git a/qwery.js b/qwery.js index dcde2ae..89ad0e4 100644 --- a/qwery.js +++ b/qwery.js @@ -64,28 +64,21 @@ } }; - // needle is possible descendent - // haystack is possible ancestor - function isAncestor(needle, haystack) { - - if (!haystack || !needle) { + function isAncestor(child, parent) { + if (!parent || !child) { return false; } - - if (haystack.contains && needle.nodeType) { - return haystack.contains(needle); + if (parent.contains && child.nodeType) { + return parent.contains(child); } - - else if (haystack.compareDocumentPosition && needle.nodeType) { - return !!(haystack.compareDocumentPosition(needle) & 16); - } else if (needle.nodeType) { - return false; + else if (parent.compareDocumentPosition && child.nodeType) { + return !!(parent.compareDocumentPosition(child) & 16); } return false; } - function _qwery(selector, root) { + function _qwery(selector) { var tokens = selector.split(' '), bits, tagName, h, i, j, k, l, len, found, foundCount, elements, currentContextIndex, currentContext = [doc]; @@ -173,43 +166,54 @@ return currentContext; } - function qwery(selector, root) { + var qwery = function () { + + // exception for pure classname selectors (it's faster) + var clas = /^\.([\w\-]+)$/, m; - if (!doc.getElementsByTagName) { - return []; + function qsa(selector, root) { + // taking for granted that every browser that supports qsa, also supports getElsByClsName + if (m = selector.match(clas)) { + return array((root || document).getElementsByClassName(m[1]), 0); + } + return array((root || document).querySelectorAll(selector), 0); } - root = root || document; - if (doc.querySelectorAll) { - // return immediately for browsers that know what they're doing - // method suggested by Mozilla https://developer.mozilla.org/En/Code_snippets/QuerySelector - return array(root.querySelectorAll(selector), 0); + + // return fast + if (document.querySelectorAll) { + return qsa; } - // these next two operations could really benefit from an accumulator (eg: map/each/accumulate) - var result = []; - // here we allow combinator selectors: $('div,span'); - var collections = _(selector.split(',')).map(function (selector) { - return _qwery(selector, root); - }); - - _(collections).each(function (collection) { - var ret = collection; - // allow contexts - if (root !== document) { - ret = []; - _(collection).each(function (element) { - // make sure element is a descendent of root - isAncestor(element, root) && ret.push(element); - }); - } + return function (selector, root) { + root = root || document; + + // these next two operations could really benefit from an accumulator (eg: map/each/accumulate) + var result = []; + // here we allow combinator selectors: $('div,span'); + var collections = _(selector.split(',')).map(function (selector) { + return _qwery(selector); + }); + + _(collections).each(function (collection) { + var ret = collection; + // allow contexts + if (root !== document) { + ret = []; + _(collection).each(function (element) { + // make sure element is a descendent of root + isAncestor(element, root) && ret.push(element); + }); + } - result = result.concat(ret); - }); - return result; - } + result = result.concat(ret); + }); + return result; + }; + }(); var oldQwery = context.qwery; + // being nice qwery.noConflict = function () { context.qwery = oldQwery; return this; diff --git a/qwery.min.js b/qwery.min.js index 0be0b3c..e068f1a 100644 --- a/qwery.min.js +++ b/qwery.min.js @@ -6,4 +6,4 @@ * MIT License */ -!function(a,b){function j(a,d){if(!b.getElementsByTagName)return[];d=d||document;if(b.querySelectorAll)return c(d.querySelectorAll(a),0);var e=[],g=f(a.split(",")).map(function(a){return i(a,d)});f(g).each(function(a){var b=a;d!==document&&(b=[],f(a).each(function(a){h(a,d)&&b.push(a)})),e=e.concat(b)});return e}function i(a,c){var e=a.split(" "),f,h,i,j,k,l,m,n,o,p,q,r,s=[b];for(j=0,m=e.length;j-1){f=token.split("#"),h=f[0];var t=b.getElementById(f[1]);if(h&&t.nodeName.toLowerCase()!=h)return[];s=[t];continue}if(token.indexOf(".")>-1){f=token.split("."),h=f[p=0],h=h||"*",o=[];for(i=0,n=s.length;i-1},"":function(a,b){return a.getAttribute(b)}},k=a.qwery;j.noConflict=function(){a.qwery=k;return this},a.qwery=j}(this,document) \ No newline at end of file +!function(a,b){function i(a){var c=a.split(" "),e,f,h,i,j,k,l,m,n,o,p,q,r=[b];for(i=0,l=c.length;i-1){e=token.split("#"),f=e[0];var s=b.getElementById(e[1]);if(f&&s.nodeName.toLowerCase()!=f)return[];r=[s];continue}if(token.indexOf(".")>-1){e=token.split("."),f=e[o=0],f=f||"*",n=[];for(h=0,m=r.length;h-1},"":function(a,b){return a.getAttribute(b)}},j=function(){function d(d,e){if(b=d.match(a))return c((e||document).getElementsByClassName(b[1]),0);return c((e||document).querySelectorAll(d),0)}var a=/^\.([\w\-]+)$/,b;if(document.querySelectorAll)return d;return function(a,b){b=b||document;var c=[],d=f(a.split(",")).map(function(a){return i(a)});f(d).each(function(a){var d=a;b!==document&&(d=[],f(a).each(function(a){h(a,b)&&d.push(a)})),c=c.concat(d)});return c}}(),k=a.qwery;j.noConflict=function(){a.qwery=k;return this},a.qwery=j}(this,document) \ No newline at end of file diff --git a/src/qwery.js b/src/qwery.js index 3880280..65df22d 100644 --- a/src/qwery.js +++ b/src/qwery.js @@ -71,7 +71,7 @@ } - function _qwery (selector) { + function _qwery(selector) { var tokens = selector.split(' '), bits, tagName, h, i, j, k, l, len, found, foundCount, elements, currentContextIndex, currentContext = [doc]; diff --git a/src/sizzlr.js b/src/sizzlr.js deleted file mode 100644 index c08a761..0000000 --- a/src/sizzlr.js +++ /dev/null @@ -1,118 +0,0 @@ -function makeArray(array, results) { - array = Array.prototype.slice.call(array, 0); - - if (results) { - results.push.apply(results, array); - return results; - } - - return array; -} - -try { - Array.prototype.slice.call(document.documentElement.childNodes, 0)[0].nodeType; -} catch (e) { - makeArray = function (array, results) { - var i = 0, - ret = results || []; - - if (toString.call(array) === "[object Array]") { - Array.prototype.push.apply(ret, array); - - } else { - if (typeof array.length === "number") { - for (var l = array.length; i < l; i++) { - ret.push(array[i]); - } - - } else { - for (; array[i]; i++) { - ret.push(array[i]); - } - } - } - - return ret; - }; -} - -QuerySelectorAll logic courtesy of SIZZLE.js -if (document.querySelectorAll) { - - !function () { - var div = document.createElement("div"), - id = "__qworry__"; - - div.innerHTML = "

"; - - if (div.querySelectorAll && div.querySelectorAll(".TEST").length === 0) { - return; - } - - context.getElementsBySelector = function (query, context, extra, seed) { - - context = context || document; - - var match = /^(\w+$)|^\.([\w\-]+$)|^#([\w\-]+$)/.exec(query); - - if (match && (context.nodeType === 1 || context.nodeType === 9)) { - if (match[1]) { - return makeArray(context.getElementsByTagName(query), extra); - } else if (match[2] && Expr.find.CLASS && context.getElementsByClassName) { - return makeArray(context.getElementsByClassName(match[2]), extra); - } - } - - if (context.nodeType === 9) { - if (query === "body" && context.body) { - return makeArray([context.body], extra); - } else if (match && match[3]) { - var elem = context.getElementById(match[3]); - if (elem && elem.parentNode) { - if (elem.id === match[3]) { - return makeArray([elem], extra); - } - } else { - return makeArray([], extra); - } - } - try { - return makeArray(context.querySelectorAll(query), extra); - } catch (qsaError) {} - - //Thanks to Andrew Dupont for the technique - } else if (context.nodeType === 1 && context.nodeName.toLowerCase() !== "object") { - var oldContext = context, - old = context.getAttribute("id"), - nid = old || id, - hasParent = context.parentNode, - relativeHierarchySelector = /^\s*[+~]/.test(query); - - if (!old) { - context.setAttribute("id", nid); - } else { - nid = nid.replace(/'/g, "\\$&"); - } - - if (relativeHierarchySelector && hasParent) { - context = context.parentNode; - } - - try { - if (!relativeHierarchySelector || hasParent) { - return makeArray(context.querySelectorAll("[id='" + nid + "'] " + query), extra); - } - } catch (pseudoError) { - } finally { - if (!old) { - oldContext.removeAttribute("id"); - } - } - } - - return []; - }; - div = null; - }(); - -} \ No newline at end of file