aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortimmywil <tim.willison@thisismedium.com>2011-05-28 12:00:28 -0400
committertimmywil <timmywillisn@gmail.com>2011-09-19 15:42:30 -0400
commit2e0c9bfd364d990051fc3de94d7abd4194b6ce7d (patch)
tree52bf4cfa6a5171c63056ddb15df741ca3b71747e
parent25205d3f90f1bda06e7ca95c2aa8c43e535badb4 (diff)
downloadjquery-2e0c9bfd364d990051fc3de94d7abd4194b6ce7d.tar.gz
jquery-2e0c9bfd364d990051fc3de94d7abd4194b6ce7d.zip
Allow second argument to be passed to array.indexOf. Fixes #9453.
-rw-r--r--src/core.js23
-rw-r--r--test/unit/core.js36
2 files changed, 49 insertions, 10 deletions
diff --git a/src/core.js b/src/core.js
index 8d9a94f63..8bc3137dd 100644
--- a/src/core.js
+++ b/src/core.js
@@ -682,18 +682,21 @@ jQuery.extend({
return ret;
},
- inArray: function( elem, array ) {
- if ( !array ) {
- return -1;
- }
+ inArray: function( elem, array, i ) {
+ var len;
- if ( indexOf ) {
- return indexOf.call( array, elem );
- }
+ if ( array ) {
+ if ( indexOf ) {
+ return indexOf.call( array, elem, i );
+ }
- for ( var i = 0, length = array.length; i < length; i++ ) {
- if ( array[ i ] === elem ) {
- return i;
+ len = array.length;
+ i = i && i < 0 ? Math.max( 0, len + i ) : 0;
+
+ for ( ; i < len; i++ ) {
+ if ( array[ i ] === elem ) {
+ return i;
+ }
}
}
diff --git a/test/unit/core.js b/test/unit/core.js
index fdf0ceb5e..50157fa48 100644
--- a/test/unit/core.js
+++ b/test/unit/core.js
@@ -627,6 +627,42 @@ test("toArray()", function() {
"Convert jQuery object to an Array" )
})
+test("inArray()", function() {
+ expect(19);
+
+ var selections = {
+ p: q("firstp", "sap", "ap", "first"),
+ em: q("siblingnext", "siblingfirst"),
+ div: q("qunit-testrunner-toolbar", "nothiddendiv", "nothiddendivchild", "foo"),
+ a: q("mark", "groups", "google", "simon1"),
+ empty: []
+ },
+ tests = {
+ p: { elem: jQuery("#ap")[0], index: 2 },
+ em: { elem: jQuery("#siblingfirst")[0], index: 1 },
+ div: { elem: jQuery("#nothiddendiv")[0], index: 1 },
+ a: { elem: jQuery("#simon1")[0], index: 3 }
+ },
+ falseTests = {
+ p: jQuery("#liveSpan1")[0],
+ em: jQuery("#nothiddendiv")[0],
+ empty: ""
+ };
+
+ jQuery.each( tests, function( key, obj ) {
+ equal( jQuery.inArray( obj.elem, selections[ key ] ), obj.index, "elem is in the array of selections of its tag" );
+ // Third argument (fromIndex)
+ equal( !!~jQuery.inArray( obj.elem, selections[ key ], 5 ), false, "elem is NOT in the array of selections given a starting index greater than its position" );
+ equal( !!~jQuery.inArray( obj.elem, selections[ key ], 1 ), true, "elem is in the array of selections given a starting index less than or equal to its position" );
+ equal( !!~jQuery.inArray( obj.elem, selections[ key ], -3 ), true, "elem is in the array of selections given a negative index" );
+ });
+
+ jQuery.each( falseTests, function( key, elem ) {
+ equal( !!~jQuery.inArray( elem, selections[ key ] ), false, "elem is NOT in the array of selections" );
+ });
+
+});
+
test("get(Number)", function() {
expect(2);
equals( jQuery("#qunit-fixture p").get(0), document.getElementById("firstp"), "Get A Single Element" );