Add support for "tabindex" in HTMLFormField subclasses
authorAlexandre Emsenhuber <ialex.wiki@gmail.com>
Thu, 2 May 2013 18:27:44 +0000 (20:27 +0200)
committerReedy <reedy@wikimedia.org>
Sat, 25 Jan 2014 04:31:01 +0000 (04:31 +0000)
It is already set for some fields in Special:Block, but are
discarded by HTMLForm and its fields.

Some notes:
- fields with multiple inputs (radio, select and other, select
  or other) will have the same tabindex set on all elements
- Some items such as multi-select and check matrix are not yet
  implemented

Change-Id: I3e1ba7f16f3a3183f231afcf60dd392ce6d6eb6b

includes/htmlform/HTMLButtonField.php
includes/htmlform/HTMLCheckField.php
includes/htmlform/HTMLCheckMatrix.php
includes/htmlform/HTMLFormField.php
includes/htmlform/HTMLMultiSelectField.php
includes/htmlform/HTMLRadioField.php
includes/htmlform/HTMLSelectAndOtherField.php
includes/htmlform/HTMLSelectField.php
includes/htmlform/HTMLSelectOrOtherField.php
includes/htmlform/HTMLTextAreaField.php
includes/htmlform/HTMLTextField.php

index c95d73b..f8d017c 100644 (file)
@@ -19,11 +19,7 @@ class HTMLButtonField extends HTMLFormField {
                $attr = array(
                        'class' => 'mw-htmlform-submit ' . $this->mClass,
                        'id' => $this->mID,
-               );
-
-               if ( !empty( $this->mParams['disabled'] ) ) {
-                       $attr['disabled'] = 'disabled';
-               }
+               ) + $this->getAttributes( array( 'disabled', 'tabindex' ) );
 
                return Html::input( $this->mName, $value, $this->buttonType, $attr );
        }
index 105a884..237fa32 100644 (file)
@@ -12,9 +12,7 @@ class HTMLCheckField extends HTMLFormField {
                $attr = $this->getTooltipAndAccessKey();
                $attr['id'] = $this->mID;
 
-               if ( !empty( $this->mParams['disabled'] ) ) {
-                       $attr['disabled'] = 'disabled';
-               }
+               $attr += $this->getAttributes( array( 'disabled', 'tabindex' ) );
 
                if ( $this->mClass !== '' ) {
                        $attr['class'] = $this->mClass;
index 323d2d9..2fc170c 100644 (file)
@@ -82,14 +82,10 @@ class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable {
        function getInputHTML( $value ) {
                $html = '';
                $tableContents = '';
-               $attribs = array();
                $rows = $this->mParams['rows'];
                $columns = $this->mParams['columns'];
 
-               // If the disabled param is set, disable all the options
-               if ( !empty( $this->mParams['disabled'] ) ) {
-                       $attribs['disabled'] = 'disabled';
-               }
+               $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
 
                // Build the column headers
                $headerContents = Html::rawElement( 'td', array(), '&#160;' );
index eeed907..622dd94 100644 (file)
@@ -484,6 +484,30 @@ abstract class HTMLFormField {
                return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
        }
 
+       /**
+        * Returns the given attributes from the parameters
+        *
+        * @param array $list List of attributes to get
+        * @return array Attributes
+        */
+       public function getAttributes( array $list ) {
+               static $boolAttribs = array( 'disabled', 'required', 'autofocus', 'multiple', 'readonly' );
+
+               $ret = array();
+
+               foreach( $list as $key ) {
+                       if ( in_array( $key, $boolAttribs ) ) {
+                               if ( !empty( $this->mParams[$key] ) ) {
+                                       $ret[$key] = '';
+                               }
+                       } elseif ( isset( $this->mParams[$key] ) ) {
+                               $ret[$key] = $this->mParams[$key];
+                       }
+               }
+
+               return $ret;
+       }
+
        /**
         * flatten an array of options to a single array, for instance,
         * a set of "<options>" inside "<optgroups>".
index 6b0396d..09576d4 100644 (file)
@@ -36,11 +36,7 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable
        function formatOptions( $options, $value ) {
                $html = '';
 
-               $attribs = array();
-
-               if ( !empty( $this->mParams['disabled'] ) ) {
-                       $attribs['disabled'] = 'disabled';
-               }
+               $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
 
                foreach ( $options as $label => $info ) {
                        if ( is_array( $info ) ) {
index 51e7cdd..f206ed6 100644 (file)
@@ -41,10 +41,7 @@ class HTMLRadioField extends HTMLFormField {
        function formatOptions( $options, $value ) {
                $html = '';
 
-               $attribs = array();
-               if ( !empty( $this->mParams['disabled'] ) ) {
-                       $attribs['disabled'] = 'disabled';
-               }
+               $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
 
                # @todo Should this produce an unordered list perhaps?
                foreach ( $options as $label => $info ) {
index 7b2e865..044670d 100644 (file)
@@ -90,11 +90,15 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
                        $textAttribs['class'] = $this->mClass;
                }
 
-               foreach ( array( 'required', 'autofocus', 'multiple', 'disabled' ) as $param ) {
-                       if ( isset( $this->mParams[$param] ) ) {
-                               $textAttribs[$param] = '';
-                       }
-               }
+               $allowedParams = array(
+                       'required',
+                       'autofocus',
+                       'multiple',
+                       'disabled',
+                       'tabindex'
+               );
+
+               $textAttribs += $this->getAttributes( $allowedParams );
 
                $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
 
index d2dd432..9d719f6 100644 (file)
@@ -37,6 +37,10 @@ class HTMLSelectField extends HTMLFormField {
                        $select->setAttribute( 'disabled', 'disabled' );
                }
 
+               if ( isset( $this->mParams['tabindex'] ) ) {
+                       $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
+               }
+
                if ( $this->mClass !== '' ) {
                        $select->setAttribute( 'class', $this->mClass );
                }
index 4e322d4..08be435 100644 (file)
@@ -47,6 +47,11 @@ class HTMLSelectOrOtherField extends HTMLTextField {
                        $tbAttribs['disabled'] = 'disabled';
                }
 
+               if ( isset( $this->mParams['tabindex'] ) ) {
+                       $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
+                       $tbAttribs['tabindex'] = $this->mParams['tabindex'];
+               }
+
                $select = $select->getHTML();
 
                if ( isset( $this->mParams['maxlength'] ) ) {
index 89e7be2..4fd1989 100644 (file)
@@ -24,23 +24,16 @@ class HTMLTextAreaField extends HTMLFormField {
                        $attribs['class'] = $this->mClass;
                }
 
-               if ( !empty( $this->mParams['disabled'] ) ) {
-                       $attribs['disabled'] = 'disabled';
-               }
-
-               if ( !empty( $this->mParams['readonly'] ) ) {
-                       $attribs['readonly'] = 'readonly';
-               }
-
-               if ( isset( $this->mParams['placeholder'] ) ) {
-                       $attribs['placeholder'] = $this->mParams['placeholder'];
-               }
-
-               foreach ( array( 'required', 'autofocus' ) as $param ) {
-                       if ( isset( $this->mParams[$param] ) ) {
-                               $attribs[$param] = '';
-                       }
-               }
+               $allowedParams = array(
+                       'placeholder',
+                       'tabindex',
+                       'disabled',
+                       'readonly',
+                       'required',
+                       'autofocus'
+               );
+
+               $attribs += $this->getAttributes( $allowedParams );
 
                return Html::element( 'textarea', $attribs, $value );
        }
index fe962f4..57f0a72 100644 (file)
@@ -17,10 +17,6 @@ class HTMLTextField extends HTMLFormField {
                        $attribs['class'] = $this->mClass;
                }
 
-               if ( !empty( $this->mParams['disabled'] ) ) {
-                       $attribs['disabled'] = 'disabled';
-               }
-
                # @todo Enforce pattern, step, required, readonly on the server side as
                # well
                $allowedParams = array(
@@ -31,19 +27,16 @@ class HTMLTextField extends HTMLFormField {
                        'step',
                        'placeholder',
                        'list',
-                       'maxlength'
+                       'maxlength',
+                       'tabindex',
+                       'disabled',
+                       'required',
+                       'autofocus',
+                       'multiple',
+                       'readonly'
                );
-               foreach ( $allowedParams as $param ) {
-                       if ( isset( $this->mParams[$param] ) ) {
-                               $attribs[$param] = $this->mParams[$param];
-                       }
-               }
 
-               foreach ( array( 'required', 'autofocus', 'multiple', 'readonly' ) as $param ) {
-                       if ( isset( $this->mParams[$param] ) ) {
-                               $attribs[$param] = '';
-                       }
-               }
+               $attribs += $this->getAttributes( $allowedParams );
 
                # Implement tiny differences between some field variants
                # here, rather than creating a new class for each one which