Treat \operatorname as a valid operator for limits

* Extend logic for checking for valid operators
* Add tests

Bug: T365170
Change-Id: I4d3762e3258c78ac5fae8b923f12aabcd2175f3d
This commit is contained in:
physikerwelt 2024-11-06 23:04:13 +01:00
parent 4ce41fa717
commit 775fec5450
No known key found for this signature in database
GPG key ID: FCC793EFFA5FB13C
2 changed files with 29 additions and 3 deletions

View file

@ -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 ];
}

View file

@ -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( '<mi>a</mi><mi>&#x03B3;</mi>', $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] );
}
}