index now works in reverse when passed a selector or undefined. fixes #3971

This commit is contained in:
Brandon Aaron 2009-05-02 19:22:55 +00:00
parent cbda6c541b
commit ffd457d456
2 changed files with 40 additions and 24 deletions

View file

@ -182,10 +182,16 @@ jQuery.fn = jQuery.prototype = {
// Determine the position of an element within // Determine the position of an element within
// the matched set of elements // the matched set of elements
index: function( elem ) { 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 // Locate the position of the desired element
return jQuery.inArray( return jQuery.inArray(
// If it receives a jQuery object, the first element is used // If it receives a jQuery object, the first element is used
elem && elem.jquery ? elem[0] : elem, this ); elem.jquery ? elem[0] : elem, this );
}, },
is: function( selector ) { is: function( selector ) {
@ -355,7 +361,7 @@ jQuery.extend({
if ( array != null ) { if ( array != null ) {
i = array.length; i = array.length;
// The window, strings (and functions) also have 'length' // The window, strings (and functions) also have 'length'
if ( i == null || typeof array === "string" || jQuery.isFunction(array) || array.setInterval ) { if ( i == null || typeof array === "string" || jQuery.isFunction(array) || array.setInterval ) {
ret[0] = array; ret[0] = array;
@ -383,7 +389,7 @@ jQuery.extend({
// We have to loop this way because IE & Opera overwrite the length // We have to loop this way because IE & Opera overwrite the length
// expando of getElementsByTagName // expando of getElementsByTagName
var i = 0, elem, pos = first.length; var i = 0, elem, pos = first.length;
// Also, we need to make sure that the correct elements are being returned // Also, we need to make sure that the correct elements are being returned
// (IE returns comment nodes in a '*' query) // (IE returns comment nodes in a '*' query)
if ( !jQuery.support.getAll ) { if ( !jQuery.support.getAll ) {

View file

@ -56,19 +56,19 @@ test("selector state", function() {
expect(30); expect(30);
var test; var test;
test = jQuery(); test = jQuery();
equals( test.selector, "", "Empty jQuery Selector" ); equals( test.selector, "", "Empty jQuery Selector" );
equals( test.context, undefined, "Empty jQuery Context" ); equals( test.context, undefined, "Empty jQuery Context" );
test = jQuery(document); test = jQuery(document);
equals( test.selector, "", "Document Selector" ); equals( test.selector, "", "Document Selector" );
equals( test.context, document, "Document Context" ); equals( test.context, document, "Document Context" );
test = jQuery(document.body); test = jQuery(document.body);
equals( test.selector, "", "Body Selector" ); equals( test.selector, "", "Body Selector" );
equals( test.context, document.body, "Body Context" ); equals( test.context, document.body, "Body Context" );
test = jQuery("#main"); test = jQuery("#main");
equals( test.selector, "#main", "#main Selector" ); equals( test.selector, "#main", "#main Selector" );
equals( test.context, document, "#main Context" ); equals( test.context, document, "#main Context" );
@ -76,11 +76,11 @@ test("selector state", function() {
test = jQuery("#notfoundnono"); test = jQuery("#notfoundnono");
equals( test.selector, "#notfoundnono", "#notfoundnono Selector" ); equals( test.selector, "#notfoundnono", "#notfoundnono Selector" );
equals( test.context, document, "#notfoundnono Context" ); equals( test.context, document, "#notfoundnono Context" );
test = jQuery("#main", document); test = jQuery("#main", document);
equals( test.selector, "#main", "#main Selector" ); equals( test.selector, "#main", "#main Selector" );
equals( test.context, document, "#main Context" ); equals( test.context, document, "#main Context" );
test = jQuery("#main", document.body); test = jQuery("#main", document.body);
equals( test.selector, "#main", "#main Selector" ); equals( test.selector, "#main", "#main Selector" );
equals( test.context, document.body, "#main Context" ); equals( test.context, document.body, "#main Context" );
@ -89,7 +89,7 @@ test("selector state", function() {
test = jQuery(test); test = jQuery(test);
equals( test.selector, "#main", "#main Selector" ); equals( test.selector, "#main", "#main Selector" );
equals( test.context, document.body, "#main Context" ); equals( test.context, document.body, "#main Context" );
test = jQuery(document.body).find("#main"); test = jQuery(document.body).find("#main");
equals( test.selector, "#main", "#main find Selector" ); equals( test.selector, "#main", "#main find Selector" );
equals( test.context, document.body, "#main find Context" ); equals( test.context, document.body, "#main find Context" );
@ -97,23 +97,23 @@ test("selector state", function() {
test = jQuery("#main").filter("div"); test = jQuery("#main").filter("div");
equals( test.selector, "#main.filter(div)", "#main filter Selector" ); equals( test.selector, "#main.filter(div)", "#main filter Selector" );
equals( test.context, document, "#main filter Context" ); equals( test.context, document, "#main filter Context" );
test = jQuery("#main").not("div"); test = jQuery("#main").not("div");
equals( test.selector, "#main.not(div)", "#main not Selector" ); equals( test.selector, "#main.not(div)", "#main not Selector" );
equals( test.context, document, "#main not Context" ); equals( test.context, document, "#main not Context" );
test = jQuery("#main").filter("div").not("div"); test = jQuery("#main").filter("div").not("div");
equals( test.selector, "#main.filter(div).not(div)", "#main filter, not Selector" ); equals( test.selector, "#main.filter(div).not(div)", "#main filter, not Selector" );
equals( test.context, document, "#main filter, not Context" ); equals( test.context, document, "#main filter, not Context" );
test = jQuery("#main").filter("div").not("div").end(); test = jQuery("#main").filter("div").not("div").end();
equals( test.selector, "#main.filter(div)", "#main filter, not, end Selector" ); equals( test.selector, "#main.filter(div)", "#main filter, not, end Selector" );
equals( test.context, document, "#main filter, not, end Context" ); equals( test.context, document, "#main filter, not, end Context" );
test = jQuery("#main").parent("body"); test = jQuery("#main").parent("body");
equals( test.selector, "#main.parent(body)", "#main parent Selector" ); equals( test.selector, "#main.parent(body)", "#main parent Selector" );
equals( test.context, document, "#main parent Context" ); equals( test.context, document, "#main parent Context" );
test = jQuery("#main").eq(0); test = jQuery("#main").eq(0);
equals( test.selector, "#main.slice(0,1)", "#main eq Selector" ); equals( test.selector, "#main.slice(0,1)", "#main eq Selector" );
equals( test.context, document, "#main eq Context" ); equals( test.context, document, "#main eq Context" );
@ -366,12 +366,13 @@ test("each(Function)", function() {
ok( pass, "Execute a function, Relative" ); ok( pass, "Execute a function, Relative" );
}); });
test("index(Object)", function() { test("index(Object|String|undefined)", function() {
expect(10); expect(15);
var elements = jQuery([window, document]), var elements = jQuery([window, document]),
inputElements = jQuery('#radio1,#radio2,#check1,#check2'); inputElements = jQuery('#radio1,#radio2,#check1,#check2');
// Passing a node
equals( elements.index(window), 0, "Check for index of elements" ); equals( elements.index(window), 0, "Check for index of elements" );
equals( elements.index(document), 1, "Check for index of elements" ); equals( elements.index(document), 1, "Check for index of elements" );
equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" ); equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" );
@ -381,24 +382,33 @@ test("index(Object)", function() {
equals( inputElements.index(window), -1, "Check for not found index" ); equals( inputElements.index(window), -1, "Check for not found index" );
equals( inputElements.index(document), -1, "Check for not found index" ); equals( inputElements.index(document), -1, "Check for not found index" );
// Passing a jQuery object
// enabled since [5500] // enabled since [5500]
equals( elements.index( elements ), 0, "Pass in a jQuery object" ); equals( elements.index( elements ), 0, "Pass in a jQuery object" );
equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" ); equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );
equals( jQuery("#form :radio").index( jQuery("#radio2") ), 1, "Pass in a jQuery object" );
// Passing a selector or nothing
// enabled since [6329]
equals( jQuery('#text2').index(), 2, "Check for index amongst siblings" );
equals( jQuery('#form').children().eq(4).index(), 4, "Check for index amongst siblings" );
equals( jQuery('#radio2').index('#form :radio') , 1, "Check for index within a selector" );
equals( jQuery('#radio2').index('#form :text') , -1, "Check for index not found within a selector" );
}); });
test("jQuery.merge()", function() { test("jQuery.merge()", function() {
expect(6); expect(6);
var parse = jQuery.merge; var parse = jQuery.merge;
same( parse([],[]), [], "Empty arrays" ); same( parse([],[]), [], "Empty arrays" );
same( parse([1],[2]), [1,2], "Basic" ); same( parse([1],[2]), [1,2], "Basic" );
same( parse([1,2],[3,4]), [1,2,3,4], "Basic" ); same( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
same( parse([1,2],[]), [1,2], "Second empty" ); same( parse([1,2],[]), [1,2], "Second empty" );
same( parse([],[1,2]), [1,2], "First empty" ); same( parse([],[1,2]), [1,2], "First empty" );
// Fixed at [5998], #3641 // Fixed at [5998], #3641
same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)"); same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
}); });
@ -531,7 +541,7 @@ test("jQuery.makeArray", function(){
// function, is tricky as it has length // function, is tricky as it has length
equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" ); equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
//window, also has length //window, also has length
equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" ); equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );