]> source.dussan.org Git - jquery.git/commitdiff
Quick improvement to the performace of .index() with no arguments - Adding a unit... 367/head
authorgnarf <gnarf@gnarf.net>
Wed, 15 Jun 2011 04:38:36 +0000 (23:38 -0500)
committergnarf <gnarf@gnarf.net>
Wed, 15 Jun 2011 04:38:36 +0000 (23:38 -0500)
src/traversing.js
test/unit/traversing.js

index 8c4b4ef83ebae837853b5b848878863b306f02e5..35a5174e9fffcc8134de6813cc41169312e6bb64 100644 (file)
@@ -145,12 +145,17 @@ jQuery.fn.extend({
        // 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() );
+
+               // No argument, return index in parent
+               if ( !elem ) {
+                       return ( this[0] && this[0].parentNode ) ? this.prevAll().length : -1;
+               }
+
+               // index in selector
+               if ( typeof elem === "string" ) {
+                       return jQuery.inArray( this[0], jQuery( elem ) );
                }
+
                // Locate the position of the desired element
                return jQuery.inArray(
                        // If it receives a jQuery object, the first element is used
index 5216ae7526ea02c956bed374bda1c5f11b8a428e..70c14259701ae10352085fe96204d10df5203848 100644 (file)
@@ -99,9 +99,11 @@ test("is(jQuery)", function() {
 });
 
 test("index()", function() {
-       expect(1);
+       expect( 2 );
 
-       equals( jQuery("#text2").index(), 2, "Returns the index of a child amongst its siblings" )
+       equal( jQuery("#text2").index(), 2, "Returns the index of a child amongst its siblings" );
+       
+       equal( jQuery("<div/>").index(), -1, "Node without parent returns -1" );
 });
 
 test("index(Object|String|undefined)", function() {