jquery/src/traversing.js

279 lines
6.8 KiB
JavaScript
Raw Normal View History

var runtil = /Until$/,
rparentsprev = /^(?:parents|prev(?:Until|All))/,
isSimple = /^.[^:#\[\.,]*$/,
rneedsContext = jQuery.expr.match.needsContext,
// methods guaranteed to produce a unique set when starting from a unique set
guaranteedUnique = {
children: true,
contents: true,
next: true,
prev: true
};
jQuery.fn.extend({
find: function( selector ) {
2012-12-18 11:29:04 +00:00
var self, matched, i,
l = this.length;
if ( typeof selector !== "string" ) {
self = this;
return this.pushStack( jQuery( selector ).filter(function() {
2012-12-18 11:29:04 +00:00
for ( i = 0; i < l; i++ ) {
if ( jQuery.contains( self[ i ], this ) ) {
return true;
}
}
}) );
}
2012-12-18 11:29:04 +00:00
matched = [];
for ( i = 0; i < l; i++ ) {
jQuery.find( selector, this[ i ], matched );
}
// Needed because $( selector, context ) becomes $( context ).find( selector )
matched = this.pushStack( l > 1 ? jQuery.unique( matched ) : matched );
2012-12-18 11:29:04 +00:00
matched.selector = ( this.selector ? this.selector + " " : "" ) + selector;
return matched;
},
has: function( target ) {
2012-12-18 11:29:04 +00:00
var targets = jQuery( target, this ),
l = targets.length;
return this.filter(function() {
2012-12-18 11:29:04 +00:00
var i = 0;
for ( ; i < l; i++ ) {
if ( jQuery.contains( this, targets[i] ) ) {
return true;
}
}
});
},
not: function( selector ) {
return this.pushStack( winnow(this, selector, false) );
},
filter: function( selector ) {
return this.pushStack( winnow(this, selector, true) );
},
is: function( selector ) {
return !!selector && (
typeof selector === "string" ?
// If this is a positional/relative selector, check membership in the returned set
// so $("p:first").is("p:last") won't return true for a doc with two "p".
rneedsContext.test( selector ) ?
2012-12-18 11:29:04 +00:00
jQuery( selector, this.context ).index( this[ 0 ] ) >= 0 :
jQuery.filter( selector, this ).length > 0 :
this.filter( selector ).length > 0 );
},
closest: function( selectors, context ) {
2012-12-19 15:38:33 +00:00
var cur,
i = 0,
l = this.length,
2012-12-18 11:29:04 +00:00
matched = [],
pos = ( rneedsContext.test( selectors ) || typeof selectors !== "string" ) ?
jQuery( selectors, context || this.context ) :
0;
for ( ; i < l; i++ ) {
2012-12-18 11:29:04 +00:00
cur = this[ i ];
2012-12-18 11:29:04 +00:00
while ( cur && cur.ownerDocument && cur !== context ) {
if ( pos ? pos.index( cur ) > -1 : jQuery.find.matchesSelector( cur, selectors ) ) {
matched.push( cur );
break;
}
2012-12-18 11:29:04 +00:00
cur = cur.parentElement;
}
}
2012-12-18 11:29:04 +00:00
return this.pushStack( matched.length > 1 ? jQuery.unique( matched ) : matched );
},
// Determine the position of an element within
// the matched set of elements
index: function( elem ) {
// No argument, return index in parent
if ( !elem ) {
2012-12-18 11:29:04 +00:00
return ( this[ 0 ] && this[ 0 ].parentNode ) ? this.first().prevAll().length : -1;
}
// index in selector
if ( typeof elem === "string" ) {
2012-12-18 11:29:04 +00:00
return core_indexOf.call( jQuery( elem ), this[ 0 ] );
}
// Locate the position of the desired element
2012-12-18 11:29:04 +00:00
return core_indexOf.call( this,
// If it receives a jQuery object, the first element is used
2012-12-18 11:29:04 +00:00
elem.jquery ? elem[ 0 ] : elem
);
},
add: function( selector, context ) {
var set = typeof selector === "string" ?
jQuery( selector, context ) :
jQuery.makeArray( selector && selector.nodeType ? [ selector ] : selector ),
all = jQuery.merge( this.get(), set );
return this.pushStack( jQuery.unique(all) );
},
addBack: function( selector ) {
return this.add( selector == null ?
2012-06-03 14:42:24 +00:00
this.prevObject : this.prevObject.filter(selector)
);
}
});
jQuery.fn.andSelf = jQuery.fn.addBack;
jQuery.each({
parent: function( elem ) {
2012-12-18 11:29:04 +00:00
return elem.parentElement;
},
parents: function( elem ) {
2012-12-18 11:29:04 +00:00
return jQuery.dir( elem, "parentElement" );
},
parentsUntil: function( elem, i, until ) {
2012-12-18 11:29:04 +00:00
return jQuery.dir( elem, "parentElement", until );
},
next: function( elem ) {
2012-12-18 11:29:04 +00:00
return elem.nextElementSibling;
},
prev: function( elem ) {
2012-12-18 11:29:04 +00:00
return elem.previousElementSibling;
},
nextAll: function( elem ) {
2012-12-18 11:29:04 +00:00
return jQuery.dir( elem, "nextElementSibling" );
},
prevAll: function( elem ) {
2012-12-18 11:29:04 +00:00
return jQuery.dir( elem, "previousElementSibling" );
},
nextUntil: function( elem, i, until ) {
2012-12-18 11:29:04 +00:00
return jQuery.dir( elem, "nextElementSibling", until );
},
prevUntil: function( elem, i, until ) {
2012-12-18 11:29:04 +00:00
return jQuery.dir( elem, "previousElementSibling", until );
},
siblings: function( elem ) {
return jQuery.sibling( ( elem.parentNode || {} ).firstChild, elem );
},
children: function( elem ) {
2012-12-18 11:29:04 +00:00
var children = elem.children;
// documentFragment or document does not have children property
return children ? jQuery.merge( [], children ) : jQuery.sibling( elem.firstChild );
},
contents: function( elem ) {
return jQuery.nodeName( elem, "iframe" ) ?
elem.contentDocument || elem.contentWindow.document :
jQuery.merge( [], elem.childNodes );
}
}, function( name, fn ) {
jQuery.fn[ name ] = function( until, selector ) {
2012-12-18 11:29:04 +00:00
var matched = jQuery.map( this, fn, until );
if ( !runtil.test( name ) ) {
selector = until;
}
if ( selector && typeof selector === "string" ) {
2012-12-18 11:29:04 +00:00
matched = jQuery.filter( selector, matched );
}
2012-12-18 11:29:04 +00:00
if ( this.length > 1 ) {
if ( !guaranteedUnique[ name ] ) {
jQuery.unique( matched );
}
2012-12-18 11:29:04 +00:00
if ( rparentsprev.test( name ) ) {
matched.reverse();
}
}
2012-12-18 11:29:04 +00:00
return this.pushStack( matched );
};
});
jQuery.extend({
filter: function( expr, elems, not ) {
if ( not ) {
expr = ":not(" + expr + ")";
}
return elems.length === 1 ?
2012-12-18 11:29:04 +00:00
jQuery.find.matchesSelector( elems[ 0 ], expr ) ? [ elems[ 0 ] ] : [] :
jQuery.find.matches( expr, elems );
},
dir: function( elem, dir, until ) {
2012-12-18 11:29:04 +00:00
var cur = elem[ dir ],
matched = [];
2012-12-18 11:29:04 +00:00
while ( cur && ( !until || !jQuery( cur ).is( until ) ) ) {
matched.push( cur );
cur = cur[ dir ];
}
2012-12-18 11:29:04 +00:00
return matched;
},
sibling: function( n, elem ) {
2012-12-18 11:29:04 +00:00
var matched = [];
for ( ; n; n = n.nextSibling ) {
if ( n.nodeType === 1 && n !== elem ) {
2012-12-18 11:29:04 +00:00
matched.push( n );
}
}
2012-12-18 11:29:04 +00:00
return matched;
}
});
// Implement the identical functionality for filter and not
function winnow( elements, qualifier, keep ) {
// Can't pass null or undefined to indexOf in Firefox 4
// Set to 0 to skip string check
qualifier = qualifier || 0;
2012-12-18 11:29:04 +00:00
var filtered;
if ( jQuery.isFunction( qualifier ) ) {
return jQuery.grep(elements, function( elem, i ) {
var retVal = !!qualifier.call( elem, i, elem );
return retVal === keep;
});
2012-12-18 11:29:04 +00:00
}
2012-12-18 11:29:04 +00:00
if ( qualifier.nodeType ) {
return jQuery.grep(elements, function( elem ) {
return ( elem === qualifier ) === keep;
});
2012-12-18 11:29:04 +00:00
}
2012-12-18 11:29:04 +00:00
if ( typeof qualifier === "string" ) {
filtered = jQuery.grep(elements, function( elem ) {
return elem.nodeType === 1;
});
if ( isSimple.test( qualifier ) ) {
2012-12-18 11:29:04 +00:00
return jQuery.filter( qualifier, filtered, !keep );
}
2012-12-18 11:29:04 +00:00
qualifier = jQuery.filter( qualifier, filtered );
}
return jQuery.grep(elements, function( elem ) {
2012-12-18 11:29:04 +00:00
return ( core_indexOf.call( qualifier, elem ) >= 0 ) === keep;
});
}