Fix MathFormatter failing on new SnakFormatter format MIME types

A formatter like this is not supposed to check if the format is known
or not. The code calling these formatters can introduce new (sub) formats
any time. What a formatter like this should do then are two things:
* If it's some HTML format, always return HTML.
* If you really can't identify the format, do a sensible fallback.

This currently blocks introducing a new format in Wikibase.

Bug: T46727
Change-Id: I585069e8f2ba33ec657ca4d514c6d502fe0f5ba3
This commit is contained in:
Thiemo Kreuz 2018-04-13 15:50:34 +02:00 committed by Hoo man
parent 82e4283f02
commit 1f87afa8e6
2 changed files with 8 additions and 18 deletions

View File

@ -25,20 +25,9 @@ class MathFormatter implements ValueFormatter {
* Loads format to distinguish the type of formatting
*
* @param string $format One of the SnakFormatter::FORMAT_... constants.
*
* @throws InvalidArgumentException
*/
public function __construct( $format ) {
switch ( $format ) {
case SnakFormatter::FORMAT_PLAIN:
case SnakFormatter::FORMAT_WIKI:
case SnakFormatter::FORMAT_HTML:
case SnakFormatter::FORMAT_HTML_DIFF:
$this->format = $format;
break;
default:
throw new InvalidArgumentException( 'Unsupported output format: ' . $format );
}
$this->format = $format;
}
/**
@ -59,6 +48,8 @@ class MathFormatter implements ValueFormatter {
return $tex;
case SnakFormatter::FORMAT_WIKI:
return "<math>$tex</math>";
// Intentionally fall back to MathML output in all other, possibly unknown cases.
default:
$renderer = new MathMathML( $tex );
@ -72,7 +63,6 @@ class MathFormatter implements ValueFormatter {
$html = $this->formatDetails( $html, $tex );
}
// TeX string is not valid or rendering failed
return $html;
}
}

View File

@ -58,11 +58,11 @@ class MathFormatterTest extends MediaWikiTestCase {
$formatter->format( null );
}
/**
* @expectedException InvalidArgumentException
*/
public function testUnknownFormat() {
new MathFormatter( 'unknown/unknown' );
public function testUnknownFormatFallsBackToMathMl() {
$formatter = new MathFormatter( 'unknown/unknown' );
$value = new StringValue( self::SOME_TEX );
$resultFormat = $formatter->format( $value );
$this->assertContains( '</math>', $resultFormat );
}
public function testFormatPlain() {