Don't accept empty option group names for dropdown elements

When this code parses a string that looks like this:

*
** A
** B

It creates an option group with 2 options, but the name
of the group is an empty string. This makes the OOUI
DropdownInputWidget fail later.

Since the empty group name is useless anyway, drop it. This
makes the code behave as if the parsed string looked like
this:

* A
* B

Live example for the issue:
https://el.wikipedia.org/wiki/MediaWiki:Protect-dropdown

Bug: T275125
Change-Id: I780c1be27740b0ed3b35aa569b5a528112d7238f
This commit is contained in:
Thiemo Kreuz 2021-02-19 15:52:53 +01:00
parent 09d2fd495e
commit 47171f7b94
2 changed files with 10 additions and 5 deletions

View file

@ -557,10 +557,14 @@ class Xml {
} elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
# A new group is starting...
$value = trim( substr( $value, 1 ) );
# Do not use the value for 'other' as option group - T251351
$optgroup = isset( $params['other'] ) && $params['other'] === $value
? false
: $value;
if ( $value !== '' &&
// Do not use the value for 'other' as option group - T251351
( !isset( $params['other'] ) || $value !== $params['other'] )
) {
$optgroup = $value;
} else {
$optgroup = false;
}
} elseif ( substr( $value, 0, 2 ) == '**' ) {
# groupmember
$opt = trim( substr( $value, 2 ) );

View file

@ -375,6 +375,7 @@ class XmlTest extends MediaWikiIntegrationTestCase {
$this->assertEquals(
[
'other reasons' => 'other',
'Empty group item' => 'Empty group item',
'Foo' => [
'Foo 1' => 'Foo 1',
'Example' => 'Example',
@ -384,7 +385,7 @@ class XmlTest extends MediaWikiIntegrationTestCase {
],
],
Xml::listDropDownOptions(
"* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
"*\n** Empty group item\n* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
[ 'other' => 'other reasons' ]
)
);