Skip to content

Commit 9f19cc1

Browse files
authored
Merge pull request #1037 from mathjax/key-val-update
More updates to keyvalOptions, and add GetBrackets() option to match brackets.
2 parents 8b7867e + fd34af5 commit 9f19cc1

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

ts/input/tex/ParseUtil.ts

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,15 @@ function readKeyval(text: string, l3keys: boolean = false): EnvList {
140140
* @return {string} The cleaned string.
141141
*/
142142
function removeBraces(text: string, count: number): string {
143+
if (count === 0) {
144+
return text.replace(/^\s+/, '')
145+
.replace(/([^\\\s]|^)((?:\\\\)*(?:\\\s)?)?\s+$/, '$1$2');
146+
}
143147
while (count > 0) {
144148
text = text.trim().slice(1, -1);
145149
count--;
146150
}
147-
return text.trim();
151+
return text;
148152
}
149153

150154

@@ -165,56 +169,46 @@ function readValue(text: string, end: string[],
165169
let value = '';
166170
let index = 0;
167171
let start = 0; // Counter for the starting left braces.
168-
let startCount = true; // Flag for counting starting left braces.
169-
let stopCount = false; // If true right braces are found directly
172+
let countBraces = true; // Flag for counting starting left braces.
170173
// after starting braces, but no other char yet.
171174
while (index < length) {
172175
let c = text[index++];
173176
switch (c) {
174177
case '\\': // Handle control sequences (in particular, \{ and \})
175-
value += c + text[index++];
176-
startCount = stopCount = false;
178+
value += c + (text[index++] || '');
179+
countBraces = false;
177180
continue;
178181
case ' ': // Ignore spaces.
179182
break;
180183
case '{':
181-
if (startCount) { // Count start left braces at start.
184+
if (countBraces) { // Count open left braces at start.
182185
start++;
183-
} else {
184-
stopCount = false;
185186
}
186187
braces++;
187188
break;
188189
case '}':
189-
if (braces) { // Closing braces.
190-
braces--;
191-
}
192-
if (startCount || stopCount) { // Closing braces at the start.
193-
start--;
194-
stopCount = true; // Continue to close braces.
190+
if (!braces) { // Closing braces.
191+
throw new TexError('ExtraCloseMissingOpen', 'Extra close brace or missing open brace');
195192
}
196-
startCount = false; // Stop counting start left braces.
193+
braces--;
194+
countBraces = false; // Stop counting start left braces.
197195
break;
198196
default:
199197
if (!braces && end.indexOf(c) !== -1) { // End character reached.
200-
return [stopCount ? 'true' : // If Stop count is true we
201-
// have balanced braces, only.
202-
removeBraces(value, l3keys ? Math.min(1, start) : start), c, text.slice(index)];
198+
return [removeBraces(value, l3keys ? Math.min(1, start) : start), c, text.slice(index)];
203199
}
204200
if (start > braces) { // Some start left braces have been closed.
205201
start = braces;
206202
}
207-
startCount = false;
208-
stopCount = false;
203+
countBraces = false;
209204
}
210205
value += c;
211206
}
212207
if (braces) {
213-
throw new TexError('ExtraOpenMissingClose',
214-
'Extra open brace or missing close brace');
208+
throw new TexError('ExtraOpenMissingClose', 'Extra open brace or missing close brace');
215209
}
216-
return (dropBrace && !stopCount && start) ? ['', '', removeBraces(value, 1)] :
217-
[stopCount ? 'true' : removeBraces(value, l3keys ? Math.min(1, start) : start), '', text.slice(index)];
210+
return (dropBrace && start) ? ['', '', removeBraces(value, 1)] :
211+
[removeBraces(value, l3keys ? Math.min(1, start) : start), '', text.slice(index)];
218212
}
219213

220214
export const ParseUtil = {

ts/input/tex/TexParser.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,16 @@ export default class TexParser {
346346

347347
/**
348348
* Get an optional LaTeX argument in brackets.
349-
* @param {string} name Name of the current control sequence.
350-
* @param {string} def? The default value for the optional argument.
349+
* @param {string} _name Name of the current control sequence.
350+
* @param {string?} def The default value for the optional argument.
351+
* @param {boolean=} matchBrackets True if indernal brackets must match.
351352
* @return {string} The optional argument.
352353
*/
353-
public GetBrackets(_name: string, def?: string): string {
354+
public GetBrackets(_name: string, def?: string, matchBrackets: boolean = false): string {
354355
if (this.GetNext() !== '[') {
355356
return def;
356357
}
357-
let j = ++this.i, parens = 0;
358+
let j = ++this.i, parens = 0, brackets = 0;
358359
while (this.i < this.string.length) {
359360
switch (this.string.charAt(this.i++)) {
360361
case '{': parens++; break;
@@ -366,9 +367,13 @@ export default class TexParser {
366367
'Extra close brace while looking for %1', '\']\'');
367368
}
368369
break;
370+
case '[': if (parens === 0) brackets++; break;
369371
case ']':
370372
if (parens === 0) {
371-
return this.string.slice(j, this.i - 1);
373+
if (!matchBrackets || brackets === 0) {
374+
return this.string.slice(j, this.i - 1);
375+
}
376+
brackets--;
372377
}
373378
break;
374379
}

0 commit comments

Comments
 (0)