Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

replace getTokens method with a regexp :P

  • Loading branch information...
commit dc2455332f6c03102da47aff44d9697da299e468 1 parent d0c17b5
@fat fat authored
View
37 qwery.js
@@ -59,7 +59,7 @@
return false;
}
if (idsAndClasses && (classes = idsAndClasses.match(clas))) {
- for (i = 0; i < classes.length; i++) {
+ for (i = classes.length; i--;) {
if (!(new RegExp('(^|\\s+)' + classes[i].slice(1) + '(\\s+|$)')).test(this.className)) {
return false;
}
@@ -80,9 +80,9 @@
}
function loopAll(token) {
- var i, item, r = [], intr = q(token), tag = intr[1] || '*',
+ var i, l, item, r = [], intr = q(token), tag = intr[1] || '*',
els = doc.getElementsByTagName(tag);
- for (i = 0; i < els.length; i++) {
+ for (i = 0, l = els.length; i < l; i++) {
el = els[i];
if (item = interpret.apply(el, intr)) {
r.push(item);
@@ -91,29 +91,6 @@
return r;
}
- function getTokens(input) {
- var r = [],
- temp = [],
- catting = false;
- _(input.split(/\s+/)).each(function (m) {
- if (catting) {
- temp.push(m);
- if (/\]/.test(m)) {
- catting = false;
- r = r.concat(temp.join(' '));
- }
- } else if (/\[[^\]]*$/.test(m)) {
- catting = true;
- temp = [];
- temp.push(m);
- } else {
- r.push(m);
- }
-
- });
- return r;
- }
-
function clean(s) {
return s.replace(/([\.\*\+\?\^\$\{\}\(\)\|\[\]\/\\])/g, '\\$1');
}
@@ -134,8 +111,8 @@
function _qwery(selector) {
var r = [], context, token, i, j, k, p, ret = [],
- el, node, found = true;
- var tokens = getTokens(selector);
+ el, node, found = false,
+ tokens = selector.split(/\s(?![\s\w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^'"]*\])/);
if (!tokens.length) {
return r;
}
@@ -149,11 +126,9 @@
p = node;
// loop through each token
for (i = tokens.length; i--;) {
- found = false;
parents:
while (p !== html && (p = p.parentNode)) { // loop through parent nodes
- if (interpret.apply(p, q(tokens[i]))) {
- found = true;
+ if (found = interpret.apply(p, q(tokens[i]))) {
break parents;
}
}
View
2  qwery.min.js
@@ -5,4 +5,4 @@
* MIT License
*/
-!function(a,b){function t(a,b){if(!b||!a||b==a)return!1;if(b.contains&&a.nodeType)return b.contains(a);if(b.compareDocumentPosition&&a.nodeType)return!!(b.compareDocumentPosition(a)&16);return!1}function s(a){var b=[],c,d,e,f,h,i,j=[],k,l,q=!0,r=p(a);if(!r.length)return b;b=o(r.pop());if(!r.length)return b;for(f=b.length;f--;){l=b[f],i=l;for(e=r.length;e--;){q=!1;parents:while(i!==g&&(i=i.parentNode))if(n.apply(i,m(r[e]))){q=!0;break parents}}q&&j.push(l)}return j}function r(a,b,c){switch(a){case"=":return b==c;case"^=":return b.match(new RegExp("^"+q(c)));case"$=":return b.match(new RegExp(q(c)+"$"));case"*=":return b.match(new RegExp(q(c)))}return!1}function q(a){return a.replace(/([\.\*\+\?\^\$\{\}\(\)\|\[\]\/\\])/g,"\\$1")}function p(a){var b=[],c=[],d=!1;l(a.split(/\s+/)).each(function(a){d?(c.push(a),/\]/.test(a)&&(d=!1,b=b.concat(c.join(" ")))):/\[[^\]]*$/.test(a)?(d=!0,c=[],c.push(a)):b.push(a)});return b}function o(a){var c,d,e=[],f=m(a),g=f[1]||"*",h=b.getElementsByTagName(g);for(c=0;c<h.length;c++)el=h[c],(d=n.apply(el,f))&&e.push(d);return e}function n(a,b,e,f,g,h,i){var j,k,l,m,n;if(b&&this.tagName.toLowerCase()!==b)return!1;if(e&&(m=e.match(c))&&m[1]!==this.id)return!1;if(e&&(n=e.match(d)))for(l=0;l<n.length;l++)if(!(new RegExp("(^|\\s+)"+n[l].slice(1)+"(\\s+|$)")).test(this.className))return!1;if(f&&!i){var o=this.attributes,p;for(p in o)if(o.hasOwnProperty(p)&&o[p].name==g)return this}if(f&&!r(h,this.getAttribute(g)||"",i))return!1;return this}function m(a){return a.match(new RegExp(h.source+"("+i.source+")?"))}function l(a){return new k(a)}function k(a){this.obj=j(a)}function j(a){var b,c,d=[];for(b=0,c=a.length;b<c;b++)d[b]=a[b];return d}var c=/#([\w\-]+)/,d=/\.[\w\-]+/g,e=/^#([\w\-]+$)/,f=/^([\w\-]+)$/,g=b.getElementsByTagName("html")[0],h=/^([a-z0-9]+)?(?:([\.\#]+[\w\-\.#]+)?)/,i=/\[([\w\-]+)(?:([\^\$\*]?\=)['"]?([ \w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^]+)["']?)?\]/;k.prototype={each:function(a){for(var b=0,c=this.obj.length;b<c;b++)a.call(this.obj[b],this.obj[b],b,this.obj);return this},map:function(a){var b=[],c,d;for(c=0,d=this.obj.length;c<d;c++)b[c]=a.call(this.obj[c],this.obj[c],c,this.obj);return b}};var u=function(){function d(d,f){f=typeof f=="string"?b.querySelector(f):f;var g;if(g=d.match(e))return[b.getElementById(g[1])];if(b.getElementsByClassName&&(c=d.match(a)))return j((f||b).getElementsByClassName(c[1]),0);return j((f||b).querySelectorAll(d),0)}var a=/^\.([\w\-]+)$/,c;if(b.querySelector&&b.querySelectorAll)return d;return function(a,c){c=typeof c=="string"?u(c)[0]:c||b;var d;if(d=a.match(e))return[b.getElementById(d[1])];if(d=a.match(f))return[c.getElementsByTagName(d[1])];var g=[],h=l(a.split(",")).map(function(a){return s(a)});l(h).each(function(a){var d=a;c!==b&&(d=[],l(a).each(function(a){t(a,c)&&d.push(a)})),g=g.concat(d)});return g}}(),v=a.qwery;u.noConflict=function(){a.qwery=v;return this},a.qwery=u}(this,document)
+!function(a,b){function s(a,b){if(!b||!a||b==a)return!1;if(b.contains&&a.nodeType)return b.contains(a);if(b.compareDocumentPosition&&a.nodeType)return!!(b.compareDocumentPosition(a)&16);return!1}function r(a){var b=[],c,d,e,f,h,i,j=[],k,l,p=!1,q=a.split(/\s(?![\s\w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^'"]*\])/);if(!q.length)return b;b=o(q.pop());if(!q.length)return b;for(f=b.length;f--;){l=b[f],i=l;for(e=q.length;e--;)parents:while(i!==g&&(i=i.parentNode))if(p=n.apply(i,m(q[e])))break parents;p&&j.push(l)}return j}function q(a,b,c){switch(a){case"=":return b==c;case"^=":return b.match(new RegExp("^"+p(c)));case"$=":return b.match(new RegExp(p(c)+"$"));case"*=":return b.match(new RegExp(p(c)))}return!1}function p(a){return a.replace(/([\.\*\+\?\^\$\{\}\(\)\|\[\]\/\\])/g,"\\$1")}function o(a){var c,d,e,f=[],g=m(a),h=g[1]||"*",i=b.getElementsByTagName(h);for(c=0,d=i.length;c<d;c++)el=i[c],(e=n.apply(el,g))&&f.push(e);return f}function n(a,b,e,f,g,h,i){var j,k,l,m,n;if(b&&this.tagName.toLowerCase()!==b)return!1;if(e&&(m=e.match(c))&&m[1]!==this.id)return!1;if(e&&(n=e.match(d)))for(l=n.length;l--;)if(!(new RegExp("(^|\\s+)"+n[l].slice(1)+"(\\s+|$)")).test(this.className))return!1;if(f&&!i){var o=this.attributes,p;for(p in o)if(o.hasOwnProperty(p)&&o[p].name==g)return this}if(f&&!q(h,this.getAttribute(g)||"",i))return!1;return this}function m(a){return a.match(new RegExp(h.source+"("+i.source+")?"))}function l(a){return new k(a)}function k(a){this.obj=j(a)}function j(a){var b,c,d=[];for(b=0,c=a.length;b<c;b++)d[b]=a[b];return d}var c=/#([\w\-]+)/,d=/\.[\w\-]+/g,e=/^#([\w\-]+$)/,f=/^([\w\-]+)$/,g=b.getElementsByTagName("html")[0],h=/^([a-z0-9]+)?(?:([\.\#]+[\w\-\.#]+)?)/,i=/\[([\w\-]+)(?:([\^\$\*]?\=)['"]?([ \w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^]+)["']?)?\]/;k.prototype={each:function(a){for(var b=0,c=this.obj.length;b<c;b++)a.call(this.obj[b],this.obj[b],b,this.obj);return this},map:function(a){var b=[],c,d;for(c=0,d=this.obj.length;c<d;c++)b[c]=a.call(this.obj[c],this.obj[c],c,this.obj);return b}};var t=function(){function d(d,f){f=typeof f=="string"?b.querySelector(f):f;var g;if(g=d.match(e))return[b.getElementById(g[1])];if(b.getElementsByClassName&&(c=d.match(a)))return j((f||b).getElementsByClassName(c[1]),0);return j((f||b).querySelectorAll(d),0)}var a=/^\.([\w\-]+)$/,c;if(b.querySelector&&b.querySelectorAll)return d;return function(a,c){c=typeof c=="string"?t(c)[0]:c||b;var d;if(d=a.match(e))return[b.getElementById(d[1])];if(d=a.match(f))return[c.getElementsByTagName(d[1])];var g=[],h=l(a.split(",")).map(function(a){return r(a)});l(h).each(function(a){var d=a;c!==b&&(d=[],l(a).each(function(a){s(a,c)&&d.push(a)})),g=g.concat(d)});return g}}(),u=a.qwery;t.noConflict=function(){a.qwery=u;return this},a.qwery=t}(this,document)
View
37 src/qwery.js
@@ -53,7 +53,7 @@
return false;
}
if (idsAndClasses && (classes = idsAndClasses.match(clas))) {
- for (i = 0; i < classes.length; i++) {
+ for (i = classes.length; i--;) {
if (!(new RegExp('(^|\\s+)' + classes[i].slice(1) + '(\\s+|$)')).test(this.className)) {
return false;
}
@@ -74,9 +74,9 @@
}
function loopAll(token) {
- var i, item, r = [], intr = q(token), tag = intr[1] || '*',
+ var i, l, item, r = [], intr = q(token), tag = intr[1] || '*',
els = doc.getElementsByTagName(tag);
- for (i = 0; i < els.length; i++) {
+ for (i = 0, l = els.length; i < l; i++) {
el = els[i];
if (item = interpret.apply(el, intr)) {
r.push(item);
@@ -85,29 +85,6 @@
return r;
}
- function getTokens(input) {
- var r = [],
- temp = [],
- catting = false;
- _(input.split(/\s+/)).each(function (m) {
- if (catting) {
- temp.push(m);
- if (/\]/.test(m)) {
- catting = false;
- r = r.concat(temp.join(' '));
- }
- } else if (/\[[^\]]*$/.test(m)) {
- catting = true;
- temp = [];
- temp.push(m);
- } else {
- r.push(m);
- }
-
- });
- return r;
- }
-
function clean(s) {
return s.replace(/([\.\*\+\?\^\$\{\}\(\)\|\[\]\/\\])/g, '\\$1');
}
@@ -128,8 +105,8 @@
function _qwery(selector) {
var r = [], context, token, i, j, k, p, ret = [],
- el, node, found = true;
- var tokens = getTokens(selector);
+ el, node, found = false,
+ tokens = selector.split(/\s(?![\s\w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^'"]*\])/);
if (!tokens.length) {
return r;
}
@@ -143,11 +120,9 @@
p = node;
// loop through each token
for (i = tokens.length; i--;) {
- found = false;
parents:
while (p !== html && (p = p.parentNode)) { // loop through parent nodes
- if (interpret.apply(p, q(tokens[i]))) {
- found = true;
+ if (found = interpret.apply(p, q(tokens[i]))) {
break parents;
}
}
View
9 tests/index.html
@@ -30,6 +30,15 @@
<div id="attr-test1" -data-attr></div>
<div id="attr-test2" -data-attr></div>
<div id="attr-test3" class="found you" -data-attr title="whatup duders"></div>
+ <div>
+ <div class="tokens" title="one" id="token-one"></div>
+ <div class="tokens" title="one two" id="token-two"></div>
+ <div class="tokens" title="one two three #%" id="token-three">
+ <a href="foo" id="token-four">
+ <div id="token-five"></div>
+ </a>
+ </div>
+ </div>
<ol id="tests"></ol>
<script src="tests.js"></script>
</body>
View
12 tests/tests.js
@@ -61,4 +61,16 @@ sink('CSS 2', function (test, ok) {
});
+sink('tokenizer', function (test, ok) {
+
+ test('gets weird tokens', 5, function () {
+ ok(Q('div .tokens[title="one"]')[0] == document.getElementById('token-one'), 'found the right element');
+ ok(Q('div .tokens[title="one two"]')[0] == document.getElementById('token-two'), 'found the right element');
+ ok(Q('div .tokens[title="one two three #%"]')[0] == document.getElementById('token-three'), 'found the right element');
+ ok(Q("div .tokens[title='one two three #%'] a")[0] == document.getElementById('token-four'), 'found the right element');
+ ok(Q('div .tokens[title="one two three #%"] a[href=foo] div')[0] == document.getElementById('token-five'), 'found the right element');
+ });
+
+});
+
start();
Please sign in to comment.
Something went wrong with that request. Please try again.