Skip to content

Commit a2f9901

Browse files
authored
Merge pull request #1455 from mathjax/issue3550
Properly handle trailing spaces in \def. (mathjax/MathJax#3550)
2 parents 66ed50a + 08e87fa commit a2f9901

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ describe('Newcommand', () => {
222222
tex2mml('\\def\\x#1{\\def\\y##1#1{[##1]}\\y} \\x\\X abc \\X')
223223
).toMatchSnapshot();
224224
});
225+
226+
it('Def insignificant spaces', () => {
227+
expect(tex2mml('\\def\\x #1 {[#1]} \\x{x}')).toMatchSnapshot();
228+
});
229+
230+
it('Def significant spaces 1', () => {
231+
expectTexError('\\def\\x#1 #2{[#1,#2]} \\x{a}{b}').toBe('Runaway argument for \\x?');
232+
expect(tex2mml('\\def\\x#1 #2{[#1,#2]} \\x{a} {b}')).toMatchSnapshot();
233+
});
234+
235+
it('Def significant spaces 2', () => {
236+
expectTexError('\\def\\x#1 #2 {[#1,#2]} \\x{a}{b}').toBe('Runaway argument for \\x?');
237+
expectTexError('\\def\\x#1 #2 {[#1,#2]} \\x{a} {b}').toBe('Runaway argument for \\x?');
238+
expect(tex2mml('\\def\\x#1 #2{[#1,#2]} \\x{a} {b} ')).toMatchSnapshot();
239+
});
225240
});
226241

227242
/**********************************************************************************/

testsuite/tests/input/tex/__snapshots__/Newcommand.test.ts.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,34 @@ exports[`Newcommand Def Template Matching 1`] = `
327327
</math>"
328328
`;
329329

330+
exports[`Newcommand Def insignificant spaces 1`] = `
331+
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\def\\x #1 {[#1]} \\x{x}" display="block">
332+
<mo data-latex="[" stretchy="false">[</mo>
333+
<mi data-latex="x">x</mi>
334+
<mo data-latex="]" stretchy="false">]</mo>
335+
</math>"
336+
`;
337+
338+
exports[`Newcommand Def significant spaces 1 1`] = `
339+
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\def\\x#1 #2{[#1,#2]} \\x{a} {b}" display="block">
340+
<mo data-latex="[" stretchy="false">[</mo>
341+
<mi data-latex="a">a</mi>
342+
<mo data-latex=",">,</mo>
343+
<mi data-latex="b">b</mi>
344+
<mo data-latex="]" stretchy="false">]</mo>
345+
</math>"
346+
`;
347+
348+
exports[`Newcommand Def significant spaces 2 1`] = `
349+
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\def\\x#1 #2{[#1,#2]} \\x{a} {b} " display="block">
350+
<mo data-latex="[" stretchy="false">[</mo>
351+
<mi data-latex="a">a</mi>
352+
<mo data-latex=",">,</mo>
353+
<mi data-latex="b">b</mi>
354+
<mo data-latex="]" stretchy="false">]</mo>
355+
</math>"
356+
`;
357+
330358
exports[`Newcommand Let Angle Circular 1`] = `
331359
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\let\\lp\\langle\\let\\rp\\rangle\\let\\mp\\rp\\left\\lp \\frac{a}{b}\\middle\\mp c \\right\\rp" display="block">
332360
<mrow data-mjx-texclass="INNER" data-latex="\\let\\lp\\langle\\let\\rp\\rangle\\let\\mp\\rp\\left\\lp \\frac{a}{b}\\middle\\mp c \\right\\rp">

ts/input/tex/newcommand/NewcommandUtil.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ export const NewcommandUtil = {
166166
// @test Optional Brace Error
167167
// parser.i >= i!
168168
params[n] = parser.string.substring(i, parser.i);
169+
if (params[n].replace(/^ +/, '') === '' && params.slice(0, n).join('') === '') {
170+
return n;
171+
}
169172
}
170173
if (params.length > 0) {
171174
// @test Def Let, Def Optional Brace

0 commit comments

Comments
 (0)