From fa1a5a4b3f34b263685c089d03f643a1617aaa70 Mon Sep 17 00:00:00 2001 From: Stegmujo Date: Wed, 5 Apr 2023 09:28:40 +0000 Subject: [PATCH] Fix style tag imbalance * sometimes there have been more start-tags than end style tags Bug: T315978 Change-Id: I6b20d45d7561b084376a54f4d6273a2ff5ed7bb4 --- src/TexVC/Nodes/TexArray.php | 19 +++++++++----- tests/phpunit/unit/TexVC/MMLRenderTest.php | 30 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/TexVC/Nodes/TexArray.php b/src/TexVC/Nodes/TexArray.php index 12b6ae941..97070da16 100644 --- a/src/TexVC/Nodes/TexArray.php +++ b/src/TexVC/Nodes/TexArray.php @@ -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; diff --git a/tests/phpunit/unit/TexVC/MMLRenderTest.php b/tests/phpunit/unit/TexVC/MMLRenderTest.php index a455d15dc..84dbbc6c1 100644 --- a/tests/phpunit/unit/TexVC/MMLRenderTest.php +++ b/tests/phpunit/unit/TexVC/MMLRenderTest.php @@ -135,6 +135,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( "", $mathMLtexVC ); + $this->assertStringContainsString( "z", $mathMLtexVC ); + $this->assertStringContainsString( "", $mathMLtexVC, ); + } + + public function testStyle2() { + $input = "\\displaystyle abc \\textstyle def"; + $mathMLtexVC = $this->generateMML( $input ); + $this->assertStringContainsString( "", $mathMLtexVC ); + $this->assertStringContainsString( "", $mathMLtexVC, ); + } + + public function testStyle3() { + $input = "\\scriptstyle{abc} def \\textstyle ghi"; + $mathMLtexVC = $this->generateMML( $input ); + $this->assertStringContainsString( "", $mathMLtexVC ); + $this->assertStringContainsString( "", $mathMLtexVC, ); + } + + public function testStyle4() { + $input = "\\textstyle b > \\textstyle \\delta"; + $mathMLtexVC = $this->generateMML( $input ); + $count = 0; + str_replace( "assertEquals( 2, $count ); + } + private function generateMML( $input, $chem = false ) { $texVC = new TexVC(); $resultT = $texVC->check( $input, [