From 775fec5450df8a2d10bedc6f8b578ff449d2c9ba Mon Sep 17 00:00:00 2001 From: physikerwelt Date: Wed, 6 Nov 2024 23:04:13 +0100 Subject: [PATCH] Treat \operatorname as a valid operator for limits * Extend logic for checking for valid operators * Add tests Bug: T365170 Change-Id: I4d3762e3258c78ac5fae8b923f12aabcd2175f3d --- src/WikiTexVC/Nodes/TexArray.php | 13 ++++++++++--- .../unit/WikiTexVC/Nodes/TexArrayTest.php | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/WikiTexVC/Nodes/TexArray.php b/src/WikiTexVC/Nodes/TexArray.php index e8480e0bf..9de11c858 100644 --- a/src/WikiTexVC/Nodes/TexArray.php +++ b/src/WikiTexVC/Nodes/TexArray.php @@ -128,9 +128,16 @@ class TexArray extends TexNode implements \ArrayAccess, \IteratorAggregate { $tu = TexUtil::getInstance(); // Check whether the current node is a possible preceding literal - if ( !( $currentNode instanceof Literal - && ( $tu->nullary_macro( trim( $currentNode->getArg() ) ) - || trim( $currentNode->getArg() ) == "\\lim" ) ) ) { + if ( !( + // logically superfluous brackets were inserted to improve readability + ( $currentNode instanceof Literal && + // Check if the current node is a nullary macro such as \iint, \sum, \prod, etc. + ( $tu->nullary_macro( trim( $currentNode->getArg() ) ) + // or a limit operator + || ( trim( $currentNode->getArg() ) == "\\lim" ) ) ) || + // or the special case of \operatorname + ( $currentNode instanceof Fun1nb && $currentNode->getFname() == "\\operatorname" ) + ) ) { return [ null, false ]; } diff --git a/tests/phpunit/unit/WikiTexVC/Nodes/TexArrayTest.php b/tests/phpunit/unit/WikiTexVC/Nodes/TexArrayTest.php index a017623b2..ab1e3cba4 100644 --- a/tests/phpunit/unit/WikiTexVC/Nodes/TexArrayTest.php +++ b/tests/phpunit/unit/WikiTexVC/Nodes/TexArrayTest.php @@ -3,6 +3,8 @@ namespace MediaWiki\Extension\Math\Tests\WikiTexVC\Nodes; use InvalidArgumentException; +use MediaWiki\Extension\Math\WikiTexVC\Nodes\DQ; +use MediaWiki\Extension\Math\WikiTexVC\Nodes\Fun1nb; use MediaWiki\Extension\Math\WikiTexVC\Nodes\Literal; use MediaWiki\Extension\Math\WikiTexVC\Nodes\TexArray; use MediaWiki\Extension\Math\WikiTexVC\Nodes\TexNode; @@ -132,4 +134,21 @@ class TexArrayTest extends MediaWikiUnitTestCase { $res = $ta->renderMML( [], [ 'squashLiterals' => true ] ); $this->assertEquals( 'aγ', $res ); } + + public function testSumInLimits() { + $ta = new TexArray(); + $sum = new Literal( '\sum' ); + $res = $ta->checkForLimits( $sum, new DQ( new Literal( '\limits' ), new Literal( 'n' ) ) ); + $this->assertTrue( $res[1] ); + $this->assertEquals( $sum, $res[0] ); + } + + public function testCustomOpInLimits() { + $ta = new TexArray(); + $custom = new Fun1nb( '\operatorname', new TexArray( new Literal( 'S' ) ) ); + $res = $ta->checkForLimits( $custom, new DQ( new Literal( '\limits' ), new Literal( 'n' ) ) ); + $this->assertTrue( $res[1] ); + $this->assertEquals( $custom, $res[0] ); + } + }