From c390f6479a4aafd3c168757e53b8109284a5a4fc Mon Sep 17 00:00:00 2001 From: physikerwelt Date: Thu, 5 Jun 2014 21:06:43 +0000 Subject: [PATCH] Add getHtmlOutput method Currently the method render always returns a html string that can be a string that represents the correct result or a rendered error message. This change adds a mechanism that allows to fetch the HTML output. In a followup commit the rendering function is changed to return a boolean value rather than the rendering result. This will simplify the error handling and caching. Change-Id: I80760493e391911c41eb69d75a93c6a34db8852e --- MathMathML.php | 51 +++++++++++++++++++++++++++++++++++++- MathRenderer.php | 4 +++ MathSource.php | 10 +++++++- MathTexvc.php | 4 +-- modules/ext.math.css | 10 +++++++- tests/MathLaTeXMLTest.php | 5 ++-- tests/MathRendererTest.php | 6 ++--- tests/MathTexvcTest.php | 23 ++++++----------- 8 files changed, 88 insertions(+), 25 deletions(-) diff --git a/MathMathML.php b/MathMathML.php index 85012a3..84cef4a 100644 --- a/MathMathML.php +++ b/MathMathML.php @@ -56,7 +56,7 @@ class MathMathML extends MathRenderer { $this->setPurge( true ); } if ( $this->renderingRequired() ) { - $res = $this->doRender( ); + $res = $this->doRender(); if ( ! $res ) { wfProfileOut( __METHOD__ ); return $this->getLastError(); @@ -208,4 +208,53 @@ class MathMathML extends MathRenderer { protected function getMathTableName() { return 'mathoid'; } + /** + * Calculates the default class name for a math element + * @param boolean $fallback + * @param boolean $png + * @return string the class name + */ + protected function getClassName( $fallback = false, $png = false ) { + $class = "mwe-math-"; + if ( $fallback ) { + $class .= 'fallback-'; + if ( $png ) { + $class .= 'png-'; + } else { + $class .= 'svg-'; + } + } else { + $class .= 'mathml-'; + } + if ( $this->getMathStyle() == MW_MATHSTYLE_DISPLAY ) { + $class .= 'display'; + } else { + $class .= 'inline'; + } + return $class; + } + /** + * @return string Html output that is embedded in the page + */ + public function getHtmlOutput() { + if ( $this->getMathStyle() == MW_MATHSTYLE_DISPLAY ) { + $element = 'div'; + } else { + $element = 'span'; + } + $attribs = array(); + $output = HTML::openElement( $element, $attribs ); + // MathML has to be wrapped into a div or span in order to be able to hide it. + if ( $this->getMathStyle() == MW_MATHSTYLE_DISPLAY ) { + // Remove displayStyle attributes set by the MathML converter + $mml = preg_replace( '/(]*)(display|mode)=["\'](inline|block)["\']/', '$1', $this->getMathml() ); + // and insert the correct value + $mml = preg_replace( '/getMathml(); + } + $output .= Xml::tags( $element, array( 'class' => $this->getClassName(), 'style' => 'display: none;' ), $mml ); + $output .= HTML::closeElement( $element ); + return $output; + } } \ No newline at end of file diff --git a/MathRenderer.php b/MathRenderer.php index 4ac900e..05c48e3 100644 --- a/MathRenderer.php +++ b/MathRenderer.php @@ -149,6 +149,10 @@ abstract class MathRenderer { */ abstract public function render(); + /** + * @return string Html output that is embedded in the page + */ + abstract public function getHtmlOutput(); /** * texvc error messages diff --git a/MathSource.php b/MathSource.php index 6b395e5..890b785 100644 --- a/MathSource.php +++ b/MathSource.php @@ -24,7 +24,7 @@ class MathSource extends MathRenderer { * * @return string span tag with TeX */ - function render() { + function getHtmlOutput() { # 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() == MW_MATHSTYLE_DISPLAY ) { @@ -49,4 +49,12 @@ class MathSource extends MathRenderer { protected function getMathTableName() { throw new MWException ( 'in math source mode no database caching should happen'); } + + /** + * No rendering required in plain text mode + * @return boolean + */ + function render() { + return $this->getHtmlOutput(); + } } diff --git a/MathTexvc.php b/MathTexvc.php index 058dc31..4a3ecc1 100644 --- a/MathTexvc.php +++ b/MathTexvc.php @@ -86,7 +86,7 @@ class MathTexvc extends MathRenderer { return $result; } } - return $this->doHTMLRender(); + return $this->getHtmlOutput(); } /** @@ -334,7 +334,7 @@ class MathTexvc extends MathRenderer { * * @return string HTML string */ - public function doHTMLRender() { + public function getHtmlOutput() { if ( $this->getMode() == MW_MATH_MATHML && $this->getMathml() != '' ) { return Xml::tags( 'math', $this->getAttributes( 'math', diff --git a/modules/ext.math.css b/modules/ext.math.css index 1160710..48bccb8 100644 --- a/modules/ext.math.css +++ b/modules/ext.math.css @@ -6,7 +6,15 @@ Shows browser-dependent math output. */ +.mwe-math-mathml-inline { display: none;} +.mwe-math-mathml-display { display: none ;} .mwe-math-fallback-png-inline { display: inline; vertical-align: middle} .mwe-math-fallback-png-display { display: block; margin-left: auto; margin-right: auto;} .mwe-math-fallback-source-inline { display: inline; vertical-align: middle} -.mwe-math-fallback-source-display { display: block; margin-left: auto; margin-right: auto;} \ No newline at end of file +.mwe-math-fallback-source-display { display: block; margin-left: auto; margin-right: auto;} + + +@-moz-document url-prefix() { + .mwe-math-mathml-inline { display: inline !important; } + .mwe-math-mathml-display { display: block !important; margin:auto!important;} +} \ No newline at end of file diff --git a/tests/MathLaTeXMLTest.php b/tests/MathLaTeXMLTest.php index 4fb116d..edd69dc 100644 --- a/tests/MathLaTeXMLTest.php +++ b/tests/MathLaTeXMLTest.php @@ -42,8 +42,9 @@ class MathLaTeXMLTest extends MediaWikiTestCase { $this->setMwGlobals( 'wgMathLaTeXMLTimeout', 20 ); $this->setMwGlobals( 'wgMathValidModes', array( MW_MATH_LATEXML ) ); $renderer = MathRenderer::getRenderer( "a+b", array(), MW_MATH_LATEXML ); - $real = $renderer->render( true ); - $expected = ' a + b a b {\displaystyle a+b} '; + $renderer->render( true ); + $expected = ''; + $real = preg_replace( "/\n\s*/", '', $renderer->getHtmlOutput() ); $this->assertEquals( $expected, $real , "Rendering of a+b in plain Text mode." . $renderer->getLastError() ); diff --git a/tests/MathRendererTest.php b/tests/MathRendererTest.php index cdc316e..5110a1e 100644 --- a/tests/MathRendererTest.php +++ b/tests/MathRendererTest.php @@ -27,7 +27,7 @@ class MathRendererTest extends MediaWikiTestCase { */ public function testWriteCacheSkip() { $renderer = $this->getMockBuilder( 'MathRenderer' ) - ->setMethods( array( 'writeToDatabase' , 'render', 'getMathTableName' ) ) + ->setMethods( array( 'writeToDatabase' , 'render', 'getMathTableName', 'getHtmlOutput' ) ) ->disableOriginalConstructor() ->getMock(); $renderer->expects( $this->never() ) @@ -41,7 +41,7 @@ class MathRendererTest extends MediaWikiTestCase { */ public function testWriteCache() { $renderer = $this->getMockBuilder( 'MathRenderer' ) - ->setMethods( array( 'writeToDatabase' , 'render', 'getMathTableName' ) ) + ->setMethods( array( 'writeToDatabase' , 'render', 'getMathTableName', 'getHtmlOutput' ) ) ->disableOriginalConstructor() ->getMock(); $renderer->expects( $this->never() ) @@ -51,7 +51,7 @@ class MathRendererTest extends MediaWikiTestCase { public function testSetPurge() { $renderer = $this->getMockBuilder( 'MathRenderer' ) - ->setMethods( array( 'render', 'getMathTableName' ) ) + ->setMethods( array( 'render', 'getMathTableName', 'getHtmlOutput' ) ) ->disableOriginalConstructor() ->getMock(); $renderer->setPurge(); diff --git a/tests/MathTexvcTest.php b/tests/MathTexvcTest.php index c1ec878..c2202fc 100644 --- a/tests/MathTexvcTest.php +++ b/tests/MathTexvcTest.php @@ -27,17 +27,13 @@ class MathTexvcTest extends MediaWikiTestCase { // Create a MathTexvc mock, replacing methods 'readFromDatabase', // 'callTexvc', and 'doHTMLRender' with test doubles. - $texvc = $this->getMockBuilder( 'MathTexvc' ) - ->setMethods( array( 'readFromDatabase', 'callTexvc', 'doHTMLRender' ) ) - ->disableOriginalConstructor() + $texvc = $this->getMockBuilder( 'MathTexvc' )->setMethods( array( 'readFromDatabase', 'callTexvc', 'getHtmlOutput' ) )->disableOriginalConstructor() ->getMock(); // When we call render() below, MathTexvc will ... // ... first check if the item exists in the database cache: - $texvc->expects( $this->once() ) - ->method( 'readFromDatabase' ) - ->with() + $texvc->expects( $this->once() )->method( 'readFromDatabase' )->with() ->will( $this->returnValue( true ) ); // ... if cache lookup succeeded, it won't shell out to texvc: @@ -45,8 +41,7 @@ class MathTexvcTest extends MediaWikiTestCase { ->method( 'callTexvc' ); // ... instead, MathTexvc will skip to HTML generation: - $texvc->expects( $this->once() ) - ->method( 'doHTMLRender' ); + $texvc->expects( $this->once() )->method( 'getHtmlOutput' ); $texvc->render(); } @@ -60,7 +55,7 @@ class MathTexvcTest extends MediaWikiTestCase { */ function testRenderCacheMiss() { $texvc = $this->getMockBuilder( 'MathTexvc' ) - ->setMethods( array( 'readCache', 'callTexvc', 'doHTMLRender' ) ) + ->setMethods( array( 'readCache', 'callTexvc', 'getHtmlOutput' ) ) ->disableOriginalConstructor() ->getMock(); @@ -77,8 +72,7 @@ class MathTexvcTest extends MediaWikiTestCase { ->will( $this->returnValue( MathTexvc::MW_TEXVC_SUCCESS ) ); // ... if texvc succeeds, MathTexvc will generate HTML: - $texvc->expects( $this->once() ) - ->method( 'doHTMLRender' ); + $texvc->expects( $this->once() )->method( 'getHtmlOutput' ); $texvc->render(); } @@ -91,7 +85,7 @@ class MathTexvcTest extends MediaWikiTestCase { */ function testRenderTexvcFailure() { $texvc = $this->getMockBuilder( 'MathTexvc' ) - ->setMethods( array( 'readCache', 'callTexvc', 'doHTMLRender' ) ) + ->setMethods( array( 'readCache', 'callTexvc', 'getHtmlOutput' ) ) ->disableOriginalConstructor() ->getMock(); @@ -104,12 +98,11 @@ class MathTexvcTest extends MediaWikiTestCase { // ... on cache miss, shell out to texvc: $texvc->expects( $this->once() ) - ->method( 'callTexvc' ) - ->will( $this->returnValue( 'error' ) ); + ->method( 'callTexvc' )->will( $this->returnValue( 'error' ) ); // ... if texvc fails, render() will not generate HTML: $texvc->expects( $this->never() ) - ->method( 'doHTMLRender' ); + ->method( 'getHtmlOutput' ); // ... it will return the error result instead: $this->assertEquals( $texvc->render(), 'error' );