From cb9a9a7b4751b016aa1e360ff3d44f2a5b285516 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Thu, 2 May 2013 20:27:44 +0200 Subject: [PATCH] Add support for "tabindex" in HTMLFormField subclasses 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 | 6 +---- includes/htmlform/HTMLCheckField.php | 4 +-- includes/htmlform/HTMLCheckMatrix.php | 6 +---- includes/htmlform/HTMLFormField.php | 24 +++++++++++++++++ includes/htmlform/HTMLMultiSelectField.php | 6 +---- includes/htmlform/HTMLRadioField.php | 5 +--- includes/htmlform/HTMLSelectAndOtherField.php | 14 ++++++---- includes/htmlform/HTMLSelectField.php | 4 +++ includes/htmlform/HTMLSelectOrOtherField.php | 5 ++++ includes/htmlform/HTMLTextAreaField.php | 27 +++++++------------ includes/htmlform/HTMLTextField.php | 23 ++++++---------- 11 files changed, 65 insertions(+), 59 deletions(-) diff --git a/includes/htmlform/HTMLButtonField.php b/includes/htmlform/HTMLButtonField.php index c95d73b64c..f8d017c685 100644 --- a/includes/htmlform/HTMLButtonField.php +++ b/includes/htmlform/HTMLButtonField.php @@ -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 ); } diff --git a/includes/htmlform/HTMLCheckField.php b/includes/htmlform/HTMLCheckField.php index 105a884da6..237fa320de 100644 --- a/includes/htmlform/HTMLCheckField.php +++ b/includes/htmlform/HTMLCheckField.php @@ -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; diff --git a/includes/htmlform/HTMLCheckMatrix.php b/includes/htmlform/HTMLCheckMatrix.php index 323d2d97f4..2fc170c83e 100644 --- a/includes/htmlform/HTMLCheckMatrix.php +++ b/includes/htmlform/HTMLCheckMatrix.php @@ -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(), ' ' ); diff --git a/includes/htmlform/HTMLFormField.php b/includes/htmlform/HTMLFormField.php index eeed907487..622dd9410b 100644 --- a/includes/htmlform/HTMLFormField.php +++ b/includes/htmlform/HTMLFormField.php @@ -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 "" inside "". diff --git a/includes/htmlform/HTMLMultiSelectField.php b/includes/htmlform/HTMLMultiSelectField.php index 6b0396da15..09576d438d 100644 --- a/includes/htmlform/HTMLMultiSelectField.php +++ b/includes/htmlform/HTMLMultiSelectField.php @@ -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 ) ) { diff --git a/includes/htmlform/HTMLRadioField.php b/includes/htmlform/HTMLRadioField.php index 51e7cddf1d..f206ed65d0 100644 --- a/includes/htmlform/HTMLRadioField.php +++ b/includes/htmlform/HTMLRadioField.php @@ -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 ) { diff --git a/includes/htmlform/HTMLSelectAndOtherField.php b/includes/htmlform/HTMLSelectAndOtherField.php index 7b2e865d05..044670df04 100644 --- a/includes/htmlform/HTMLSelectAndOtherField.php +++ b/includes/htmlform/HTMLSelectAndOtherField.php @@ -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 ); diff --git a/includes/htmlform/HTMLSelectField.php b/includes/htmlform/HTMLSelectField.php index d2dd4326e7..9d719f640c 100644 --- a/includes/htmlform/HTMLSelectField.php +++ b/includes/htmlform/HTMLSelectField.php @@ -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 ); } diff --git a/includes/htmlform/HTMLSelectOrOtherField.php b/includes/htmlform/HTMLSelectOrOtherField.php index 4e322d4ed6..08be435500 100644 --- a/includes/htmlform/HTMLSelectOrOtherField.php +++ b/includes/htmlform/HTMLSelectOrOtherField.php @@ -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'] ) ) { diff --git a/includes/htmlform/HTMLTextAreaField.php b/includes/htmlform/HTMLTextAreaField.php index 89e7be2e92..4fd198976b 100644 --- a/includes/htmlform/HTMLTextAreaField.php +++ b/includes/htmlform/HTMLTextAreaField.php @@ -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 ); } diff --git a/includes/htmlform/HTMLTextField.php b/includes/htmlform/HTMLTextField.php index fe962f4ac1..57f0a72527 100644 --- a/includes/htmlform/HTMLTextField.php +++ b/includes/htmlform/HTMLTextField.php @@ -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 -- 2.20.1