Relax support for error/warning message keys with dashes

This is a direct follow up for I6f05842 where we already started
supporting dashes, but converted them to underscores. The only change
in this patch is that the CSS class will use the message key as it
is, without the dashes being converted to underscores. I added a test
case specifically for this.

Bug: T352676
Change-Id: Ic22e897c27b8371e3b1ed63932b619e7af71bd5c
This commit is contained in:
thiemowmde 2023-12-04 17:51:38 +01:00 committed by Thiemo Kreuz (WMDE)
parent 70a19357f3
commit fb8494b4dd
2 changed files with 12 additions and 1 deletions

View file

@ -2,6 +2,7 @@
namespace Cite;
use InvalidArgumentException;
use Language;
use MediaWiki\Html\Html;
use MediaWiki\Message\Message;
@ -119,7 +120,10 @@ class ErrorReporter {
* @return string[] Two elements, e.g. "error" and "ref_numeric_key"
*/
private function parseTypeAndIdFromMessageKey( string $messageKey ): array {
return array_slice( explode( '_', str_replace( '-', '_', $messageKey ), 3 ), 1 );
if ( !preg_match( '/^cite.(error|warning).(.+)/i', $messageKey, $matches ) ) {
throw new InvalidArgumentException( 'Unexpected message key' );
}
return array_slice( $matches, 1 );
}
}

View file

@ -66,6 +66,13 @@ class ErrorReporterTest extends \MediaWikiUnitTestCase {
'dir="rtl">(cite_warning|(cite_warning_example|first param))</span>',
'expectedCategory' => null,
],
'Optional support for messages with dashes' => [
'key' => 'cite-warning-with-dashes',
'expectedHtml' => '<span class="warning mw-ext-cite-warning ' .
'mw-ext-cite-warning-with-dashes" lang="qqx" dir="rtl">' .
'(cite_warning|(cite-warning-with-dashes|first param))</span>',
'expectedCategory' => null,
],
];
}