Physikerwelt has uploaded a new change for review. https://gerrit.wikimedia.org/r/111171
Change subject: Fix: Allow <div /> as valid Math output ...................................................................... Fix: Allow <div /> as valid Math output Some simple math tags such as the rendering result of $\mathcal{ABC}$ are converted to an HTML-div tag by LaTeXML if inline-mode is used. This change * makes the $wgMathDefaultLaTeXMLSetting easier to read * improves the XML validation of the LaTeXML output. Change-Id: I92cf5189bfce6c05e7e794155b4e412545db3723 --- M Math.php M MathLaTeXML.php M tests/MathLaTeXMLTest.php 3 files changed, 90 insertions(+), 12 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Math refs/changes/71/111171/1 diff --git a/Math.php b/Math.php index 6ed0d6c..5f42ff7 100644 --- a/Math.php +++ b/Math.php @@ -130,7 +130,26 @@ * Setting for the LaTeXML renderer. * See http://dlmf.nist.gov/LaTeXML/manual/commands/latexmlpost.xhtml for details. */ -$wgMathDefaultLaTeXMLSetting = 'format=xhtml&whatsin=math&whatsout=math&pmml&cmml&nodefaultresources&preload=LaTeX.pool&preload=article.cls&preload=amsmath.sty&preload=amsthm.sty&preload=amstext.sty&preload=amssymb.sty&preload=eucal.sty&preload=[dvipsnames]xcolor.sty&preload=url.sty&preload=hyperref.sty&preload=[ids]latexml.sty&preload=texvc'; +$wgMathDefaultLaTeXMLSetting = array( 'format' => 'xhtml', + 'whatsin' => 'math', + 'whatsout' => 'math', + 'pmml', + 'cmml', + 'nodefaultresources', + 'preload' => array( 'LaTeX.pool', + 'article.cls', + 'amsmath.sty', + 'amsthm.sty', + 'amstext.sty', + 'amssymb.sty', + 'eucal.sty', + '[dvipsnames]xcolor.sty', + 'url.sty', + 'hyperref.sty', + '[ids]latexml.sty', + 'texvc' ), + ); + /** * The link to the texvc executable */ diff --git a/MathLaTeXML.php b/MathLaTeXML.php index f3dce52..b65ebd6 100644 --- a/MathLaTeXML.php +++ b/MathLaTeXML.php @@ -15,6 +15,10 @@ * @var String settings for LaTeXML daemon */ private $LaTeXMLSettings = ''; + /** @var boolean if false LaTeXML output is not validated*/ + private $XMLValidation = true; + protected static $DEFAULT_ALLOWED_ROOT_ELEMENTS = array( 'math', 'div', 'table', 'query' ); + protected $allowedRootElements = ''; /** * Converts an array with LaTeXML settings to a URL encoded String. @@ -30,7 +34,10 @@ //removes the [1] [2]... for the unnamed subarrays since LaTeXML //assigns multiple values to one key e.g. //preload=amsmath.sty&preload=amsthm.sty&preload=amstext.sty - return preg_replace('|\%5B\d+\%5D|', '', wfArrayToCgi($array)) ; + $cgi_string = wfArrayToCgi( $array ); + $cgi_string = preg_replace( '|\%5B\d+\%5D|', '', $cgi_string ); + $cgi_string = preg_replace( '|&\d+=|', '&', $cgi_string ); + return $cgi_string; } } /** @@ -57,6 +64,28 @@ */ public function setLaTeXMLSettings( $settings ) { $this->LaTeXMLSettings = $settings; + } + + /** + * Gets the allowed root elements the rendered math tag might have. + * + * @return array + */ + public function getAllowedRootElements() { + if ( $this->allowedRootElements ) { + return $this->allowedRootElements; + } else { + return self::$DEFAULT_ALLOWED_ROOT_ELEMENTS; + } + } + + /** + * Sets the allowed root elements the rendered math tag might have. + * An empty value indicates to use the default settings. + * @param array $settings + */ + public function setAllowedRootElments( $settings ) { + $this->allowedRootElements = $settings; } /* (non-PHPdoc) @@ -90,7 +119,7 @@ } else { $dbres = $this->readFromDatabase(); if ( $dbres ) { - if ( self::isValidMathML( $this->getMathml() ) ) { + if ( $this->isValidMathML( $this->getMathml() ) ) { wfDebugLog( "Math", "Valid entry found in database." ); return false; } else { @@ -199,7 +228,7 @@ if ( $this->makeRequest( $host, $post, $res, $this->lastError ) ) { $result = json_decode( $res ); if ( json_last_error() === JSON_ERROR_NONE ) { - if ( self::isValidMathML( $result->result ) ) { + if ( $this->isValidMathML( $result->result ) ) { $this->setMathml( $result->result ); wfProfileOut( __METHOD__ ); return true; @@ -229,30 +258,43 @@ } /** + * Sets the XML validaton. + * If set to false the output of LaTeXML is not validated. + * @param boolean $newval + */ + public function setXMLValidaton( $newval = true ) { + $this->XMLValidation = $newval; + } + + /** * Checks if the input is valid MathML, * and if the root element has the name math * @param string $XML * @return boolean */ - static public function isValidMathML( $XML ) { + public function isValidMathML( $XML ) { + $out = false; + if ( !$this->XMLValidation ) { + return true; + } $out = false; // depends on https://gerrit.wikimedia.org/r/#/c/66365/ - if ( ! is_callable( 'XmlTypeCheck::newFromString' ) ) { + if ( !is_callable( 'XmlTypeCheck::newFromString' ) ) { $msg = wfMessage( 'math_latexml_xmlversion' )->inContentLanguage()->escaped(); trigger_error( $msg, E_USER_NOTICE ); wfDebugLog( 'Math', $msg ); return true; } $xmlObject = new XmlTypeCheck( $XML, null, false ); - if ( ! $xmlObject->wellFormed ) { + if ( !$xmlObject->wellFormed ) { wfDebugLog( "Math", "XML validation error:\n " . var_export( $XML, true ) . "\n" ); } else { $name = $xmlObject->getRootElement(); - $name = str_replace( 'http://www.w3.org/1998/Math/MathML:', '', $name ); - if ( $name == "math" or $name == "table" or $name == "div" ) { + $elementSplit = explode( ':', $name ); + if ( in_array( end( $elementSplit ), $this->getAllowedRootElements() ) ) { $out = true; } else { - wfDebugLog( "Math", "got wrong root element " . $name ); + wfDebugLog( "Math", 'got wrong root element :' . end( $elementSplit ) . ' with namespace ' . $name ); } } return $out; diff --git a/tests/MathLaTeXMLTest.php b/tests/MathLaTeXMLTest.php index 4c103c7..754f7e0 100644 --- a/tests/MathLaTeXMLTest.php +++ b/tests/MathLaTeXMLTest.php @@ -97,8 +97,12 @@ public function testisValidXML() { $validSample = '<math>content</math>'; $invalidSample = '<notmath />'; - $this->assertTrue( MathLaTeXML::isValidMathML( $validSample ), 'test if math expression is valid mathml sample' ); - $this->assertFalse( MathLaTeXML::isValidMathML( $invalidSample ), 'test if math expression is invalid mathml sample' ); + $renderer = $this->getMockBuilder( 'MathLaTeXML' ) + ->setMethods( NULL ) + ->disableOriginalConstructor() + ->getMock(); + $this->assertTrue( $renderer->isValidMathML( $validSample ), 'test if math expression is valid mathml sample' ); + $this->assertFalse( $renderer->isValidMathML( $invalidSample ), 'test if math expression is invalid mathml sample' ); } /** @@ -133,6 +137,19 @@ $this->assertEquals( $expected, $real , "Rendering of a+b in plain Text mode" ); } + /** + * Checks the basic functionallity + * i.e. if the span element is generated right. + */ + public function testMathCalSample() { + global $wgMathLaTeXMLTimeout; + $wgMathLaTeXMLTimeout = 20; + $renderer = MathRenderer::getRenderer( "\mathcal{ABC}", array(), MW_MATH_LATEXML ); + $real = $renderer->render( true ); + $expected = '<span class="tex" dir="ltr" id=".5Cmathcal.7BABC.7D"><div xmlns="http://www.w3.org/1999/xhtml" class="ltx_document"> <div id="p1" class="ltx_para"> <p id="p1.1" class="ltx_p"><span id="p1.1.1" class="ltx_text ltx_font_caligraphic">ABC</span></p> </div> </div></span>'; + $this->assertEquals( $expected, $real + , "Rendering of a+b in plain Text mode" ); + } } /** -- To view, visit https://gerrit.wikimedia.org/r/111171 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I92cf5189bfce6c05e7e794155b4e412545db3723 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Math Gerrit-Branch: master Gerrit-Owner: Physikerwelt <w...@physikerwelt.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits