Permalink
Browse files

replace getTokens method with a regexp :P

  • Loading branch information...
1 parent d0c17b5 commit dc2455332f6c03102da47aff44d9697da299e468 @fat fat committed Mar 31, 2011
Showing with 34 additions and 63 deletions.
  1. +6 −31 qwery.js
  2. +1 −1 qwery.min.js
  3. +6 −31 src/qwery.js
  4. +9 −0 tests/index.html
  5. +12 −0 tests/tests.js
View
@@ -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

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -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
@@ -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
@@ -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();

0 comments on commit dc24553

Please sign in to comment.