jquery.tabIndex.js was broken, not it works (for the first time?)
authorKrinkle <krinkle@users.mediawiki.org>
Sun, 22 May 2011 23:52:16 +0000 (23:52 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Sun, 22 May 2011 23:52:16 +0000 (23:52 +0000)
* Defaulting to null
* Setting to initial value during first iteration (instead of outside the loop), that way we dont have to set minTabIndex to some insane high value to let if (  .. > .. ) work
* (bug 29048) jQuery.tabIndex: firstTabIndex() outputs the same as lastTabIndex()
* Added extra <textarea> to test suite to make sure the 'correct answer' is not the first or last element

resources/jquery/jquery.tabIndex.js
tests/qunit/index.html
tests/qunit/suites/resources/jquery/jquery.tabIndex.js

index 9d4da4d..ec5371d 100644 (file)
@@ -8,10 +8,12 @@
  * @return number Lowest tabindex on the page
  */
 $.fn.firstTabIndex = function() {
-       var minTabIndex = 0;
-       $(this).find( '[tabindex]' ).each( function() {
+       var minTabIndex = null;
+       $(this).find( '[tabindex]' ).each( function( i ) {
                var tabIndex = parseInt( $(this).attr( 'tabindex' ), 10 );
-               if ( tabIndex > minTabIndex ) {
+               if ( i === 0 ) {
+                       minTabIndex = tabIndex;
+               } else if ( tabIndex < minTabIndex ) {
                        minTabIndex = tabIndex;
                }
        } );
@@ -24,13 +26,15 @@ $.fn.firstTabIndex = function() {
  * @return number Highest tabindex on the page
  */
 $.fn.lastTabIndex = function() {
-       var maxTabIndex = 0;
-       $(this).find( '[tabindex]' ).each( function() {
+       var maxTabIndex = null;
+       $(this).find( '[tabindex]' ).each( function( i ) {
                var tabIndex = parseInt( $(this).attr( 'tabindex' ), 10 );
-               if ( tabIndex > maxTabIndex ) {
+               if ( i === 0 ) {
+                       maxTabIndex = tabIndex;
+               } else if ( tabIndex > maxTabIndex ) {
                        maxTabIndex = tabIndex;
                }
        } );
        return maxTabIndex;
 };
-} )( jQuery );
\ No newline at end of file
+} )( jQuery );
index a59cda5..d5a1a2a 100644 (file)
@@ -47,6 +47,7 @@
        <script src="suites/resources/jquery/jquery.mwPrototypes.js"></script>
        <script src="suites/resources/mediawiki.util/mediawiki.util.js"></script>
        <script src="suites/resources/jquery/jquery.autoEllipsis.js"></script>
+       <script src="suites/resources/jquery/jquery.colorUtil.js"></script>
        <script src="suites/resources/jquery/jquery.tabIndex.js"></script>
 
        <!-- TestSwarm: If a test swarm is running this,
index 32862cd..73a1800 100644 (file)
@@ -12,15 +12,20 @@ test( 'firstTabIndex', function(){
        var testEnvironment = 
 '<form>\
        <input tabindex="7" />\
-       <input tabindex="2" />\
-       <textarea tabindex="9">Foobar</textarea>\
+       <input tabindex="9" />\
+       <textarea tabindex="2">Foobar</textarea>\
+       <textarea tabindex="5">Foobar</textarea>\
 </form>';
-       var $test = $( '<div />' ).html( testEnvironment ).appendTo( 'body' );
+       var $testA = $( '<div />' ).html( testEnvironment ).appendTo( 'body' );
 
-       deepEqual( $test.firstTabIndex(), 2, 'First tabindex should be 2 within this context.' );
+       deepEqual( $testA.firstTabIndex(), 2, 'First tabindex should be 2 within this context.' );
+
+       var $testB = $( '<div />' );
+
+       deepEqual( $testB.firstTabIndex(), null, 'Return null if none available.' );
 
        // Clean up
-       $test.remove();
+       $testA.add( $testB).remove();
 });
 
 test( 'lastTabIndex', function(){
@@ -28,13 +33,18 @@ test( 'lastTabIndex', function(){
        var testEnvironment = 
 '<form>\
        <input tabindex="7" />\
-       <input tabindex="2" />\
-       <textarea tabindex="9">Foobar</textarea>\
+       <input tabindex="9" />\
+       <textarea tabindex="2">Foobar</textarea>\
+       <textarea tabindex="5">Foobar</textarea>\
 </form>';
-       var $test = $( '<div />' ).html( testEnvironment ).appendTo( 'body' );
+       var $testA = $( '<div />' ).html( testEnvironment ).appendTo( 'body' );
+
+       deepEqual( $testA.lastTabIndex(), 9, 'Last tabindex should be 9 within this context.' );
+
+       var $testB = $( '<div />' );
 
-       deepEqual( $test.lastTabIndex(), 9, 'Last tabindex should be 9 within this context.' );
+       deepEqual( $testB.lastTabIndex(), null, 'Return null if none available.' );
 
        // Clean up
-       $test.remove();
+       $testA.add( $testB).remove();
 });
\ No newline at end of file