Skip to content

Commit

Permalink
ability to select the document node with $(document)
Browse files Browse the repository at this point in the history
enables usage like `$(document).delegate(...)`
  • Loading branch information
mislav committed Mar 1, 2011
1 parent 83c5626 commit 7cf0d71
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/zepto.js
Expand Up @@ -30,14 +30,15 @@ var Zepto = (function() {
}

function $(selector, context){
if (selector == document) return Z();
else if (context !== undefined) return $(context).find(selector);
if (!selector) return Z();
if (context !== undefined) return $(context).find(selector);
else if (typeof selector === 'function') return $(document).ready(selector);
else if (selector instanceof Z) return selector;
else {
var dom;
if (selector instanceof Array) dom = compact(selector);
else if (selector instanceof Element || selector === window) dom = [selector];
else if (selector instanceof Element || selector === window || selector === document)
dom = [selector], selector = null;
else if (fragmentRE.test(selector)) dom = fragment(selector);
else dom = $$(document, selector);
return Z(dom, selector);
Expand Down Expand Up @@ -106,7 +107,7 @@ var Zepto = (function() {
var node = this[0], nodes = $$(context !== undefined ? context : document, selector);
if (nodes.length === 0) node = null;
while(node && node !== document && nodes.indexOf(node) < 0) node = node.parentNode;
return $(node);
return $(node !== document && node);
},
parents: function(selector){
var ancestors = [], nodes = this;
Expand Down
9 changes: 8 additions & 1 deletion test/zepto.html
Expand Up @@ -203,6 +203,13 @@ <h1>Zepto DOM unit tests</h1>
t.assertLength(1, $('p > span.yay'));
},

testDollarWithNil: function(t){
t.assertLength(0, $(null));
t.assertLength(0, $(undefined));
t.assertLength(0, $(false));
t.assertLength(0, $(''));
},

testSize: function(t){
t.assertEqual(4, $('#find1 .findme').size());
},
Expand Down Expand Up @@ -244,7 +251,7 @@ <h1>Zepto DOM unit tests</h1>

testDollarWithDocument: function(t){
var z = $(document);
t.assertLength(0, z);
t.assertLength(1, z);
t.assertEqual('', z.selector);
},

Expand Down

0 comments on commit 7cf0d71

Please sign in to comment.