From 1e7fe633e8a2122d4f71fb39fa2cfc1212828975 Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Thu, 26 Dec 2019 15:38:13 -0500 Subject: [PATCH 1/2] Update FindTeX to properly skip comments, even if they include unbalanced braces. Resolves issue mathjax/MathJax#2271 --- ts/input/tex/FindTeX.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ts/input/tex/FindTeX.ts b/ts/input/tex/FindTeX.ts index ad40c3fb7..c8e45c0a0 100644 --- a/ts/input/tex/FindTeX.ts +++ b/ts/input/tex/FindTeX.ts @@ -154,7 +154,7 @@ export class FindTeX extends AbstractFindMath { * @return {RegExp} The regular expression for the end delimiter */ protected endPattern(end: string) { - return new RegExp(quotePattern(end) + '|\\\\(?:[a-zA-Z]|.)|[{}]', 'g'); + return new RegExp(quotePattern(end) + '|\\\\(?:[a-zA-Z]|.)|[{}%]', 'g'); } /** @@ -180,11 +180,24 @@ export class FindTeX extends AbstractFindMath { braces++; } else if (match[0] === '}' && braces) { braces--; + } else if (match[0] === '%') { + pattern.lastIndex += this.skipComment(text.substr(pattern.lastIndex)); } } return null; } + /** + * Skip a comment (without matching braces or other special characters) + * + * @param {string} text The string containing the comment to skip + * @return {number} The position of the string after the comment + */ + protected skipComment(text: string) { + const match = text.match(/.*?[\n\r]/); + return match ? match[0].length : text.length; + } + /** * Search a string for math delimited by one of the delimiter pairs, * or by \begin{env}...\end{env}, or \eqref{...}, \ref{...}, \\, or \$. From ef679bd71e37dc1f0bdc43faad8563b8b3d86a8a Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Tue, 31 Dec 2019 16:05:07 -0500 Subject: [PATCH 2/2] Simplify how comments are handled. --- ts/input/tex/FindTeX.ts | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/ts/input/tex/FindTeX.ts b/ts/input/tex/FindTeX.ts index c8e45c0a0..2f73f1905 100644 --- a/ts/input/tex/FindTeX.ts +++ b/ts/input/tex/FindTeX.ts @@ -154,7 +154,7 @@ export class FindTeX extends AbstractFindMath { * @return {RegExp} The regular expression for the end delimiter */ protected endPattern(end: string) { - return new RegExp(quotePattern(end) + '|\\\\(?:[a-zA-Z]|.)|[{}%]', 'g'); + return new RegExp(quotePattern(end) + '|\\\\(?:[a-zA-Z]|.)|[{}]|%.*(?:[\n\r]|$)', 'g'); } /** @@ -180,24 +180,13 @@ export class FindTeX extends AbstractFindMath { braces++; } else if (match[0] === '}' && braces) { braces--; - } else if (match[0] === '%') { - pattern.lastIndex += this.skipComment(text.substr(pattern.lastIndex)); + } else if (match[0].substr(0) === '%') { + pattern.lastIndex += match[0].length; } } return null; } - /** - * Skip a comment (without matching braces or other special characters) - * - * @param {string} text The string containing the comment to skip - * @return {number} The position of the string after the comment - */ - protected skipComment(text: string) { - const match = text.match(/.*?[\n\r]/); - return match ? match[0].length : text.length; - } - /** * Search a string for math delimited by one of the delimiter pairs, * or by \begin{env}...\end{env}, or \eqref{...}, \ref{...}, \\, or \$.