Streamline HTML rendering code for format messages

The idea is to make it a little easier to follow what's going on
here.

This also improves an error message when tests fail.

Change-Id: If35be8aefab5a1568d53a9ecdc4313a66f71317b
This commit is contained in:
Thiemo Kreuz 2022-04-06 18:40:50 +02:00 committed by Umherirrender
parent 71392dc6d4
commit e297e767f0
2 changed files with 23 additions and 17 deletions

View file

@ -31,17 +31,27 @@ class TemplateDataHtmlFormatter {
public function getHtml( TemplateDataBlob $templateData ): string {
$data = $templateData->getDataInLanguage( $this->languageCode );
if ( is_string( $data->format ) && isset( TemplateDataValidator::PREDEFINED_FORMATS[$data->format] ) ) {
// The following icon names are used here:
// * template-format-block
// * template-format-inline
// @phan-suppress-next-line PhanTypeSuspiciousStringExpression
$icon = 'template-format-' . $data->format;
$formatMsg = $data->format;
} else {
$icon = 'settings';
$formatMsg = $data->format ? 'custom' : null;
$icon = null;
$formatMsg = null;
if ( isset( $data->format ) && is_string( $data->format ) ) {
$format = $data->format;
'@phan-var string $format';
if ( isset( TemplateDataValidator::PREDEFINED_FORMATS[$format] ) ) {
// The following icon names are used here:
// * template-format-block
// * template-format-inline
$icon = 'template-format-' . $format;
// Messages that can be used here:
// * templatedata-doc-format-block
// * templatedata-doc-format-inline
$formatMsg = $this->localizer->msg( 'templatedata-doc-format-' . $format );
}
if ( !$formatMsg || $formatMsg->isDisabled() ) {
$icon = 'settings';
$formatMsg = $this->localizer->msg( 'templatedata-doc-format-custom' );
}
}
$sorting = count( (array)$data->params ) > 1 ? " sortable" : "";
$html = '<header>'
. Html::element( 'p',
@ -60,17 +70,13 @@ class TemplateDataHtmlFormatter {
Html::element( 'p', [],
$this->localizer->msg( 'templatedata-doc-params' )->text()
)
. ( $formatMsg !== null ?
. ( $formatMsg ?
Html::rawElement( 'p', [],
new \OOUI\IconWidget( [ 'icon' => $icon ] )
. Html::element(
'span',
[ 'class' => 'mw-templatedata-format' ],
// Messages that can be used here:
// * templatedata-doc-format-block
// * templatedata-doc-format-custom
// * templatedata-doc-format-inline
$this->localizer->msg( 'templatedata-doc-format-' . $formatMsg )->text()
$formatMsg->text()
)
) :
''

View file

@ -753,7 +753,7 @@ class TemplateDataBlobTest extends MediaWikiIntegrationTestCase {
$this->assertSame(
$case['status'],
is_string( $case['status'] ) ? $this->getStatusText( $status ) : $status->isGood(),
'Status: ' . $case['msg']
$case['msg'] . ' (status "' . $this->getStatusText( $status ) . '")'
);
if ( !isset( $case['output'] ) ) {