From ee5af951679d9bf39a68fdf9eefd096aacb682f3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Wed, 2 Mar 2016 20:09:53 +0100 Subject: [PATCH] HTMLForm: Use OOUI\Element::configFromHtmlAttributes instead of rolling our own Depends on I0e5c956b9358b510c8473b1cfe6465ea1b5c07ef in OOjs UI. This mostly reverts dd04b310527c3ce69911789254939e9a2b627ada and parts of e85bd04bcd26f9a05406cedf6ee0dcac0b54c435. In addition to cleanup, it fixes bugs in HTMLFormFieldWithButton (which did not add some attributes in OOUI mode) and HTMLMultiSelectField (which did not do the mapping, losing some attributes in OOUI mode). Change-Id: I0d1a5288e9edb73a0c3a8431feca9fcc67b72b6a --- .../htmlform/HTMLAutoCompleteSelectField.php | 4 +-- includes/htmlform/HTMLButtonField.php | 4 ++- includes/htmlform/HTMLCheckField.php | 5 ++-- includes/htmlform/HTMLCheckMatrix.php | 13 +++------ includes/htmlform/HTMLComboboxField.php | 8 ++++-- includes/htmlform/HTMLFormField.php | 28 ++----------------- includes/htmlform/HTMLFormFieldWithButton.php | 4 ++- includes/htmlform/HTMLMultiSelectField.php | 5 ++-- includes/htmlform/HTMLRadioField.php | 4 ++- includes/htmlform/HTMLSelectField.php | 4 ++- includes/htmlform/HTMLTextAreaField.php | 7 ++--- includes/htmlform/HTMLTextField.php | 8 ++---- 12 files changed, 37 insertions(+), 57 deletions(-) diff --git a/includes/htmlform/HTMLAutoCompleteSelectField.php b/includes/htmlform/HTMLAutoCompleteSelectField.php index 1f077a4318..6606ca3833 100644 --- a/includes/htmlform/HTMLAutoCompleteSelectField.php +++ b/includes/htmlform/HTMLAutoCompleteSelectField.php @@ -101,11 +101,11 @@ class HTMLAutoCompleteSelectField extends HTMLTextField { } // FIXME Ewww, this shouldn't be adding any attributes not requested in $list :( - public function getAttributes( array $list, array $mappings = null ) { + public function getAttributes( array $list ) { $attribs = [ 'type' => 'text', 'data-autocomplete' => FormatJson::encode( array_keys( $this->autocomplete ) ), - ] + parent::getAttributes( $list, $mappings ); + ] + parent::getAttributes( $list ); if ( $this->getOptions() ) { $attribs['data-hide-if'] = FormatJson::encode( diff --git a/includes/htmlform/HTMLButtonField.php b/includes/htmlform/HTMLButtonField.php index 211089f765..0b07765931 100644 --- a/includes/htmlform/HTMLButtonField.php +++ b/includes/htmlform/HTMLButtonField.php @@ -104,7 +104,9 @@ class HTMLButtonField extends HTMLFormField { 'id' => $this->mID, 'flags' => $this->mFlags, 'useInputTag' => $this->isBadIE(), - ] + $this->getAttributes( [ 'disabled', 'tabindex' ], [ 'tabindex' => 'tabIndex' ] ) ); + ] + OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( [ 'disabled', 'tabindex' ] ) + ) ); } protected function needsLabel() { diff --git a/includes/htmlform/HTMLCheckField.php b/includes/htmlform/HTMLCheckField.php index 13f30c2d06..a59b15e828 100644 --- a/includes/htmlform/HTMLCheckField.php +++ b/includes/htmlform/HTMLCheckField.php @@ -56,9 +56,8 @@ class HTMLCheckField extends HTMLFormField { $attr['id'] = $this->mID; $attr['name'] = $this->mName; - $attr += $this->getAttributes( - [ 'disabled', 'tabindex' ], - [ 'tabindex' => 'tabIndex' ] + $attr += OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( [ 'disabled', 'tabindex' ] ) ); if ( $this->mClass !== '' ) { diff --git a/includes/htmlform/HTMLCheckMatrix.php b/includes/htmlform/HTMLCheckMatrix.php index 6a4bec8370..9f67233641 100644 --- a/includes/htmlform/HTMLCheckMatrix.php +++ b/includes/htmlform/HTMLCheckMatrix.php @@ -85,13 +85,7 @@ class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable { $rows = $this->mParams['rows']; $columns = $this->mParams['columns']; - $mappings = []; - - if ( $this->mParent instanceof OOUIHTMLForm ) { - $mappings['tabindex'] = 'tabIndex'; - } - - $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ], $mappings ); + $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] ); // Build the column headers $headerContents = Html::rawElement( 'td', [], ' ' ); @@ -156,8 +150,9 @@ class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable { return new OOUI\CheckboxInputWidget( [ 'name' => "{$this->mName}[]", 'selected' => $checked, - 'value' => $attribs['value'], - ] + $attribs ); + ] + OOUI\Element::configFromHtmlAttributes( + $attribs + ) ); } else { $checkbox = Xml::check( "{$this->mName}[]", $checked, $attribs ); if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) { diff --git a/includes/htmlform/HTMLComboboxField.php b/includes/htmlform/HTMLComboboxField.php index a0f248fdea..778aedbc8a 100644 --- a/includes/htmlform/HTMLComboboxField.php +++ b/includes/htmlform/HTMLComboboxField.php @@ -16,11 +16,11 @@ */ class HTMLComboboxField extends HTMLTextField { // FIXME Ewww, this shouldn't be adding any attributes not requested in $list :( - public function getAttributes( array $list, array $mappings = null ) { + public function getAttributes( array $list ) { $attribs = [ 'type' => 'text', 'list' => $this->mName . '-datalist', - ] + parent::getAttributes( $list, $mappings ); + ] + parent::getAttributes( $list ); return $attribs; } @@ -36,7 +36,9 @@ class HTMLComboboxField extends HTMLTextField { function getInputOOUI( $value ) { $disabled = false; $allowedParams = [ 'tabindex' ]; - $attribs = $this->getAttributes( $allowedParams, [ 'tabindex' => 'tabIndex' ] ); + $attribs = OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( $allowedParams ) + ); if ( $this->mClass !== '' ) { $attribs['classes'] = [ $this->mClass ]; diff --git a/includes/htmlform/HTMLFormField.php b/includes/htmlform/HTMLFormField.php index ebbe3233ee..6e5d6569f5 100644 --- a/includes/htmlform/HTMLFormField.php +++ b/includes/htmlform/HTMLFormField.php @@ -912,45 +912,23 @@ abstract class HTMLFormField { return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] ); } - /** - * Get a translated key if necessary. - * @param array|null $mappings Array of mappings, 'original' => 'translated' - * @param string $key - * @return string - */ - protected function getMappedKey( $mappings, $key ) { - if ( !is_array( $mappings ) ) { - return $key; - } - - if ( !empty( $mappings[$key] ) ) { - return $mappings[$key]; - } - - return $key; - } - /** * Returns the given attributes from the parameters * * @param array $list List of attributes to get - * @param array $mappings Optional - Key/value map of attribute names to use - * instead of the ones passed in. * @return array Attributes */ - public function getAttributes( array $list, array $mappings = null ) { + public function getAttributes( array $list ) { static $boolAttribs = [ 'disabled', 'required', 'autofocus', 'multiple', 'readonly' ]; $ret = []; foreach ( $list as $key ) { - $mappedKey = $this->getMappedKey( $mappings, $key ); - if ( in_array( $key, $boolAttribs ) ) { if ( !empty( $this->mParams[$key] ) ) { - $ret[$mappedKey] = $mappedKey; + $ret[$key] = ''; } } elseif ( isset( $this->mParams[$key] ) ) { - $ret[$mappedKey] = $this->mParams[$key]; + $ret[$key] = $this->mParams[$key]; } } diff --git a/includes/htmlform/HTMLFormFieldWithButton.php b/includes/htmlform/HTMLFormFieldWithButton.php index 272af15479..a8a5014c65 100644 --- a/includes/htmlform/HTMLFormFieldWithButton.php +++ b/includes/htmlform/HTMLFormFieldWithButton.php @@ -59,7 +59,9 @@ class HTMLFormFieldWithButton extends HTMLFormField { 'type' => $this->mButtonType, 'label' => $this->mButtonValue, 'flags' => $this->mButtonFlags, - ] ); + ] + OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( [ 'disabled', 'tabindex' ] ) + ) ); } /** diff --git a/includes/htmlform/HTMLMultiSelectField.php b/includes/htmlform/HTMLMultiSelectField.php index 251bb051a8..1aaa3e88fa 100644 --- a/includes/htmlform/HTMLMultiSelectField.php +++ b/includes/htmlform/HTMLMultiSelectField.php @@ -72,8 +72,9 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable new OOUI\CheckboxInputWidget( [ 'name' => "{$this->mName}[]", 'selected' => $checked, - 'value' => $attribs['value'], - ] + $attribs ), + ] + OOUI\Element::configFromHtmlAttributes( + $attribs + ) ), [ 'label' => $label, 'align' => 'inline', diff --git a/includes/htmlform/HTMLRadioField.php b/includes/htmlform/HTMLRadioField.php index ecca6ff807..12a8a1fd37 100644 --- a/includes/htmlform/HTMLRadioField.php +++ b/includes/htmlform/HTMLRadioField.php @@ -53,7 +53,9 @@ class HTMLRadioField extends HTMLFormField { 'value' => $value, 'options' => $options, 'classes' => 'mw-htmlform-flatlist-item', - ] + $this->getAttributes( [ 'disabled', 'tabindex' ], [ 'tabindex' => 'tabIndex' ] ) ); + ] + OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( [ 'disabled', 'tabindex' ] ) + ) ); } function formatOptions( $options, $value ) { diff --git a/includes/htmlform/HTMLSelectField.php b/includes/htmlform/HTMLSelectField.php index 6191665903..b6ad46c5ae 100644 --- a/includes/htmlform/HTMLSelectField.php +++ b/includes/htmlform/HTMLSelectField.php @@ -45,7 +45,9 @@ class HTMLSelectField extends HTMLFormField { function getInputOOUI( $value ) { $disabled = false; $allowedParams = [ 'tabindex' ]; - $attribs = $this->getAttributes( $allowedParams, [ 'tabindex' => 'tabIndex' ] ); + $attribs = OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( $allowedParams ) + ); if ( $this->mClass !== '' ) { $attribs['classes'] = [ $this->mClass ]; diff --git a/includes/htmlform/HTMLTextAreaField.php b/includes/htmlform/HTMLTextAreaField.php index b05fd7c79a..973f1cb8d8 100644 --- a/includes/htmlform/HTMLTextAreaField.php +++ b/includes/htmlform/HTMLTextAreaField.php @@ -66,10 +66,9 @@ class HTMLTextAreaField extends HTMLFormField { 'autofocus', ]; - $attribs += $this->getAttributes( $allowedParams, [ - 'tabindex' => 'tabIndex', - 'readonly' => 'readOnly', - ] ); + $attribs += OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( $allowedParams ) + ); return new OOUI\TextInputWidget( [ 'id' => $this->mID, diff --git a/includes/htmlform/HTMLTextField.php b/includes/htmlform/HTMLTextField.php index fb7584b7d1..4d5bcabbc8 100644 --- a/includes/htmlform/HTMLTextField.php +++ b/includes/htmlform/HTMLTextField.php @@ -107,11 +107,9 @@ class HTMLTextField extends HTMLFormField { 'type', ]; - $attribs += $this->getAttributes( $allowedParams, [ - 'maxlength' => 'maxLength', - 'readonly' => 'readOnly', - 'tabindex' => 'tabIndex', - ] ); + $attribs += OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( $allowedParams ) + ); $type = $this->getType( $attribs ); -- 2.20.1