From 99a81aca50e211bc3a616c618e16a0990f258f48 Mon Sep 17 00:00:00 2001 From: Emily Eisenberg Date: Wed, 1 Apr 2015 15:29:04 -0700 Subject: [PATCH] Fix the greediness of the `\color` function Summary: The greediness of the `\color` function wasn't set correctly, leading to expressions like `\color{red}\text{a}` parsing correctly, when they shouldn't. (This is based on how MathJax parses, since TeX doesn't have a `\color` function, so MathJax is the standard). Test Plan: - Make test - See that `\color{red}\text{a}` doesn't parse (like MathJax) - See that `\color{red}{\text{a}}` does parse (like MathJax) - See that `\color{red}\frac12` doesn't parse (like MathJax) - See that `\color{red}{\frac12}` does parse (like MathJax) - See that `\red\text{a}` doesn't parse (like MathJax) - See that `\red{\text{a}}` does parse (like MathJax) - See that `\red\frac12` doesn't parse (like MathJax) - See that `\red{\frac12}` does parse (like MathJax) Reviewers: alpert Reviewed By: alpert Differential Revision: https://phabricator.khanacademy.org/D17130 --- src/functions.js | 2 ++ test/katex-spec.js | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/src/functions.js b/src/functions.js index aa8b8eb..5bdbe02 100644 --- a/src/functions.js +++ b/src/functions.js @@ -112,6 +112,7 @@ var functions = { "\\color": { numArgs: 2, allowedInText: true, + greediness: 3, argTypes: ["color", "original"], handler: function(func, color, body) { // Normalize the different kinds of bodies (see \text above) @@ -235,6 +236,7 @@ var duplicatedFunctions = [ data: { numArgs: 1, allowedInText: true, + greediness: 3, handler: function(func, body) { var atoms; if (body.type === "ordgroup") { diff --git a/test/katex-spec.js b/test/katex-spec.js index 2042db9..e89ccdc 100644 --- a/test/katex-spec.js +++ b/test/katex-spec.js @@ -676,6 +676,13 @@ describe("A color parser", function() { it("should not parse a bad custom color", function() { expect(badCustomColorExpression).toNotParse(); }); + + it("should have correct greediness", function() { + expect("\\color{red}a").toParse(); + expect("\\color{red}{\\text{a}}").toParse(); + expect("\\color{red}\\text{a}").toNotParse(); + expect("\\color{red}\\frac12").toNotParse(); + }); }); describe("A tie parser", function() {