From b524a4333f9c1b42bf6ac0f6caa3055bd8acf84a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Sat, 11 Jul 2015 18:46:05 +0200 Subject: [PATCH] OOUIHTMLForm: Support setWrapperLegend() Changed FormSpecialPage not to call setWrapperLegend() for OOUI forms to preserve current default behavior. Bonus: * Correct documentation of setWrapperLegend() to state that it HTML-escapes legend text. * Remove hard-coded class="visualClear" in getFormAttributes(). * Allow setWrapperLegend( true ) to display the wrapper without legend text. * Rejigger things so that we can put the legend and "header HTML" into correct order. Bug: T103026 Change-Id: I847c5e18ae5469aa3a68cc9fa37b2a6614476ca2 --- includes/htmlform/HTMLForm.php | 31 +++++++++----- includes/htmlform/OOUIHTMLForm.php | 41 ++++++++++--------- includes/htmlform/VFormHTMLForm.php | 2 +- includes/specialpage/FormSpecialPage.php | 6 ++- .../src/mediawiki/mediawiki.htmlform.ooui.css | 3 +- 5 files changed, 49 insertions(+), 34 deletions(-) diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index 702651ea83..43cac880bf 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -869,7 +869,8 @@ class HTMLForm extends ContextSource { $html = '' . $this->getErrors( $submitResult ) - . $this->mHeader + // In OOUI forms, we handle mHeader elsewhere. FIXME This is horrible. + . ( $this->getDisplayFormat() === 'ooui' ? '' : $this->mHeader ) . $this->getBody() . $this->getHiddenFields() . $this->getButtons() @@ -893,7 +894,6 @@ class HTMLForm extends ContextSource { $attribs = array( 'action' => $this->getAction(), 'method' => $this->getMethod(), - 'class' => array( 'visualClear' ), 'enctype' => $encType, ); if ( !empty( $this->mId ) ) { @@ -912,10 +912,11 @@ class HTMLForm extends ContextSource { function wrapForm( $html ) { # Include a
wrapper for style, if requested. if ( $this->mWrapperLegend !== false ) { - $html = Xml::fieldset( $this->mWrapperLegend, $html ); + $legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend : false; + $html = Xml::fieldset( $legend, $html ); } - return Html::rawElement( 'form', $this->getFormAttributes(), $html ); + return Html::rawElement( 'form', $this->getFormAttributes() + array( 'class' => 'visualClear' ), $html ); } /** @@ -1219,9 +1220,10 @@ class HTMLForm extends ContextSource { * Prompt the whole form to be wrapped in a "
", with * this text as its "" element. * - * @param string|bool $legend HTML to go inside the "" element, or - * false for no - * Will be escaped + * @param string|bool $legend If false, no wrapper or legend will be displayed. + * If true, a wrapper will be displayed, but no legend. + * If a string, a wrapper will be displayed with that string as a legend. + * The string will be escaped before being output (this doesn't support HTML). * * @return HTMLForm $this for chaining calls (since 1.20) */ @@ -1409,19 +1411,26 @@ class HTMLForm extends ContextSource { if ( $sectionName ) { $config['id'] = Sanitizer::escapeId( $sectionName ); } + if ( is_string( $this->mWrapperLegend ) ) { + $config['label'] = $this->mWrapperLegend; + } $fieldset = new OOUI\FieldsetLayout( $config ); // Ewww. We should pass this as $config['items'], but there might be string snippets. $fieldset->group->appendContent( new OOUI\HtmlSnippet( $html ) ); - $html = $fieldset->toString(); + $html = $fieldset; } else { $html = Html::rawElement( 'div', $attribs, "\n$html\n" ); } } - if ( $this->mSubSectionBeforeFields ) { - return $subsectionHtml . "\n" . $html; + if ( $subsectionHtml ) { + if ( $this->mSubSectionBeforeFields ) { + return $subsectionHtml . "\n" . $html; + } else { + return $html . "\n" . $subsectionHtml; + } } else { - return $html . "\n" . $subsectionHtml; + return $html; } } diff --git a/includes/htmlform/OOUIHTMLForm.php b/includes/htmlform/OOUIHTMLForm.php index 80e91f7ed2..fe2f26eb1d 100644 --- a/includes/htmlform/OOUIHTMLForm.php +++ b/includes/htmlform/OOUIHTMLForm.php @@ -25,12 +25,6 @@ * Compact stacked vertical format for forms, implemented using OOUI widgets. */ class OOUIHTMLForm extends HTMLForm { - /** - * Wrapper and its legend are never generated in OOUI mode. - * @var boolean - */ - protected $mWrapperLegend = false; - public function __construct( $descriptor, $context = null, $messagePrefix = '' ) { parent::__construct( $descriptor, $context, $messagePrefix ); $this->getOutput()->enableOOUI(); @@ -110,23 +104,30 @@ class OOUIHTMLForm extends HTMLForm { return $html; } - function getFormAttributes() { - $attribs = parent::getFormAttributes(); - if ( !isset( $attribs['class'] ) ) { - $attribs['class'] = ''; + function getBody() { + $fieldset = parent::getBody(); + // FIXME This only works for forms with no subsections + if ( $fieldset instanceof OOUI\FieldsetLayout ) { + $fieldset->group->prependContent( new OOUI\HtmlSnippet( $this->mHeader ) ); } - - if ( is_string( $attribs['class'] ) ) { - $attribs['class'] = trim( $attribs['class'] . ' mw-htmlform-ooui' ); - } else { - $attribs['class'][] = 'mw-htmlform-ooui'; - } - - return $attribs; + return $fieldset; } function wrapForm( $html ) { - // Always discard $this->mWrapperLegend - return Html::rawElement( 'form', $this->getFormAttributes(), $html ); + $form = new OOUI\FormLayout( $this->getFormAttributes() + array( + 'classes' => array( 'mw-htmlform-ooui' ), + 'content' => new OOUI\HtmlSnippet( $html ), + ) ); + + // Include a wrapper for style, if requested. + $form = new OOUI\PanelLayout( array( + 'classes' => array( 'mw-htmlform-ooui-wrapper' ), + 'expanded' => false, + 'padded' => $this->mWrapperLegend !== false, + 'framed' => $this->mWrapperLegend !== false, + 'content' => $form, + ) ); + + return $form; } } diff --git a/includes/htmlform/VFormHTMLForm.php b/includes/htmlform/VFormHTMLForm.php index f1fd05d653..c544f161e9 100644 --- a/includes/htmlform/VFormHTMLForm.php +++ b/includes/htmlform/VFormHTMLForm.php @@ -65,7 +65,7 @@ class VFormHTMLForm extends HTMLForm { protected function getFormAttributes() { $attribs = parent::getFormAttributes(); - array_push( $attribs['class'], 'mw-ui-vform', 'mw-ui-container' ); + $attribs['class'] = array( 'mw-ui-vform', 'mw-ui-container', 'visualClear' ); return $attribs; } diff --git a/includes/specialpage/FormSpecialPage.php b/includes/specialpage/FormSpecialPage.php index 90567617fa..42c59806b4 100644 --- a/includes/specialpage/FormSpecialPage.php +++ b/includes/specialpage/FormSpecialPage.php @@ -96,7 +96,11 @@ abstract class FormSpecialPage extends SpecialPage { $this->getMessagePrefix() ); $form->setSubmitCallback( array( $this, 'onSubmit' ) ); - $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' ); + if ( $this->getDisplayFormat() !== 'ooui' ) { + // No legend and wrapper by default in OOUI forms, but can be set manually + // from alterForm() + $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' ); + } $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' ); if ( !$headerMsg->isDisabled() ) { diff --git a/resources/src/mediawiki/mediawiki.htmlform.ooui.css b/resources/src/mediawiki/mediawiki.htmlform.ooui.css index 92294c94c5..31d9854d73 100644 --- a/resources/src/mediawiki/mediawiki.htmlform.ooui.css +++ b/resources/src/mediawiki/mediawiki.htmlform.ooui.css @@ -1,7 +1,8 @@ /* OOUIHTMLForm styles */ -.mw-htmlform-ooui { +.mw-htmlform-ooui-wrapper { width: 50em; + margin: 1em 0; } .mw-htmlform-ooui .mw-htmlform-submit-buttons { -- 2.20.1