Permalink
Browse files

remove window from isNode method -- method forking for qwery

  • Loading branch information...
1 parent 8db387b commit 25284520db40a9c92b2f41041909a65bc57b3fe8 @fat fat committed May 8, 2011
Showing with 111 additions and 135 deletions.
  1. +55 −67 qwery.js
  2. +1 −1 qwery.min.js
  3. +55 −67 src/qwery.js
View
122 qwery.js
@@ -147,26 +147,10 @@
return ret;
}
- var isAncestor = 'compareDocumentPosition' in html ?
- function (element, container) {
- return (container.compareDocumentPosition(element) & 16) == 16;
- } : 'contains' in html ?
- function (element, container) {
- return container !== element && container.contains(element);
- } :
- function (element, container) {
- while (element = element.parentNode) {
- if (element === container) {
- return 1;
- }
- }
- return 0;
- };
-
function boilerPlate(selector, _root, fn) {
var root = (typeof _root == 'string') ? fn(_root)[0] : (_root || doc);
- if (isNode(selector)) {
- return !_root || (isNode(root) && isAncestor(selector, root)) ? [selector] : [];
+ if (selector === window || isNode(selector)) {
+ return !_root || (selector !== window && isNode(root) && isAncestor(selector, root)) ? [selector] : [];
}
if (selector && typeof selector === 'object' && isFinite(selector.length)) {
return array(selector);
@@ -181,21 +165,7 @@
}
function isNode(el) {
- return (el === window || el && el.nodeType && el.nodeType.toString().match(/[19]/));
- }
-
- function qsa(selector, _root) {
- var root = (typeof _root == 'string') ? qsa(_root)[0] : (_root || doc);
- if (!root || !selector) {
- return [];
- }
- if (m = boilerPlate(selector, _root, qsa)) {
- return m;
- }
- if (doc.getElementsByClassName && (m = selector.match(classOnly))) {
- return array((root).getElementsByClassName(m[1]));
- }
- return array((root).querySelectorAll(selector));
+ return (el && el.nodeType == 1 || el.nodeType == 9);
}
function uniq(ar) {
@@ -212,45 +182,63 @@
return a;
}
- var qwery = function () {
- // return fast. boosh.
- if (doc.querySelector && doc.querySelectorAll) {
- return qsa;
+ function qwery(selector, _root) {
+ var root = (typeof _root == 'string') ? qwery(_root)[0] : (_root || doc);
+ if (!root || !selector) {
+ return [];
}
- return function (selector, _root) {
- var root = (typeof _root == 'string') ? qwery(_root)[0] : (_root || doc);
- if (!root || !selector) {
- return [];
- }
- var i, l, result = [], collections = [], element;
- if (m = boilerPlate(selector, _root, qwery)) {
- return m;
- }
- if (m = selector.match(tagAndOrClass)) {
- items = root.getElementsByTagName(m[1] || '*');
- r = classCache.g(m[2]) || classCache.s(m[2], new RegExp('(^|\\s+)' + m[2] + '(\\s+|$)'));
- for (i = 0, l = items.length, j = 0; i < l; i++) {
- r.test(items[i].className) && (result[j++] = items[i]);
+ if (m = boilerPlate(selector, _root, qwery)) {
+ return m;
+ }
+ return select(selector, root);
+ }
+
+ var isAncestor = 'compareDocumentPosition' in html ?
+ function (element, container) {
+ return (container.compareDocumentPosition(element) & 16) == 16;
+ } : 'contains' in html ?
+ function (element, container) {
+ return container !== element && container.contains(element);
+ } :
+ function (element, container) {
+ while (element = element.parentNode) {
+ if (element === container) {
+ return 1;
}
- return result;
}
- for (i = 0, items = selector.split(','), l = items.length; i < l; i++) {
- collections[i] = _qwery(items[i]);
+ return 0;
+ },
+
+ select = doc.querySelector && doc.querySelectorAll ? function (selector, root) {
+ if (doc.getElementsByClassName && (m = selector.match(classOnly))) {
+ return array((root).getElementsByClassName(m[1]));
+ }
+ return array((root).querySelectorAll(selector));
+ } : function (selector, root) {
+ if (m = selector.match(tagAndOrClass)) {
+ items = root.getElementsByTagName(m[1] || '*');
+ r = classCache.g(m[2]) || classCache.s(m[2], new RegExp('(^|\\s+)' + m[2] + '(\\s+|$)'));
+ for (i = 0, l = items.length, j = 0; i < l; i++) {
+ r.test(items[i].className) && (result[j++] = items[i]);
}
- for (i = 0, l = collections.length; i < l && (collection = collections[i]); i++) {
- var ret = collection;
- if (root !== doc) {
- ret = [];
- for (j = 0, m = collection.length; j < m && (element = collection[j]); j++) {
- // make sure element is a descendent of root
- isAncestor(element, root) && ret.push(element);
- }
+ return result;
+ }
+ for (i = 0, items = selector.split(','), l = items.length; i < l; i++) {
+ collections[i] = _qwery(items[i]);
+ }
+ for (i = 0, l = collections.length; i < l && (collection = collections[i]); i++) {
+ var ret = collection;
+ if (root !== doc) {
+ ret = [];
+ for (j = 0, m = collection.length; j < m && (element = collection[j]); j++) {
+ // make sure element is a descendent of root
+ isAncestor(element, root) && ret.push(element);
}
- result = result.concat(ret);
}
- return uniq(result);
- };
- }();
+ result = result.concat(ret);
+ }
+ return uniq(result);
+ };
qwery.uniq = uniq;
var oldQwery = context.qwery;
@@ -260,4 +248,4 @@
};
context.qwery = qwery;
-}(this, document);
+}(this, document);
View
2 qwery.min.js
@@ -5,4 +5,4 @@
* MIT License
*/
-!function(a,b){function W(a){var b=[],c,d;label:for(c=0;c<a.length;c++){for(d=0;d<b.length;d++)if(b[d]==a[c])continue label;b[b.length]=a[c]}return b}function V(a,c){var d=typeof c=="string"?V(c)[0]:c||b;if(!d||!a)return[];if(h=T(a,c,V))return h;if(b.getElementsByClassName&&(h=a.match(y)))return G(d.getElementsByClassName(h[1]));return G(d.querySelectorAll(a))}function U(a){return a===window||a&&a.nodeType&&a.nodeType.toString().match(/[19]/)}function T(a,c,d){var e=typeof c=="string"?d(c)[0]:c||b;if(U(a))return!c||U(e)&&S(a,e)?[a]:[];if(a&&typeof a=="object"&&isFinite(a.length))return G(a);if(h=a.match(x))return(m=b.getElementById(h[1]))?[m]:[];if(h=a.match(z))return G(e.getElementsByTagName(h[1]));return!1}function R(a){var b=[],c=[],d,g,h=L.g(a)||L.s(a,a.split(C));h=h.slice(0);if(!h.length)return b;b=O(h);if(!h.length)return b;for(e=0,g=b.length,f=0;e<g;e++){n=b[e],j=n;for(d=h.length;d--;)z:while(j!==B&&(j=j.parentNode))if(p=N.apply(j,M(h[d])))break z;p&&(c[f++]=n)}return c}function Q(a,b,c){switch(a){case"=":return b==c;case"^=":return b.match(K.g("^="+c)||K.s("^="+c,new RegExp("^"+P(c))));case"$=":return b.match(K.g("$="+c)||K.s("$="+c,new RegExp(P(c)+"$")));case"*=":return b.match(K.g(c)||K.s(c,new RegExp(P(c))));case"~=":return b.match(K.g("~="+c)||K.s("~="+c,new RegExp("(?:^|\\s+)"+P(c)+"(?:\\s+|$)")));case"|=":return b.match(K.g("|="+c)||K.s("|="+c,new RegExp("^"+P(c)+"(-|$)")))}return!1}function P(a){return J.g(a)||J.s(a,a.replace(/([.*+?\^=!:${}()|\[\]\/\\])/g,"\\$1"))}function O(a){var c=[],d=a.pop(),e=M(d),f=e[1]||"*",g,i,j,k=a.length&&(h=a[0].match(x))?b.getElementById(h[1]):b;if(!k)return c;j=k.getElementsByTagName(f);for(g=0,i=j.length;g<i;g++)m=j[g],(r=N.apply(m,e))&&c.push(r);return c}function N(a,b,c,e,f,g,h){var j,k,l;if(b&&this.tagName.toLowerCase()!==b)return!1;if(c&&(j=c.match(v))&&j[1]!==this.id)return!1;if(c&&(q=c.match(w)))for(d=q.length;d--;){k=q[d].slice(1);if(!(I.g(k)||I.s(k,new RegExp("(^|\\s+)"+k+"(\\s+|$)"))).test(this.className))return!1}if(e&&!h){i=this.attributes;for(l in i)if(Object.prototype.hasOwnProperty.call(i,l)&&(i[l].name||l)==f)return this}if(e&&!Q(g,this.getAttribute(f)||"",h))return!1;return this}function M(a){return a.match(F)}function G(a){k=[];for(d=0,o=a.length;d<o;d++)k[d]=a[d];return k}var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v=/#([\w\-]+)/,w=/\.[\w\-]+/g,x=/^#([\w\-]+$)/,y=/^\.([\w\-]+)$/,z=/^([\w\-]+)$/,A=/^([\w]+)?\.([\w\-]+)$/,B=b.documentElement,C=/\s(?![\s\w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^'"]*\])/,D=/^([a-z0-9]+)?(?:([\.\#]+[\w\-\.#]+)?)/,E=/\[([\w\-]+)(?:([\|\^\$\*\~]?\=)['"]?([ \w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^]+)["']?)?\]/,F=new RegExp(D.source+"("+E.source+")?"),H=function(){this.c={}};H.prototype={g:function(a){return this.c[a]||undefined},s:function(a,b){this.c[a]=b;return b}};var I=new H,J=new H,K=new H,L=new H,S="compareDocumentPosition"in B?function(a,b){return(b.compareDocumentPosition(a)&16)==16}:"contains"in B?function(a,b){return b!==a&&b.contains(a)}:function(a,b){while(a=a.parentNode)if(a===b)return 1;return 0},X=function(){if(b.querySelector&&b.querySelectorAll)return V;return function(a,c){var d=typeof c=="string"?X(c)[0]:c||b;if(!d||!a)return[];var f,g,i=[],j=[],l;if(h=T(a,c,X))return h;if(h=a.match(A)){s=d.getElementsByTagName(h[1]||"*"),k=I.g(h[2])||I.s(h[2],new RegExp("(^|\\s+)"+h[2]+"(\\s+|$)"));for(f=0,g=s.length,e=0;f<g;f++)k.test(s[f].className)&&(i[e++]=s[f]);return i}for(f=0,s=a.split(","),g=s.length;f<g;f++)j[f]=R(s[f]);for(f=0,g=j.length;f<g&&(u=j[f]);f++){var m=u;if(d!==b){m=[];for(e=0,h=u.length;e<h&&(l=u[e]);e++)S(l,d)&&m.push(l)}i=i.concat(m)}return W(i)}}();X.uniq=W;var Y=a.qwery;X.noConflict=function(){a.qwery=Y;return this},a.qwery=X}(this,document)
+!function(a,b){function V(a,c){var d=typeof c=="string"?V(c)[0]:c||b;if(!d||!a)return[];if(h=S(a,c,V))return h;return X(a,d)}function U(a){var b=[],c,d;label:for(c=0;c<a.length;c++){for(d=0;d<b.length;d++)if(b[d]==a[c])continue label;b[b.length]=a[c]}return b}function T(a){return a&&a.nodeType==1||a.nodeType==9}function S(a,c,d){var e=typeof c=="string"?d(c)[0]:c||b;if(a===window||T(a))return!c||a!==window&&T(e)&&W(a,e)?[a]:[];if(a&&typeof a=="object"&&isFinite(a.length))return G(a);if(h=a.match(x))return(m=b.getElementById(h[1]))?[m]:[];if(h=a.match(z))return G(e.getElementsByTagName(h[1]));return!1}function R(a){var b=[],c=[],d,g,h=L.g(a)||L.s(a,a.split(C));h=h.slice(0);if(!h.length)return b;b=O(h);if(!h.length)return b;for(e=0,g=b.length,f=0;e<g;e++){n=b[e],j=n;for(d=h.length;d--;)z:while(j!==B&&(j=j.parentNode))if(p=N.apply(j,M(h[d])))break z;p&&(c[f++]=n)}return c}function Q(a,b,c){switch(a){case"=":return b==c;case"^=":return b.match(K.g("^="+c)||K.s("^="+c,new RegExp("^"+P(c))));case"$=":return b.match(K.g("$="+c)||K.s("$="+c,new RegExp(P(c)+"$")));case"*=":return b.match(K.g(c)||K.s(c,new RegExp(P(c))));case"~=":return b.match(K.g("~="+c)||K.s("~="+c,new RegExp("(?:^|\\s+)"+P(c)+"(?:\\s+|$)")));case"|=":return b.match(K.g("|="+c)||K.s("|="+c,new RegExp("^"+P(c)+"(-|$)")))}return!1}function P(a){return J.g(a)||J.s(a,a.replace(/([.*+?\^=!:${}()|\[\]\/\\])/g,"\\$1"))}function O(a){var c=[],d=a.pop(),e=M(d),f=e[1]||"*",g,i,j,k=a.length&&(h=a[0].match(x))?b.getElementById(h[1]):b;if(!k)return c;j=k.getElementsByTagName(f);for(g=0,i=j.length;g<i;g++)m=j[g],(r=N.apply(m,e))&&c.push(r);return c}function N(a,b,c,e,f,g,h){var j,k,l;if(b&&this.tagName.toLowerCase()!==b)return!1;if(c&&(j=c.match(v))&&j[1]!==this.id)return!1;if(c&&(q=c.match(w)))for(d=q.length;d--;){k=q[d].slice(1);if(!(I.g(k)||I.s(k,new RegExp("(^|\\s+)"+k+"(\\s+|$)"))).test(this.className))return!1}if(e&&!h){i=this.attributes;for(l in i)if(Object.prototype.hasOwnProperty.call(i,l)&&(i[l].name||l)==f)return this}if(e&&!Q(g,this.getAttribute(f)||"",h))return!1;return this}function M(a){return a.match(F)}function G(a){k=[];for(d=0,o=a.length;d<o;d++)k[d]=a[d];return k}var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v=/#([\w\-]+)/,w=/\.[\w\-]+/g,x=/^#([\w\-]+$)/,y=/^\.([\w\-]+)$/,z=/^([\w\-]+)$/,A=/^([\w]+)?\.([\w\-]+)$/,B=b.documentElement,C=/\s(?![\s\w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^'"]*\])/,D=/^([a-z0-9]+)?(?:([\.\#]+[\w\-\.#]+)?)/,E=/\[([\w\-]+)(?:([\|\^\$\*\~]?\=)['"]?([ \w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^]+)["']?)?\]/,F=new RegExp(D.source+"("+E.source+")?"),H=function(){this.c={}};H.prototype={g:function(a){return this.c[a]||undefined},s:function(a,b){this.c[a]=b;return b}};var I=new H,J=new H,K=new H,L=new H,W="compareDocumentPosition"in B?function(a,b){return(b.compareDocumentPosition(a)&16)==16}:"contains"in B?function(a,b){return b!==a&&b.contains(a)}:function(a,b){while(a=a.parentNode)if(a===b)return 1;return 0},X=b.querySelector&&b.querySelectorAll?function(a,c){if(b.getElementsByClassName&&(h=a.match(y)))return G(c.getElementsByClassName(h[1]));return G(c.querySelectorAll(a))}:function(a,c){if(h=a.match(A)){s=c.getElementsByTagName(h[1]||"*"),k=I.g(h[2])||I.s(h[2],new RegExp("(^|\\s+)"+h[2]+"(\\s+|$)"));for(d=0,g=s.length,e=0;d<g;d++)k.test(s[d].className)&&(result[e++]=s[d]);return result}for(d=0,s=a.split(","),g=s.length;d<g;d++)collections[d]=R(s[d]);for(d=0,g=collections.length;d<g&&(u=collections[d]);d++){var f=u;if(c!==b){f=[];for(e=0,h=u.length;e<h&&(element=u[e]);e++)W(element,c)&&f.push(element)}result=result.concat(f)}return U(result)};V.uniq=U;var Y=a.qwery;V.noConflict=function(){a.qwery=Y;return this},a.qwery=V}(this,document)
View
122 src/qwery.js
@@ -141,26 +141,10 @@
return ret;
}
- var isAncestor = 'compareDocumentPosition' in html ?
- function (element, container) {
- return (container.compareDocumentPosition(element) & 16) == 16;
- } : 'contains' in html ?
- function (element, container) {
- return container !== element && container.contains(element);
- } :
- function (element, container) {
- while (element = element.parentNode) {
- if (element === container) {
- return 1;
- }
- }
- return 0;
- };
-
function boilerPlate(selector, _root, fn) {
var root = (typeof _root == 'string') ? fn(_root)[0] : (_root || doc);
- if (isNode(selector)) {
- return !_root || (isNode(root) && isAncestor(selector, root)) ? [selector] : [];
+ if (selector === window || isNode(selector)) {
+ return !_root || (selector !== window && isNode(root) && isAncestor(selector, root)) ? [selector] : [];
}
if (selector && typeof selector === 'object' && isFinite(selector.length)) {
return array(selector);
@@ -175,21 +159,7 @@
}
function isNode(el) {
- return (el === window || el && el.nodeType && el.nodeType.toString().match(/[19]/));
- }
-
- function qsa(selector, _root) {
- var root = (typeof _root == 'string') ? qsa(_root)[0] : (_root || doc);
- if (!root || !selector) {
- return [];
- }
- if (m = boilerPlate(selector, _root, qsa)) {
- return m;
- }
- if (doc.getElementsByClassName && (m = selector.match(classOnly))) {
- return array((root).getElementsByClassName(m[1]));
- }
- return array((root).querySelectorAll(selector));
+ return (el && el.nodeType == 1 || el.nodeType == 9);
}
function uniq(ar) {
@@ -206,45 +176,63 @@
return a;
}
- var qwery = function () {
- // return fast. boosh.
- if (doc.querySelector && doc.querySelectorAll) {
- return qsa;
+ function qwery(selector, _root) {
+ var root = (typeof _root == 'string') ? qwery(_root)[0] : (_root || doc);
+ if (!root || !selector) {
+ return [];
}
- return function (selector, _root) {
- var root = (typeof _root == 'string') ? qwery(_root)[0] : (_root || doc);
- if (!root || !selector) {
- return [];
- }
- var i, l, result = [], collections = [], element;
- if (m = boilerPlate(selector, _root, qwery)) {
- return m;
- }
- if (m = selector.match(tagAndOrClass)) {
- items = root.getElementsByTagName(m[1] || '*');
- r = classCache.g(m[2]) || classCache.s(m[2], new RegExp('(^|\\s+)' + m[2] + '(\\s+|$)'));
- for (i = 0, l = items.length, j = 0; i < l; i++) {
- r.test(items[i].className) && (result[j++] = items[i]);
+ if (m = boilerPlate(selector, _root, qwery)) {
+ return m;
+ }
+ return select(selector, root);
+ }
+
+ var isAncestor = 'compareDocumentPosition' in html ?
+ function (element, container) {
+ return (container.compareDocumentPosition(element) & 16) == 16;
+ } : 'contains' in html ?
+ function (element, container) {
+ return container !== element && container.contains(element);
+ } :
+ function (element, container) {
+ while (element = element.parentNode) {
+ if (element === container) {
+ return 1;
}
- return result;
}
- for (i = 0, items = selector.split(','), l = items.length; i < l; i++) {
- collections[i] = _qwery(items[i]);
+ return 0;
+ },
+
+ select = doc.querySelector && doc.querySelectorAll ? function (selector, root) {
+ if (doc.getElementsByClassName && (m = selector.match(classOnly))) {
+ return array((root).getElementsByClassName(m[1]));
+ }
+ return array((root).querySelectorAll(selector));
@ded
ded May 11, 2011

this short circuits the rest of the boilerplate and slows down what would otherwise be faster with native getElementsByTagName and getElementById.

I suppose it'll be simple to merge this, then add back the boilerPlate check

@ded
ded May 11, 2011

nm. i'm a tool. i see the main qwery fn does that ahead of time. good catch. ran the tests, all pass. benchmarks still look good.

@fat
fat May 11, 2011

lol -- it's ok :D

+ } : function (selector, root) {
+ if (m = selector.match(tagAndOrClass)) {
+ items = root.getElementsByTagName(m[1] || '*');
+ r = classCache.g(m[2]) || classCache.s(m[2], new RegExp('(^|\\s+)' + m[2] + '(\\s+|$)'));
+ for (i = 0, l = items.length, j = 0; i < l; i++) {
+ r.test(items[i].className) && (result[j++] = items[i]);
}
- for (i = 0, l = collections.length; i < l && (collection = collections[i]); i++) {
- var ret = collection;
- if (root !== doc) {
- ret = [];
- for (j = 0, m = collection.length; j < m && (element = collection[j]); j++) {
- // make sure element is a descendent of root
- isAncestor(element, root) && ret.push(element);
- }
+ return result;
+ }
+ for (i = 0, items = selector.split(','), l = items.length; i < l; i++) {
+ collections[i] = _qwery(items[i]);
+ }
+ for (i = 0, l = collections.length; i < l && (collection = collections[i]); i++) {
+ var ret = collection;
+ if (root !== doc) {
+ ret = [];
+ for (j = 0, m = collection.length; j < m && (element = collection[j]); j++) {
+ // make sure element is a descendent of root
+ isAncestor(element, root) && ret.push(element);
}
- result = result.concat(ret);
}
- return uniq(result);
- };
- }();
+ result = result.concat(ret);
+ }
+ return uniq(result);
+ };
qwery.uniq = uniq;
var oldQwery = context.qwery;
@@ -254,4 +242,4 @@
};
context.qwery = qwery;
-}(this, document);
+}(this, document);

0 comments on commit 2528452

Please sign in to comment.