HTMLForm: Use OOUI\Element::configFromHtmlAttributes instead of rolling our own
authorBartosz Dziewoński <matma.rex@gmail.com>
Wed, 2 Mar 2016 19:09:53 +0000 (20:09 +0100)
committerBartosz Dziewoński <matma.rex@gmail.com>
Wed, 9 Mar 2016 00:08:05 +0000 (00:08 +0000)
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

12 files changed:
includes/htmlform/HTMLAutoCompleteSelectField.php
includes/htmlform/HTMLButtonField.php
includes/htmlform/HTMLCheckField.php
includes/htmlform/HTMLCheckMatrix.php
includes/htmlform/HTMLComboboxField.php
includes/htmlform/HTMLFormField.php
includes/htmlform/HTMLFormFieldWithButton.php
includes/htmlform/HTMLMultiSelectField.php
includes/htmlform/HTMLRadioField.php
includes/htmlform/HTMLSelectField.php
includes/htmlform/HTMLTextAreaField.php
includes/htmlform/HTMLTextField.php

index 1f077a4..6606ca3 100644 (file)
@@ -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(
index 211089f..0b07765 100644 (file)
@@ -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() {
index 13f30c2..a59b15e 100644 (file)
@@ -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 !== '' ) {
index 6a4bec8..9f67233 100644 (file)
@@ -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', [], '&#160;' );
@@ -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' ) ) {
index a0f248f..778aedb 100644 (file)
  */
 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 ];
index ebbe323..6e5d656 100644 (file)
@@ -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];
                        }
                }
 
index 272af15..a8a5014 100644 (file)
@@ -59,7 +59,9 @@ class HTMLFormFieldWithButton extends HTMLFormField {
                        'type' => $this->mButtonType,
                        'label' => $this->mButtonValue,
                        'flags' => $this->mButtonFlags,
-               ] );
+               ] + OOUI\Element::configFromHtmlAttributes(
+                       $this->getAttributes( [ 'disabled', 'tabindex' ] )
+               ) );
        }
 
        /**
index 251bb05..1aaa3e8 100644 (file)
@@ -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',
index ecca6ff..12a8a1f 100644 (file)
@@ -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 ) {
index 6191665..b6ad46c 100644 (file)
@@ -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 ];
index b05fd7c..973f1cb 100644 (file)
@@ -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,
index fb7584b..4d5bcab 100644 (file)
@@ -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 );