From: Bartosz DziewoƄski Date: Wed, 12 Jul 2017 19:31:33 +0000 (+0200) Subject: Reduce code duplication for parsing messages into dropdown menus X-Git-Tag: 1.31.0-rc.0~2137^2 X-Git-Url: https://git.heureux-cyclage.org/?a=commitdiff_plain;h=a5c3d029c5d0f5765827d10d3fc34942a4d16853;p=lhc%2Fweb%2Fwiklou.git Reduce code duplication for parsing messages into dropdown menus The same behavior was implemented by Xml::listDropDown(), Article::confirmDelete() and HTMLFormField::getOptions(). A new function Xml::listDropDownOptions() is added and all three are changed to use it. Additionally: * Xml::listDropDown() now uses XmlSelect internally to generate the dropdown HTML. * Xml::listDropDownOptionsOoui() is introduced to handle converting the existing format to the OOUI format, which was previously duplicated in Article::confirmDelete() and HTMLFormField::getOptionsOOUI(). * This change allows HTMLForm 'select' fields (HTMLSelectField) to support nested options (optgroups) in OOUI mode. This is a prerequisite for T117781. Bug: T117781 Change-Id: I0a088f61eb32ec59677113583c7ecdcbc3fd2af0 --- diff --git a/includes/Xml.php b/includes/Xml.php index 16a5a9ddec..0091513125 100644 --- a/includes/Xml.php +++ b/includes/Xml.php @@ -493,7 +493,8 @@ class Xml { } /** - * Build a drop-down box from a textual list. + * Build a drop-down box from a textual list. This is a wrapper + * for Xml::listDropDownOptions() plus the XmlSelect class. * * @param string $name Name and id for the drop-down * @param string $list Correctly formatted text (newline delimited) to be @@ -507,60 +508,91 @@ class Xml { public static function listDropDown( $name = '', $list = '', $other = '', $selected = '', $class = '', $tabindex = null ) { - $optgroup = false; + $options = self::listDropDownOptions( $list, [ 'other' => $other ] ); + + $xmlSelect = new XmlSelect( $name, $name, $selected ); + $xmlSelect->addOptions( $options ); + + if ( $class ) { + $xmlSelect->setAttribute( 'class', $class ); + } + if ( $tabindex ) { + $xmlSelect->setAttribute( 'tabindex', $tabindex ); + } - $options = self::option( $other, 'other', $selected === 'other' ); + return $xmlSelect->getHTML(); + } + /** + * Build options for a drop-down box from a textual list. + * + * The result of this function can be passed to XmlSelect::addOptions() + * (to render a plain `' . "\n" . - '' . - '' . - '' . - '' . - '' . "\n" . + '', Xml::listDropDown( // name @@ -426,4 +429,52 @@ class XmlTest extends MediaWikiTestCase { ) ); } + + /** + * @covers Xml::listDropDownOptions + */ + public function testListDropDownOptions() { + $this->assertEquals( + [ + 'other reasons' => 'other', + 'Foo' => [ + 'Foo 1' => 'Foo 1', + 'Example' => 'Example', + ], + 'Bar' => [ + 'Bar 1' => 'Bar 1', + ], + ], + Xml::listDropDownOptions( + "* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1", + [ 'other' => 'other reasons' ] + ) + ); + } + + /** + * @covers Xml::listDropDownOptionsOoui + */ + public function testListDropDownOptionsOoui() { + $this->assertEquals( + [ + [ 'data' => 'other', 'label' => 'other reasons' ], + [ 'optgroup' => 'Foo' ], + [ 'data' => 'Foo 1', 'label' => 'Foo 1' ], + [ 'data' => 'Example', 'label' => 'Example' ], + [ 'optgroup' => 'Bar' ], + [ 'data' => 'Bar 1', 'label' => 'Bar 1' ], + ], + Xml::listDropDownOptionsOoui( [ + 'other reasons' => 'other', + 'Foo' => [ + 'Foo 1' => 'Foo 1', + 'Example' => 'Example', + ], + 'Bar' => [ + 'Bar 1' => 'Bar 1', + ], + ] ) + ); + } }