From cd9ee27ac68337695bb94c8bdb21a781a2508f7a Mon Sep 17 00:00:00 2001 From: Ron Kok Date: Tue, 29 Dec 2020 11:52:41 -0800 Subject: [PATCH] fix: Prevent global group from adversely affecting color. (#2703) Co-authored-by: ylemkimon --- src/parseTree.js | 6 ++++++ test/katex-spec.js | 1 + 2 files changed, 7 insertions(+) diff --git a/src/parseTree.js b/src/parseTree.js index 26270fba..c1cd3df9 100644 --- a/src/parseTree.js +++ b/src/parseTree.js @@ -18,10 +18,16 @@ const parseTree = function(toParse: string, settings: Settings): AnyParseNode[] throw new TypeError('KaTeX can only parse string typed expression'); } const parser = new Parser(toParse, settings); + // Blank out any \df@tag to avoid spurious "Duplicate \tag" errors delete parser.gullet.macros.current["\\df@tag"]; + let tree = parser.parse(); + // Prevent a color definition from persisting between calls to katex.render(). + delete parser.gullet.macros.current["\\current@color"]; + delete parser.gullet.macros.current["\\color"]; + // If the input used \tag, it will set the \df@tag macro to the tag. // In this case, we separately parse the tag and wrap the tree. if (parser.gullet.macros.get("\\df@tag")) { diff --git a/test/katex-spec.js b/test/katex-spec.js index 715513e7..ac10e482 100644 --- a/test/katex-spec.js +++ b/test/katex-spec.js @@ -869,6 +869,7 @@ describe("A color parser", function() { const macros = {}; expect(oldColorExpression).toParseLike(r`\textcolor{#fA6}{x}y`, { colorIsTextColor: true, + globalGroup: true, macros: macros, }); expect(macros).toEqual({});