Moved some methods around inbetween core.js and traversing.js. Core methods shouldn...
[jquery.git] / src / traversing.js
index 4efe282..6375d9d 100644 (file)
@@ -2,14 +2,13 @@ var runtil = /Until$/,
        rparentsprev = /^(?:parents|prevUntil|prevAll)/,
        // Note: This RegExp should be improved, or likely pulled from Sizzle
        rmultiselector = /,/,
-       slice = Array.prototype.slice,
-       join = Array.prototype.join;
+       slice = Array.prototype.slice;
 
 // Implement the identical functionality for filter and not
 var winnow = function( elements, qualifier, keep ) {
        if ( jQuery.isFunction( qualifier ) ) {
                return jQuery.grep(elements, function(elem, i) {
-                       return !!qualifier.call( elem, i ) === keep;
+                       return !!qualifier.call( elem, i, elem ) === keep;
                });
 
        } else if ( qualifier.nodeType ) {
@@ -58,6 +57,21 @@ jQuery.fn.extend({
                return ret;
        },
 
+       has: function( target ) {
+               var targets = jQuery( target );
+               return this.filter(function() {
+                       for ( var i = 0, l = targets.length; i < l; i++ ) {
+                               if ( jQuery.contains( this, targets[i] ) ) {
+                                       return true;
+                               }
+                       }
+               });
+       },
+
+       contains: function( target ) {
+               return this.has( target ).length > 0;
+       },
+
        not: function( selector ) {
                return this.pushStack( winnow(this, selector, false), "not", selector);
        },
@@ -65,6 +79,10 @@ jQuery.fn.extend({
        filter: function( selector ) {
                return this.pushStack( winnow(this, selector, true), "filter", selector );
        },
+       
+       is: function( selector ) {
+               return !!selector && jQuery.filter( selector, this ).length > 0;
+       },
 
        closest: function( selectors, context ) {
                if ( jQuery.isArray( selectors ) ) {
@@ -110,6 +128,21 @@ jQuery.fn.extend({
                        return null;
                });
        },
+       
+       // Determine the position of an element within
+       // the matched set of elements
+       index: function( elem ) {
+               if ( !elem || typeof elem === "string" ) {
+                       return jQuery.inArray( this[0],
+                               // If it receives a string, the selector is used
+                               // If it receives nothing, the siblings are used
+                               elem ? jQuery( elem ) : this.parent().children() );
+               }
+               // Locate the position of the desired element
+               return jQuery.inArray(
+                       // If it receives a jQuery object, the first element is used
+                       elem.jquery ? elem[0] : elem, this );
+       },
 
        add: function( selector, context ) {
                var set = typeof selector === "string" ?
@@ -122,37 +155,8 @@ jQuery.fn.extend({
                        all );
        },
 
-       eq: function( i ) {
-               return i === -1 ?
-                       this.slice( i ) :
-                       this.slice( i, +i + 1 );
-       },
-
-       first: function() {
-               return this.eq( 0 );
-       },
-
-       last: function() {
-               return this.eq( -1 );
-       },
-
-       slice: function() {
-               return this.pushStack( slice.apply( this, arguments ),
-                       "slice", join.call(arguments, ",") );
-       },
-
-       map: function( callback ) {
-               return this.pushStack( jQuery.map(this, function(elem, i){
-                       return callback.call( elem, i, elem );
-               }));
-       },
-
        andSelf: function() {
                return this.add( this.prevObject );
-       },
-
-       end: function() {
-               return this.prevObject || jQuery(null);
        }
 });
 
@@ -187,7 +191,7 @@ jQuery.each({
                        ret = ret.reverse();
                }
 
-               return this.pushStack( ret, name, join.call(arguments, ",") );
+               return this.pushStack( ret, name, slice.call(arguments).join(",") );
        };
 });