Skip to content

Commit 2bb30bb

Browse files
authored
Merge pull request #1428 from mathjax/fix/empty-op
Add isEmpty test (deep tree check) to MmlNode and use it to test for empty mathop.
2 parents 2cf7459 + 574e1c3 commit 2bb30bb

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

testsuite/tests/input/tex/Base.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12191,7 +12191,7 @@ describe('Character Class Changes', () => {
1219112191

1219212192
/********************************************************************************/
1219312193

12194-
it('Mathop No Apply', () => {
12194+
it('Mathop No Apply I', () => {
1219512195
toXmlMatch(
1219612196
tex2mml('\\mathop{} x'),
1219712197
`<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\mathop{} x" display="block">
@@ -12203,6 +12203,21 @@ describe('Character Class Changes', () => {
1220312203

1220412204
/********************************************************************************/
1220512205

12206+
it('Mathop no Apply II', () => {
12207+
toXmlMatch(
12208+
tex2mml('\\mathop{\\mathrm{}{}} x'),
12209+
`<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\mathop{\\mathrm{}{}} x" display="block">
12210+
<mrow data-mjx-texclass="OP" data-latex="\\mathop{\\mathrm{}{}}">
12211+
<mrow data-mjx-texclass="ORD" data-latex="\\mathrm{}"></mrow>
12212+
<mrow data-mjx-texclass="ORD" data-latex="{}"></mrow>
12213+
</mrow>
12214+
<mi data-latex="x">x</mi>
12215+
</math>`
12216+
);
12217+
});
12218+
12219+
/********************************************************************************/
12220+
1220612221
it('Mathrel', () => {
1220712222
toXmlMatch(
1220812223
tex2mml('\\mathrel{R}'),

ts/core/MmlTree/MmlNode.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export interface MmlNode extends Node<MmlNode, MmlNodeClass> {
144144
readonly isSpacelike: boolean;
145145
readonly linebreakContainer: boolean;
146146
readonly linebreakAlign: string;
147+
readonly isEmpty: boolean;
147148

148149
/**
149150
* The expected number of children (-1 means use inferred mrow)
@@ -540,6 +541,16 @@ export abstract class AbstractMmlNode
540541
return 'data-align';
541542
}
542543

544+
/**
545+
* @returns {string} True if all child nodes are empty
546+
*/
547+
public get isEmpty(): boolean {
548+
for (const child of this.childNodes) {
549+
if (!child.isEmpty) return false;
550+
}
551+
return true;
552+
}
553+
543554
/**
544555
* @returns {number} The number of children allowed, or Infinity for any number,
545556
* or -1 for when an inferred row is needed for the children.
@@ -1023,6 +1034,18 @@ export abstract class AbstractMmlTokenNode extends AbstractMmlNode {
10231034
return true;
10241035
}
10251036

1037+
/**
1038+
* @override
1039+
*/
1040+
public get isEmpty() {
1041+
for (const child of this.childNodes) {
1042+
if (!(child instanceof TextNode) || child.getText().length) {
1043+
return false;
1044+
}
1045+
}
1046+
return true;
1047+
}
1048+
10261049
/**
10271050
* Get the text of the token node (skipping mglyphs, and combining
10281051
* multiple text nodes)
@@ -1228,6 +1251,13 @@ export abstract class AbstractMmlEmptyNode
12281251
return false;
12291252
}
12301253

1254+
/**
1255+
* @returns {boolean} Is empty
1256+
*/
1257+
public get isEmpty(): boolean {
1258+
return true;
1259+
}
1260+
12311261
/**
12321262
* @returns {boolean} Not embellished
12331263
*/

ts/input/tex/base/BaseItems.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ export class FnItem extends BaseItem {
788788
}
789789
}
790790
// @test Mathop Apply, Mathop No Apply
791-
if (top.isKind('TeXAtom') && top.childNodes[0].childNodes.length === 0) {
791+
if (top.isKind('TeXAtom') && top.isEmpty) {
792792
return [[top, item], true];
793793
}
794794
// @test Named Function, Named Function Arg

ts/input/tex/mathtools/MathtoolsUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export const MathtoolsUtil = {
158158
parser.stack.env,
159159
parser.configuration
160160
).mml();
161-
return mml.isKind('TeXAtom') && mml.childNodes[0].childNodes.length === 0
161+
return mml.isKind('TeXAtom') && mml.isEmpty
162162
? parser.create('node', 'none')
163163
: mml;
164164
},

0 commit comments

Comments
 (0)