Anomie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/259410

Change subject: HTMLForm: Use <button> and allow differing label and value
......................................................................

HTMLForm: Use <button> and allow differing label and value

Submit buttons, whether via HTMLForm::addButton() or HTMLSubmitField,
are difficult to use effectively when the submitted value has to match
the internationalized display label. HTML5 (and HTML4 for that matter)
already has <button> that does what we need, we just need to actually
use it.

Bug: T121584
Change-Id: I7e15331efb162275c4116bcae61f19d6b884cbe3
---
M includes/htmlform/HTMLButtonField.php
M includes/htmlform/HTMLForm.php
M includes/htmlform/OOUIHTMLForm.php
3 files changed, 92 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/10/259410/1

diff --git a/includes/htmlform/HTMLButtonField.php 
b/includes/htmlform/HTMLButtonField.php
index 1d45e5f..f3421c2 100644
--- a/includes/htmlform/HTMLButtonField.php
+++ b/includes/htmlform/HTMLButtonField.php
@@ -9,6 +9,7 @@
  */
 class HTMLButtonField extends HTMLFormField {
        protected $buttonType = 'button';
+       protected $buttonLabel = null;
 
        /** @var array $mFlags Flags to add to OOUI Button widget */
        protected $mFlags = array();
@@ -18,6 +19,30 @@
                if ( isset( $info['flags'] ) ) {
                        $this->mFlags = $info['flags'];
                }
+
+               # Generate the label from a message, if possible
+               if ( isset( $info['buttonlabel-message'] ) ) {
+                       $msgInfo = $info['buttonlabel-message'];
+
+                       if ( is_array( $msgInfo ) ) {
+                               $msg = array_shift( $msgInfo );
+                       } else {
+                               $msg = $msgInfo;
+                               $msgInfo = array();
+                       }
+
+                       $this->buttonLabel = $this->msg( $msg, $msgInfo 
)->parse();
+               } elseif ( isset( $info['buttonlabel'] ) ) {
+                       if ( $info['buttonlabel'] === '&#160;' ) {
+                               // Apparently some things set &nbsp directly 
and in an odd format
+                               $this->buttonLabel = '&#160;';
+                       } else {
+                               $this->buttonLabel = htmlspecialchars( 
$info['buttonlabel'] );
+                       }
+               } elseif ( isset( $info['buttonlabel-raw'] ) ) {
+                       $this->buttonLabel = $info['buttonlabel-raw'];
+               }
+
                parent::__construct( $info );
        }
 
@@ -37,9 +62,12 @@
                $attr = array(
                        'class' => 'mw-htmlform-submit ' . $this->mClass . 
$flags,
                        'id' => $this->mID,
+                       'type' => $this->buttonType,
+                       'name' => $this->mName,
+                       'value' => $value,
                ) + $this->getAttributes( array( 'disabled', 'tabindex' ) );
 
-               return Html::input( $this->mName, $value, $this->buttonType, 
$attr );
+               return Html::rawElement( 'button', $attr, $this->buttonLabel ?: 
htmlspecialchars( $value ) );
        }
 
        /**
@@ -51,7 +79,7 @@
                return new OOUI\ButtonInputWidget( array(
                        'name' => $this->mName,
                        'value' => $value,
-                       'label' => $value,
+                       'label' => $this->buttonLabel ? new OOUI\HtmlSnippet( 
$this->buttonLabel ) : $value,
                        'type' => $this->buttonType,
                        'classes' => array( 'mw-htmlform-submit', $this->mClass 
),
                        'id' => $this->mID,
diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php
index b0d90af..ed60d21 100644
--- a/includes/htmlform/HTMLForm.php
+++ b/includes/htmlform/HTMLForm.php
@@ -855,15 +855,47 @@
        /**
         * Add a button to the form
         *
-        * @param string $name Field name.
-        * @param string $value Field value
-        * @param string $id DOM id for the button (default: null)
-        * @param array $attribs
-        *
+        * @since 1.27 takes an array as shown. Earlier versions accepted
+        *  'name', 'value', 'id', and 'attribs' as separate parameters in that
+        *  order.
+        * @param array $data Data to define the button:
+        *  - name: (string) Button name.
+        *  - value: (string) Button value.
+        *  - label: (string, optional) Button label text.
+        *  - label-raw: (string, optional) Button label HTML.
+        *  - label-message: (string, optional) Button label message key.
+        *  - id: (string, optional) DOM id for the button.
+        *  - attribs: (array) Additional HTML attributes.
+        *  - flags: (string|string[]) OOUI flags.
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
-       public function addButton( $name, $value, $id = null, $attribs = null ) 
{
-               $this->mButtons[] = compact( 'name', 'value', 'id', 'attribs' );
+       public function addButton( $data ) {
+               if ( !is_array( $data ) ) {
+                       $args = func_get_args();
+                       if ( count( $args ) < 2 || count( $args ) > 4 ) {
+                               throw new InvalidArgumentException(
+                                       'Incorrect number of arguments for 
deprecated calling style'
+                               );
+                       }
+                       $data = array(
+                               'name' => $args[0],
+                               'value' => $args[1],
+                               'id' => isset( $args[2] ) ? $args[2] : null,
+                               'attribs' => isset( $args[3] ) ? $args[3] : 
null,
+                       );
+               } else {
+                       if ( !isset( $data['name'] ) ) {
+                               throw new InvalidArgumentException( 'A name is 
required' );
+                       }
+                       if ( !isset( $data['value'] ) ) {
+                               throw new InvalidArgumentException( 'A value is 
required' );
+                       }
+               }
+               $this->mButtons[] = $data + array(
+                       'id' => null,
+                       'attribs' => null,
+                       'flags' => null,
+               );
 
                return $this;
        }
@@ -1048,6 +1080,16 @@
                                'value' => $button['value']
                        );
 
+                       if ( isset( $button['label-message'] ) ) {
+                               $label = $this->msg( $button['label-message'] 
)->parse();
+                       } elseif ( isset( $button['label'] ) ) {
+                               $label = htmlspecialchars( $button['label'] );
+                       } elseif ( isset( $button['label-raw'] ) ) {
+                               $label = $button['label-raw'];
+                       } else {
+                               $label = htmlspecialchars( $button['value'] );
+                       }
+
                        if ( $button['attribs'] ) {
                                $attrs += $button['attribs'];
                        }
@@ -1061,7 +1103,7 @@
                                $attrs['class'][] = 'mw-ui-button';
                        }
 
-                       $buttons .= Html::element( 'input', $attrs ) . "\n";
+                       $buttons .= Html::rawElement( 'button', $attrs, $label 
) . "\n";
                }
 
                $html = Html::rawElement( 'span',
diff --git a/includes/htmlform/OOUIHTMLForm.php 
b/includes/htmlform/OOUIHTMLForm.php
index c11449a..12c8f27 100644
--- a/includes/htmlform/OOUIHTMLForm.php
+++ b/includes/htmlform/OOUIHTMLForm.php
@@ -92,13 +92,24 @@
                                $attrs['id'] = $button['id'];
                        }
 
+                       if ( isset( $button['label-message'] ) ) {
+                               $label = new OOUI\HtmlSnippet( $this->msg( 
$button['label-message'] )->parse() );
+                       } elseif ( isset( $button['label'] ) ) {
+                               $label = $button['label'];
+                       } elseif ( isset( $button['label-raw'] ) ) {
+                               $label = new OOUI\HtmlSnippet( 
$button['label-raw'] );
+                       } else {
+                               $label = $button['value'];
+                       }
+
                        $attrs['classes'] = isset( $attrs['class'] ) ? 
(array)$attrs['class'] : array();
 
                        $buttons .= new OOUI\ButtonInputWidget( array(
                                'type' => 'submit',
                                'name' => $button['name'],
                                'value' => $button['value'],
-                               'label' => $button['value'],
+                               'label' => $label,
+                               'flags' => $button['flags'],
                        ) + $attrs );
                }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/259410
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7e15331efb162275c4116bcae61f19d6b884cbe3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to