Remove images from native MathML

In native MathML we only display MathML, also get
rid of enclosing <div / <span container.

Bug: T182041
Change-Id: I26bc4ea88d360d53cc7a34a89bef78812b84b2ea
This commit is contained in:
Moritz Schubotz (physikerwelt) 2023-03-30 20:24:25 +02:00
parent 04dfc54493
commit 8c6eeae82e
No known key found for this signature in database
GPG key ID: F803DB146DDF36C3
3 changed files with 53 additions and 3 deletions

View file

@ -10,7 +10,10 @@ namespace MediaWiki\Extension\Math;
use MediaWiki\Extension\Math\InputCheck\LocalChecker; use MediaWiki\Extension\Math\InputCheck\LocalChecker;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmath; use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmath;
use MediaWiki\MediaWikiServices;
use SpecialPage;
use StatusValue; use StatusValue;
use Title;
/** /**
* Converts LaTeX to MathML using PHP * Converts LaTeX to MathML using PHP
@ -26,7 +29,22 @@ class MathNativeMML extends MathMathML {
protected function doRender(): StatusValue { protected function doRender(): StatusValue {
$presentation = $this->getChecker()->getParseTree()->renderMML(); $presentation = $this->getChecker()->getParseTree()->renderMML();
$root = new MMLmath(); $config = MediaWikiServices::getInstance()->getMainConfig();
$attributes = [ 'class' => 'mwe-math-element' ];
if ( $this->getID() !== '' ) {
$attributes['id'] = $this->getID();
}
if ( $config->get( 'MathEnableFormulaLinks' ) &&
isset( $this->params['qid'] ) &&
preg_match( '/Q\d+/', $this->params['qid'] ) ) {
$titleObj = Title::newFromLinkTarget( SpecialPage::getTitleValueFor( 'MathWikibase' ) );
$attributes['href'] = $titleObj->getLocalURL( [ 'qid' => $this->params['qid'] ] );
}
if ( $this->getMathStyle() == 'display' ) {
$attributes['display'] = 'block';
}
$root = new MMLmath( "", $attributes );
$this->setMathml( $root->encapsulateRaw( $presentation ) ); $this->setMathml( $root->encapsulateRaw( $presentation ) );
return StatusValue::newGood(); return StatusValue::newGood();
} }
@ -37,4 +55,11 @@ class MathNativeMML extends MathMathML {
return $this->checker; return $this->checker;
} }
/**
* @inheritDoc
*/
public function getHtmlOutput(): string {
return $this->getMathml();
}
} }

View file

@ -4,8 +4,8 @@ namespace MediaWiki\Extension\Math\TexVC\MMLnodes;
class MMLmath extends MMLbase { class MMLmath extends MMLbase {
public function __construct( string $texclass = "" ) { public function __construct( string $texclass = "", $attributes = [] ) {
$attributes = [ "xmlns" => "http://www.w3.org/1998/Math/MathML" ]; $attributes["xmlns"] = "http://www.w3.org/1998/Math/MathML";
parent::__construct( "math", $texclass, $attributes ); parent::__construct( "math", $texclass, $attributes );
} }
} }

View file

@ -26,4 +26,29 @@ class MathNativeMMLTest extends MediaWikiIntegrationTestCase {
$this->assertStringContainsString( 'sin', $mml->getMathml() ); $this->assertStringContainsString( 'sin', $mml->getMathml() );
} }
public function testNoLink() {
$this->setMwGlobals( 'wgMathEnableFormulaLinks', false );
$mml = new MathNativeMML( '\sin', [ 'qid' => 'Q1' ] );
$this->assertTrue( $mml->render() );
$this->assertStringNotContainsString( 'href', $mml->getMathml() );
}
public function testLink() {
$this->setMwGlobals( 'wgMathEnableFormulaLinks', true );
$mml = new MathNativeMML( '\sin', [ 'qid' => 'Q1' ] );
$this->assertTrue( $mml->render() );
$this->assertStringContainsString( 'href', $mml->getMathml() );
}
public function testId() {
$mml = new MathNativeMML( '\sin', [ 'id' => 'unique-id' ] );
$this->assertTrue( $mml->render() );
$this->assertStringContainsString( 'unique-id', $mml->getMathml() );
}
public function testBlock() {
$mml = new MathNativeMML( '\sin', [ 'display' => 'block' ] );
$this->assertTrue( $mml->render() );
$this->assertStringContainsString( 'block', $mml->getMathml() );
}
} }