From 4da2778a290eb3fe9f709085ddab84346c96c60a Mon Sep 17 00:00:00 2001 From: "Moritz Schubotz (physikerwelt)" Date: Sat, 1 Jun 2024 16:30:18 +0200 Subject: [PATCH] LaTeXML: Remove SVG fallback images Removes Fallback images for LaTeXML rendering Change-Id: Id158de53b3f68f85fe55a3f7ace9abdc55c21160 --- src/MathLaTeXML.php | 38 +------------------ src/MathMathML.php | 26 ++++++++++--- src/MathNativeMML.php | 2 +- src/MathRenderer.php | 3 +- src/MathSource.php | 3 +- tests/phpunit/MathMathMLTest.php | 8 ++++ .../integration/ParserIntegrationTests.php | 2 +- 7 files changed, 36 insertions(+), 46 deletions(-) diff --git a/src/MathLaTeXML.php b/src/MathLaTeXML.php index 2ee26ac34..252912ef2 100644 --- a/src/MathLaTeXML.php +++ b/src/MathLaTeXML.php @@ -148,42 +148,8 @@ class MathLaTeXML extends MathMathML { } } - /** - * Calculates the SVG image based on the MathML input - * No cache is used. - * @return bool - */ - public function calculateSvg() { - $renderer = new MathMathML( $this->getTex() ); - $renderer->setMathml( $this->getMathml() ); - $renderer->setMode( MathConfig::MODE_LATEXML ); - $renderer->setPurge(); - $res = $renderer->render(); - if ( $res == true ) { - $this->setSvg( $renderer->getSvg() ); - } else { - $lastError = $renderer->getLastError(); - LoggerFactory::getInstance( 'Math' )->error( - 'Failed to convert LaTeXML-MathML to SVG:' . $lastError ); - } - return $res; - } - - /** - * Gets the SVG image - * - * @param string $render if set to 'render' (default) and no SVG image exists, the function - * tries to generate it on the fly. - * Otherwise, if set to 'cached', and there is no SVG in the database - * cache, an empty string is returned. - * - * @return string XML-Document of the rendered SVG - */ - public function getSvg( $render = 'render' ) { - if ( $render == 'render' && ( $this->isPurge() || $this->svg == '' ) ) { - $this->calculateSvg(); - } - return parent::getSvg( $render ); + public function getHtmlOutput( bool $svg = true ): string { + return parent::getHtmlOutput( false ); } /** diff --git a/src/MathMathML.php b/src/MathMathML.php index ada1a04bf..a90e3212c 100644 --- a/src/MathMathML.php +++ b/src/MathMathML.php @@ -450,15 +450,18 @@ class MathMathML extends MathRenderer { } /** + * @param bool $svg * @return string Html output that is embedded in the page */ - public function getHtmlOutput() { + public function getHtmlOutput( bool $svg = true ): string { $config = MediaWikiServices::getInstance()->getMainConfig(); $enableLinks = $config->get( "MathEnableFormulaLinks" ); - if ( $this->getMathStyle() == 'display' ) { + if ( $this->getMathStyle() === 'display' ) { $element = 'div'; + $mml_class = 'mwe-math-mathml-display'; } else { $element = 'span'; + $mml_class = 'mwe-math-mathml-inline'; } $attribs = [ 'class' => 'mwe-math-element' ]; if ( $this->getID() !== '' ) { @@ -480,10 +483,21 @@ class MathMathML extends MathRenderer { if ( $this->getMathStyle() == 'display' ) { $mml = preg_replace( '/ $this->getClassName(), 'style' => 'display: none;' - ], $mml ); - $output .= $this->getFallbackImage(); + + if ( $svg ) { + $mml_attribs = [ + 'class' => $this->getClassName(), + 'style' => 'display: none;' + ]; + } else { + $mml_attribs = [ + 'class' => $mml_class, + ]; + } + $output .= Html::rawElement( $element, $mml_attribs, $mml ); + if ( $svg ) { + $output .= $this->getFallbackImage(); + } if ( $hyperlink && $enableLinks ) { $output = Html::rawElement( 'a', diff --git a/src/MathNativeMML.php b/src/MathNativeMML.php index ae911dee4..f5e14bf8c 100644 --- a/src/MathNativeMML.php +++ b/src/MathNativeMML.php @@ -60,7 +60,7 @@ class MathNativeMML extends MathMathML { /** * @inheritDoc */ - public function getHtmlOutput(): string { + public function getHtmlOutput( bool $svg = true ): string { return $this->getMathml(); } diff --git a/src/MathRenderer.php b/src/MathRenderer.php index 071a85755..0225687d3 100644 --- a/src/MathRenderer.php +++ b/src/MathRenderer.php @@ -143,9 +143,10 @@ abstract class MathRenderer { abstract public function render(); /** + * @param bool $svg * @return string Html output that is embedded in the page */ - abstract public function getHtmlOutput(); + abstract public function getHtmlOutput( bool $svg = true ): string; /** * texvc error messages diff --git a/src/MathSource.php b/src/MathSource.php index b8371742a..bbc3b5630 100644 --- a/src/MathSource.php +++ b/src/MathSource.php @@ -36,9 +36,10 @@ class MathSource extends MathRenderer { /** * Renders TeX by outputting it to the browser in a span tag * + * @param bool $svg * @return string span tag with TeX */ - public function getHtmlOutput() { + public function getHtmlOutput( bool $svg = true ): string { # No need to render or parse anything more! # New lines are replaced with spaces, which avoids confusing our parser (bugs 23190, 22818) if ( $this->getMathStyle() == 'display' ) { diff --git a/tests/phpunit/MathMathMLTest.php b/tests/phpunit/MathMathMLTest.php index 215e26e60..63ec3abae 100644 --- a/tests/phpunit/MathMathMLTest.php +++ b/tests/phpunit/MathMathMLTest.php @@ -200,6 +200,14 @@ class MathMathMLTest extends MediaWikiIntegrationTestCase { $this->assertStringNotContainsString( "data-qid", $out ); } + public function testGetHtmlOutputNoSvg() { + $math = new MathMathML( "a+b" ); + $out = $math->getHtmlOutput( false ); + $this->assertStringNotContainsString( "assertStringNotContainsString( "mwe-math-mathml-a11y", $out ); + $this->assertStringContainsString( "mwe-math-mathml-", $out ); + } + public function testEmpty() { // TODO: Once render returns status, we won't need TestingAccessWrapper anymore. $math = TestingAccessWrapper::newFromObject( new MathMathML( '' ) ); diff --git a/tests/phpunit/integration/ParserIntegrationTests.php b/tests/phpunit/integration/ParserIntegrationTests.php index 572f05114..cac4b20ce 100644 --- a/tests/phpunit/integration/ParserIntegrationTests.php +++ b/tests/phpunit/integration/ParserIntegrationTests.php @@ -49,7 +49,7 @@ class ParserIntegrationTests extends MediaWikiIntegrationTestCase { return true; } - public function getHtmlOutput() { + public function getHtmlOutput( bool $svg = true ): string { return "$this->mode:$this->tex"; }