allow sizing commands inside optional groups (#885)

* allow sizing commands inside optional groups

* allow color, old font, and style commands inside optional groups
This commit is contained in:
Kevin Barabash
2017-10-03 11:30:59 -06:00
committed by GitHub
parent 1c1b3c81b6
commit 71e0b35b27
7 changed files with 235 additions and 10 deletions

View File

@@ -203,7 +203,7 @@ export default class Parser {
if (breakOnInfix && functions[lex.text] && functions[lex.text].infix) {
break;
}
const atom = this.parseAtom();
const atom = this.parseAtom(breakOnTokenText);
if (!atom) {
if (!this.settings.throwOnError && lex.text[0] === "\\") {
const errorNode = this.handleUnsupportedCmd();
@@ -349,12 +349,13 @@ export default class Parser {
/**
* Parses a group with optional super/subscripts.
*
* @param {"]" | "}"} breakOnTokenText - character to stop parsing the group on.
* @return {?ParseNode}
*/
parseAtom() {
parseAtom(breakOnTokenText) {
// The body of an atom is an implicit group, so that things like
// \left(x\right)^2 work correctly.
const base = this.parseImplicitGroup();
const base = this.parseImplicitGroup(breakOnTokenText);
// In text mode, we don't have superscripts or subscripts
if (this.mode === "text") {
@@ -466,9 +467,10 @@ export default class Parser {
* small text {\Large large text} small text again
* It is also used for \left and \right to get the correct grouping.
*
* @param {"]" | "}"} breakOnTokenText - character to stop parsing the group on.
* @return {?ParseNode}
*/
parseImplicitGroup() {
parseImplicitGroup(breakOnTokenText) {
const start = this.parseSymbol();
if (start == null) {
@@ -527,7 +529,7 @@ export default class Parser {
} else if (utils.contains(Parser.sizeFuncs, func)) {
// If we see a sizing function, parse out the implicit body
this.consumeSpaces();
const body = this.parseExpression(false);
const body = this.parseExpression(false, breakOnTokenText);
return new ParseNode("sizing", {
// Figure out what size to use based on the list of functions above
size: utils.indexOf(Parser.sizeFuncs, func) + 1,
@@ -536,7 +538,7 @@ export default class Parser {
} else if (utils.contains(Parser.styleFuncs, func)) {
// If we see a styling function, parse out the implicit body
this.consumeSpaces();
const body = this.parseExpression(true);
const body = this.parseExpression(true, breakOnTokenText);
return new ParseNode("styling", {
// Figure out what style to use by pulling out the style from
// the function name
@@ -547,7 +549,7 @@ export default class Parser {
const style = Parser.oldFontFuncs[func];
// If we see an old font function, parse out the implicit body
this.consumeSpaces();
const body = this.parseExpression(true);
const body = this.parseExpression(true, breakOnTokenText);
if (style.slice(0, 4) === 'text') {
return new ParseNode("text", {
style: style,
@@ -565,7 +567,7 @@ export default class Parser {
if (!color) {
throw new ParseError("\\color not followed by color");
}
const body = this.parseExpression(true);
const body = this.parseExpression(true, breakOnTokenText);
return new ParseNode("color", {
type: "color",
color: color.result.value,
@@ -875,7 +877,7 @@ export default class Parser {
if (this.nextToken.text === (optional ? "[" : "{")) {
// If we get a brace, parse an expression
this.consume();
const expression = this.parseExpression(false, optional ? "]" : null);
const expression = this.parseExpression(false, optional ? "]" : "}");
const lastToken = this.nextToken;
// Make sure we get a close brace
this.expect(optional ? "]" : "}");