Add node and jQuery object support to $.fn.closest

This commit is contained in:
timmywil 2011-03-21 20:59:20 -04:00
parent eb1497c1fb
commit e09d8898d8
2 changed files with 26 additions and 8 deletions

View file

@ -32,6 +32,7 @@ jQuery.fn.extend({
var ret = this.pushStack( "", "find", selector ), var ret = this.pushStack( "", "find", selector ),
length, n, r; length, n, r;
for ( i = 0, l = this.length; i < l; i++ ) { for ( i = 0, l = this.length; i < l; i++ ) {
length = ret.length; length = ret.length;
jQuery.find( selector, this[i], ret ); jQuery.find( selector, this[i], ret );
@ -78,6 +79,7 @@ jQuery.fn.extend({
closest: function( selectors, context ) { closest: function( selectors, context ) {
var ret = [], i, l, cur = this[0]; var ret = [], i, l, cur = this[0];
// Array
if ( jQuery.isArray( selectors ) ) { if ( jQuery.isArray( selectors ) ) {
var match, selector, var match, selector,
matches = {}, matches = {},
@ -88,7 +90,7 @@ jQuery.fn.extend({
selector = selectors[i]; selector = selectors[i];
if ( !matches[ selector ] ) { if ( !matches[ selector ] ) {
matches[selector] = jQuery.expr.match.POS.test( selector ) ? matches[ selector ] = POS.test( selector ) ?
jQuery( selector, context || this.context ) : jQuery( selector, context || this.context ) :
selector; selector;
} }
@ -111,8 +113,10 @@ jQuery.fn.extend({
return ret; return ret;
} }
var pos = POS.test( selectors ) ? // String
jQuery( selectors, context || this.context ) : null; var pos = POS.test( selectors ) || typeof selectors !== "string" ?
jQuery( selectors, context || this.context ) :
0;
for ( i = 0, l = this.length; i < l; i++ ) { for ( i = 0, l = this.length; i < l; i++ ) {
cur = this[i]; cur = this[i];

View file

@ -182,6 +182,20 @@ test("closest(Array)", function() {
same( jQuery("body").closest(["span","html"]), [{selector:"html", elem:document.documentElement, level:2}], "closest([body, html])" ); same( jQuery("body").closest(["span","html"]), [{selector:"html", elem:document.documentElement, level:2}], "closest([body, html])" );
}); });
test("closest(jQuery)", function() {
expect(7);
var $child = jQuery("#nothiddendivchild"),
$parent = jQuery("#nothiddendiv"),
$main = jQuery("#main");
ok( $child.closest( $parent ).is('#nothiddendiv'), "closest( jQuery('#nothiddendiv') )" );
ok( $child.closest( $parent[0] ).is('#nothiddendiv'), "closest( jQuery('#nothiddendiv') ) :: node" );
ok( $child.closest( $child ).is('#nothiddendivchild'), "child is included" );
ok( $child.closest( $child[0] ).is('#nothiddendivchild'), "child is included :: node" );
equals( $child.closest( document.createElement('div') ).length, 0, "created element is not related" );
equals( $child.closest( $main ).length, 0, "Main not a parent of child" );
equals( $child.closest( $main[0] ).length, 0, "Main not a parent of child :: node" );
});
test("not(Selector)", function() { test("not(Selector)", function() {
expect(7); expect(7);
equals( jQuery("#main > p#ap > a").not("#google").length, 2, "not('selector')" ); equals( jQuery("#main > p#ap > a").not("#google").length, 2, "not('selector')" );