Permalink
Browse files

byId fixes and feature additions

byId only called on document nodes, either just `document` or `ownerDocument` of
the context element. isAncestor check is performed so you can narrow the query down
  • Loading branch information...
rvagg committed Nov 17, 2011
1 parent b394aa8 commit 21ee99d70373942906c18b297ac95a748a97b300
Showing with 43 additions and 5 deletions.
  1. +8 −3 src/qwery.js
  2. +1 −0 tests/index.html
  3. +34 −2 tests/tests.js
View
@@ -9,7 +9,6 @@
, html = doc.documentElement
, byClass = 'getElementsByClassName'
, byTag = 'getElementsByTagName'
- , byId = 'getElementById'
, qSA = 'querySelectorAll'
, id = /#([\w\-]+)/
, clas = /\.[\w\-]+/g
@@ -161,7 +160,7 @@
if (!tokens.length) return r
token = (tokens = tokens.slice(0)).pop() // copy cached tokens, take the last one
- if (tokens.length && (m = tokens[tokens.length - 1].match(idOnly))) root = _root[byId](m[1])
+ if (tokens.length && (m = tokens[tokens.length - 1].match(idOnly))) root = byId(_root, m[1])
if (!root) return r
intr = q(token)
@@ -244,6 +243,12 @@
return root
}
+ function byId(root, id) {
+ if (root.nodeType === 9) return root.getElementById(id)
+ var el = root.ownerDocument ? root.ownerDocument.getElementById(id) : null
+ return el && isAncestor(el, root) ? el : null
+ }
+
function qwery(selector, _root) {
var m, el, root = normalizeRoot(_root)
@@ -254,7 +259,7 @@
}
if (selector && arrayLike(selector)) return flatten(selector)
if (m = selector.match(easy)) {
- if (m[1]) return (el = root[byId](m[1])) ? [el] : []
+ if (m[1]) return (el = byId(root, m[1])) ? [el] : []
if (m[2]) return arrayify(root[byTag](m[2]))
if (supportsCSS3 && m[3]) return arrayify(root[byClass](m[3]))
}
View
@@ -58,6 +58,7 @@ <h1>Qwery Tests</h1>
<span class="h i a"></span>
</div>
</div>
+ <div id="lonelyBoosh"></div>
<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>
View
@@ -31,6 +31,14 @@ sink('Contexts', function (test, ok) {
ok(Q('#boosh,#boosh').length == 1, 'two booshes dont make a thing go right');
});
+ test('byId sub-queries within context', 6, function() {
+ ok(Q('#booshTest', Q('#boosh')).length == 1, 'found "#id #id"')
+ ok(Q('.a.b #booshTest', Q('#boosh')).length == 1, 'found ".class.class #id"')
+ ok(Q('.a>#booshTest', Q('#boosh')).length == 1, 'found ".class>#id"')
+ ok(Q('>.a>#booshTest', Q('#boosh')).length == 1, 'found ">.class>#id"')
+ ok(!Q('#boosh', Q('#booshTest')).length, 'shouldn\'t find #boosh (ancestor) within #booshTest (descendent)')
+ ok(!Q('#boosh', Q('#lonelyBoosh')).length, 'shouldn\'t find #boosh within #lonelyBoosh (unrelated)')
+ })
});
sink('CSS 1', function (test, ok) {
@@ -40,6 +48,13 @@ sink('CSS 1', function (test, ok) {
ok(!!Q('h1')[0], 'found 1 h1');
});
+ test('byId sub-queries', 4, function() {
+ ok(Q('#boosh #booshTest').length == 1, 'found "#id #id"')
+ ok(Q('.a.b #booshTest').length == 1, 'found ".class.class #id"')
+ ok(Q('#boosh>.a>#booshTest').length == 1, 'found "#id>.class>#id"')
+ ok(Q('.a>#booshTest').length == 1, 'found ".class>#id"')
+ })
+
test('get elements by class', 6, function () {
ok(Q('#boosh .a').length == 2, 'found two elements');
ok(!!Q('#boosh div.a')[0], 'found one element');
@@ -485,12 +500,13 @@ sink('selecting elements in other documents', function (test, ok) {
doc.body.innerHTML =
'<div id="hsoob">' +
'<div class="a b">' +
- '<div class="d e sib" test="fg" id="booshTest"></div>' +
+ '<div class="d e sib" test="fg" id="booshTest"><p><span id="spanny"></span></p></div>' +
'<em nopass="copyrighters" rel="copyright booshrs" test="f g" class="sib"></em>' +
'<span class="h i a sib"></span>' +
'</div>' +
'<p class="odd"></p>' +
- '</div>'
+ '</div>' +
+ '<div id="lonelyHsoob"></div>'
test('get element by id', 1, function () {
var result = Q('#hsoob', doc);
@@ -513,6 +529,22 @@ sink('selecting elements in other documents', function (test, ok) {
ok(Q('.a .d ~ .sib[test="f g"]', doc).length === 1, 'found 1 ~ sibling with test attribute')
});
+ test('byId sub-queries', 3, function () {
+ ok(Q('#hsoob #spanny', doc).length == 1, 'found "#id #id" in frame')
+ ok(Q('.a #spanny', doc).length == 1, 'found ".class #id" in frame')
+ ok(Q('.a #booshTest #spanny', doc).length == 1, 'found ".class #id #id" in frame')
+ //ok(Q('> #hsoob', doc).length == 1, 'found "> #id" in frame') --> would be good to support this, needs some tweaking though
+ })
+
+ test('byId sub-queries within sub-context', 6, function () {
+ ok(Q('#spanny', Q('#hsoob', doc)).length == 1, 'found "#id #id" in frame')
+ ok(Q('.a #spanny', Q('#hsoob', doc)).length == 1, 'found ".class #id" in frame')
+ ok(Q('.a #booshTest #spanny', Q('#hsoob', doc)).length == 1, 'found ".class #id #id" in frame')
+ ok(Q('.a > #booshTest', Q('#hsoob', doc)).length == 1, 'found "> .class #id" in frame')
+ ok(!Q('#booshTest', Q('#spanny', doc)).length, 'shouldn\'t find #booshTest (ancestor) within #spanny (descendent)')
+ ok(!Q('#booshTest', Q('#lonelyHsoob', doc)).length, 'shouldn\'t find #boosh within #lonelyHsoob (unrelated)')
+ })
+
});
start();

0 comments on commit 21ee99d

Please sign in to comment.