Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow passing nodes directly into qwery

  • Loading branch information...
commit 654d7e21182950a2af67f5a8f6972bb446014830 1 parent df6a440
F A T fat authored
Showing with 41 additions and 9 deletions.
  1. +14 −4 qwery.js
  2. +1 −1  qwery.min.js
  3. +14 −4 src/qwery.js
  4. +12 −0 tests/tests.js
18 qwery.js
View
@@ -173,8 +173,15 @@
return false;
}
- function qsa(selector, root) {
- root = (typeof root == 'string') ? qsa(root)[0] : (root || doc);
+ 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 (isNode(selector)) {
+ return !_root || isAncestor(selector, root) ? [selector] : [];
+ }
if (!root) {
return [];
}
@@ -206,8 +213,11 @@
if (doc.querySelector && doc.querySelectorAll) {
return qsa;
}
- return function (selector, root) {
- root = (typeof root == 'string') ? qwery(root)[0] : (root || doc);
+ return function (selector, _root) {
+ var root = (typeof _root == 'string') ? qwery(_root)[0] : (_root || doc);
+ if (isNode(selector)) {
+ return !_root || isAncestor(selector, root) ? [selector] : [];
+ }
if (!root) {
return [];
}
2  qwery.min.js
View
@@ -5,4 +5,4 @@
* MIT License
*/
-!function(a,b){function V(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 U(a,c){c=typeof c=="string"?U(c)[0]:c||b;if(!c)return[];if(h=T(a,c))return h;if(b.getElementsByClassName&&(h=a.match(y)))return G(c.getElementsByClassName(h[1]));return G(c.querySelectorAll(a))}function T(a,c){if(h=a.match(x))return(m=b.getElementById(h[1]))?[m]:[];if(h=a.match(z))return G(c.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},W=function(){if(b.querySelector&&b.querySelectorAll)return U;return function(a,c){c=typeof c=="string"?W(c)[0]:c||b;if(!c)return[];var d,f,g=[],i=[],j;if(h=T(a,c))return h;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,f=s.length,e=0;d<f;d++)k.test(s[d].className)&&(g[e++]=s[d]);return g}for(d=0,s=a.split(","),f=s.length;d<f;d++)i[d]=R(s[d]);for(d=0,f=i.length;d<f&&(u=i[d]);d++){var l=u;if(c!==b){l=[];for(e=0,h=u.length;e<h&&(j=u[e]);e++)S(j,c)&&l.push(j)}g=g.concat(l)}return V(g)}}(),X=a.qwery;W.noConflict=function(){a.qwery=X;return this},a.qwery=W}(this,document)
+!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(U(a))return!c||S(a,d)?[a]:[];if(!d)return[];if(h=T(a,d))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){if(h=a.match(x))return(m=b.getElementById(h[1]))?[m]:[];if(h=a.match(z))return G(c.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(U(a))return!c||S(a,d)?[a]:[];if(!d)return[];var f,g,i=[],j=[],l;if(h=T(a,d))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)}}(),Y=a.qwery;X.noConflict=function(){a.qwery=Y;return this},a.qwery=X}(this,document)
18 src/qwery.js
View
@@ -167,8 +167,15 @@
return false;
}
- function qsa(selector, root) {
- root = (typeof root == 'string') ? qsa(root)[0] : (root || doc);
+ 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 (isNode(selector)) {
+ return !_root || isAncestor(selector, root) ? [selector] : [];
+ }
if (!root) {
return [];
}
@@ -200,8 +207,11 @@
if (doc.querySelector && doc.querySelectorAll) {
return qsa;
}
- return function (selector, root) {
- root = (typeof root == 'string') ? qwery(root)[0] : (root || doc);
+ return function (selector, _root) {
+ var root = (typeof _root == 'string') ? qwery(_root)[0] : (_root || doc);
+ if (isNode(selector)) {
+ return !_root || isAncestor(selector, root) ? [selector] : [];
+ }
if (!root) {
return [];
}
12 tests/tests.js
View
@@ -149,4 +149,16 @@ sink('order matters', function (test, ok) {
});
+sink('argument types', function (test, ok) {
+ test('should be able to pass in nodes as arguments', 6, function () {
+ var el = document.getElementById('boosh');
+ ok(Q(el)[0] == el, '1st condition');
+ ok(Q(el, 'body')[0] == el, '2nd condition');
+ ok(Q(el, document)[0] == el, '3rd condition');
+ ok(Q(window, document)[0] == null, '4th condition');
+ ok(Q(window)[0] == window, '5th condition');
+ ok(Q(document)[0] == document, '6th condition');
+ });
+});
+
start();
Please sign in to comment.
Something went wrong with that request. Please try again.