Merge "This patch reduces the edit summary length to 500 characters"
[lhc/web/wiklou.git] / includes / htmlform / OOUIHTMLForm.php
index 46b570d..ba36888 100644 (file)
@@ -66,7 +66,10 @@ class OOUIHTMLForm extends HTMLForm {
                        }
 
                        if ( isset( $this->mSubmitTooltip ) ) {
-                               $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
+                               $attribs += [
+                                       'title' => Linker::titleAttrib( $this->mSubmitTooltip ),
+                                       'accessKey' => Linker::accesskey( $this->mSubmitTooltip ),
+                               ];
                        }
 
                        $attribs['classes'] = [ 'mw-htmlform-submit' ];
@@ -173,16 +176,22 @@ class OOUIHTMLForm extends HTMLForm {
         * @return string HTML
         */
        protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
-               $config = [
-                       'items' => $fieldsHtml,
-               ];
-               if ( $sectionName ) {
-                       $config['id'] = Sanitizer::escapeId( $sectionName );
+               if ( !$fieldsHtml ) {
+                       // Do not generate any wrappers for empty sections. Sections may be empty if they only have
+                       // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
+                       return '';
                }
-               if ( is_string( $this->mWrapperLegend ) ) {
-                       $config['label'] = $this->mWrapperLegend;
+
+               $html = implode( '', $fieldsHtml );
+
+               if ( $sectionName ) {
+                       $html = Html::rawElement(
+                               'div',
+                               [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ],
+                               $html
+                       );
                }
-               return new OOUI\FieldsetLayout( $config );
+               return $html;
        }
 
        /**
@@ -191,15 +200,16 @@ class OOUIHTMLForm extends HTMLForm {
         * @return string
         */
        public function getErrorsOrWarnings( $elements, $elementsType ) {
-               if ( !in_array( $elementsType, [ 'error', 'warning' ] ) ) {
+               if ( $elements === '' ) {
+                       return '';
+               }
+
+               if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
                        throw new DomainException( $elementsType . ' is not a valid type.' );
                }
-               if ( !$elements ) {
-                       $errors = [];
-               } elseif ( $elements instanceof Status ) {
-                       if ( $elements->isGood() ) {
-                               $errors = [];
-                       } else {
+               $errors = [];
+               if ( $elements instanceof Status ) {
+                       if ( !$elements->isGood() ) {
                                $errors = $elements->getErrorsByType( $elementsType );
                                foreach ( $errors as &$error ) {
                                        // Input:  [ 'message' => 'foo', 'errors' => [ 'a', 'b', 'c' ] ]
@@ -207,13 +217,12 @@ class OOUIHTMLForm extends HTMLForm {
                                        $error = array_merge( [ $error['message'] ], $error['params'] );
                                }
                        }
-               } elseif ( $elementsType === 'errors' )  {
-                       $errors = $elements;
-                       if ( !is_array( $errors ) ) {
-                               $errors = [ $errors ];
+               } elseif ( $elementsType === 'error' ) {
+                       if ( is_array( $elements ) ) {
+                               $errors = $elements;
+                       } elseif ( is_string( $elements ) ) {
+                               $errors = [ $elements ];
                        }
-               } else {
-                       $errors = [];
                }
 
                foreach ( $errors as &$error ) {
@@ -240,9 +249,8 @@ class OOUIHTMLForm extends HTMLForm {
        }
 
        public function getBody() {
-               $fieldset = parent::getBody();
-               // FIXME This only works for forms with no subsections
-               if ( $fieldset instanceof OOUI\FieldsetLayout ) {
+               $html = parent::getBody();
+               if ( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) {
                        $classes = [ 'mw-htmlform-ooui-header' ];
                        if ( $this->oouiErrors ) {
                                $classes[] = 'mw-htmlform-ooui-header-errors';
@@ -250,33 +258,42 @@ class OOUIHTMLForm extends HTMLForm {
                        if ( $this->oouiWarnings ) {
                                $classes[] = 'mw-htmlform-ooui-header-warnings';
                        }
-                       if ( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) {
-                               // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
-                               if ( $this->mHeader ) {
-                                       $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
-                               } else {
-                                       $element = new OOUI\Widget( [] );
-                               }
-                               $fieldset->addItems( [
-                                       new OOUI\FieldLayout(
-                                               $element,
-                                               [
-                                                       'align' => 'top',
-                                                       'errors' => $this->oouiErrors,
-                                                       'notices' => $this->oouiWarnings,
-                                                       'classes' => $classes,
-                                               ]
-                                       )
-                               ], 0 );
+                       // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
+                       if ( $this->mHeader ) {
+                               $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
+                       } else {
+                               $element = new OOUI\Widget( [] );
                        }
+                       $html = new OOUI\FieldLayout(
+                               $element,
+                               [
+                                       'align' => 'top',
+                                       'errors' => $this->oouiErrors,
+                                       'notices' => $this->oouiWarnings,
+                                       'classes' => $classes,
+                               ]
+                       ) . $html;
                }
-               return $fieldset;
+               return $html;
        }
 
        public function wrapForm( $html ) {
+               if ( is_string( $this->mWrapperLegend ) ) {
+                       $content = new OOUI\FieldsetLayout( [
+                               'label' => $this->mWrapperLegend,
+                               'items' => [
+                                       new OOUI\Widget( [
+                                               'content' => new OOUI\HtmlSnippet( $html )
+                                       ] ),
+                               ],
+                       ] );
+               } else {
+                       $content = new OOUI\HtmlSnippet( $html );
+               }
+
                $form = new OOUI\FormLayout( $this->getFormAttributes() + [
-                       'classes' => [ 'mw-htmlform-ooui' ],
-                       'content' => new OOUI\HtmlSnippet( $html ),
+                       'classes' => [ 'mw-htmlform', 'mw-htmlform-ooui' ],
+                       'content' => $content,
                ] );
 
                // Include a wrapper for style, if requested.