From 1f87afa8e680918fcfaae990b52fd862800d67f6 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Fri, 13 Apr 2018 15:50:34 +0200 Subject: [PATCH] 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 --- MathFormatter.php | 16 +++------------- tests/MathFormatterTest.php | 10 +++++----- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/MathFormatter.php b/MathFormatter.php index c21a1fc..966faa0 100644 --- a/MathFormatter.php +++ b/MathFormatter.php @@ -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 "$tex"; + + // 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; } } diff --git a/tests/MathFormatterTest.php b/tests/MathFormatterTest.php index fec365d..a435a70 100644 --- a/tests/MathFormatterTest.php +++ b/tests/MathFormatterTest.php @@ -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( '', $resultFormat ); } public function testFormatPlain() {