Merge "Fix style tag imbalance"

This commit is contained in:
jenkins-bot 2023-04-06 07:10:30 +00:00 committed by Gerrit Code Review
commit df0fbc7ebd
2 changed files with 43 additions and 6 deletions

View file

@ -157,7 +157,7 @@ class TexArray extends TexNode {
public function renderMML( $arguments = [], $state = [] ) {
// Everything here is for parsing displaystyle, probably refactored to TexVC grammar later
$fullRenderedArray = "";
$mmlStyle = null;
$mmlStyles = [];
$currentColor = null;
for ( $i = 0, $count = count( $this->args ); $i < $count; $i++ ) {
@ -213,17 +213,23 @@ class TexArray extends TexNode {
continue;
}
$styleArguments = $this->checkForStyleArgs( $current );
// For cases with "displaystyle{someargs} otherargs"
if ( $styleArguments ) {
$mmlStyle = new MMLmstyle( "", $styleArguments );
$state["styleargs"] = $styleArguments;
$fullRenderedArray .= $mmlStyle->getStart();
if ( $next instanceof Curly ) {
// Wrap with style-tags when the next element is a Curly which determines start and end tag.
$mmlStyle = new MMLmstyle( "", $styleArguments );
$fullRenderedArray .= $mmlStyle->getStart();
$fullRenderedArray .= $this->createMMLwithContext( $currentColor, $next, $state, $arguments );
$fullRenderedArray .= $mmlStyle->getEnd();
$mmlStyle = null;
unset( $state["styleargs"] );
$i++;
} else {
// Start the style indicator in cases like \textstyle abc
$mmlStyle = new MMLmstyle( "", $styleArguments );
$fullRenderedArray .= $mmlStyle->getStart();
$mmlStyles[] = $mmlStyle->getEnd();
}
} else {
$fullRenderedArray .= $this->createMMLwithContext( $currentColor, $current, $state, $arguments );
@ -239,8 +245,9 @@ class TexArray extends TexNode {
unset( $state["deriv"] );
}
}
if ( $mmlStyle ) {
$fullRenderedArray .= $mmlStyle->getEnd();
foreach ( array_reverse( $mmlStyles ) as $mmlStyleEnd ) {
$fullRenderedArray .= $mmlStyleEnd;
}
return $fullRenderedArray;

View file

@ -150,6 +150,36 @@ class MMLRenderTest extends MediaWikiUnitTestCase {
$this->assertStringContainsString( "#EF559F", $mathMLtexVC );
}
public function testStyle1() {
$input = "\\displaystyle \{U(\omega )\cdot \sigma _{H}(\omega )\} z";
$mathMLtexVC = $this->generateMML( $input );
$this->assertStringContainsString( "<mstyle displaystyle=\"true\" scriptlevel=\"0\">", $mathMLtexVC );
$this->assertStringContainsString( "<mi>z</mi>", $mathMLtexVC );
$this->assertStringContainsString( "</mstyle>", $mathMLtexVC, );
}
public function testStyle2() {
$input = "\\displaystyle abc \\textstyle def";
$mathMLtexVC = $this->generateMML( $input );
$this->assertStringContainsString( "<mstyle displaystyle=\"false\" scriptlevel=\"0\">", $mathMLtexVC );
$this->assertStringContainsString( "</mstyle>", $mathMLtexVC, );
}
public function testStyle3() {
$input = "\\scriptstyle{abc} def \\textstyle ghi";
$mathMLtexVC = $this->generateMML( $input );
$this->assertStringContainsString( "<mstyle displaystyle=\"false\" scriptlevel=\"1\">", $mathMLtexVC );
$this->assertStringContainsString( "</mstyle>", $mathMLtexVC, );
}
public function testStyle4() {
$input = "\\textstyle b > \\textstyle \\delta";
$mathMLtexVC = $this->generateMML( $input );
$count = 0;
str_replace( "<mstyle displaystyle", "", $mathMLtexVC, $count );
$this->assertEquals( 2, $count );
}
private function generateMML( $input, $chem = false ) {
$texVC = new TexVC();
$resultT = $texVC->check( $input, [