Skip to content

Commit f5c3f1f

Browse files
committed
Refactors package option parsing to ParseUtils.
1 parent 910f643 commit f5c3f1f

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

mathjax3-ts/input/tex/ParseUtil.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,30 @@ namespace ParseUtil {
470470
return (font ? {mathvariant: font} : {});
471471
};
472472

473+
474+
/**
475+
* Splits a package option list of the form [x=y,z=1] into an attribute list
476+
* of the form {x: y, z: 1}.
477+
* @param {string} attrib The attributes of the package.
478+
* @param {{[key: string]: number}?} allowed A list of allowed options.
479+
* @return {EnvList} The attribute list.
480+
*/
481+
export function splitPackageOptions(attrib: string, allowed: {[key: string]: number} = {}): EnvList {
482+
let def: EnvList = {};
483+
if (attrib !== '') {
484+
const attr = attrib.replace(/ /g, '').split(/,/);
485+
for (let i = 0, m = attr.length; i < m; i++) {
486+
const keyvalue = attr[i].split(/[:=]/);
487+
if (allowed[keyvalue[0]]) {
488+
let value = keyvalue[1];
489+
def[keyvalue[0]] = (value === 'true') ? true :
490+
(value === 'false') ? false : value;
491+
}
492+
}
493+
}
494+
return def;
495+
}
496+
473497
}
474498

475499
export default ParseUtil;

mathjax3-ts/input/tex/cancel/CancelConfiguration.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import TexParser from '../TexParser.js';
2727
import {TexConstant} from '../TexConstants.js';
2828
import {CommandMap} from '../SymbolMap.js';
2929
import {ParseMethod} from '../Types.js';
30+
import ParseUtil from '../ParseUtil.js';
3031

3132

3233
/**
@@ -41,27 +42,6 @@ const ALLOWED: {[key: string]: number} = {
4142
};
4243

4344

44-
/**
45-
* Rewrites tex parameter string into allowable attributes list.
46-
* @param {any} def
47-
* @param {string} attrib
48-
*/
49-
function setAttributes(def: any, attrib: string) {
50-
if (attrib !== '') {
51-
const attr = attrib.replace(/ /g, '').split(/,/);
52-
for (let i = 0, m = attr.length; i < m; i++) {
53-
const keyvalue = attr[i].split(/[:=]/);
54-
if (ALLOWED[keyvalue[0]]) {
55-
let value = keyvalue[1];
56-
def[keyvalue[0]] = (value === 'true') ? true :
57-
(value === 'false') ? false : value;
58-
}
59-
}
60-
}
61-
return def;
62-
};
63-
64-
6545
// Namespace
6646
export let CancelMethods: Record<string, ParseMethod> = {};
6747

@@ -75,7 +55,8 @@ export let CancelMethods: Record<string, ParseMethod> = {};
7555
CancelMethods.Cancel = function(parser: TexParser, name: string, notation: string) {
7656
const attr = parser.GetBrackets(name, '');
7757
const math = parser.ParseArg(name);
78-
const def = setAttributes({notation: notation}, attr);
58+
const def = ParseUtil.splitPackageOptions(attr, ALLOWED);
59+
def['notation'] = notation;
7960
parser.Push(parser.create('node', 'menclose', [math], def));
8061
};
8162

@@ -87,13 +68,13 @@ CancelMethods.Cancel = function(parser: TexParser, name: string, notation: strin
8768
* @param {string} notation The type of cancel notation to use.
8869
*/
8970

90-
CancelMethods.CancelTo = function(parser: TexParser, name: string, notation: string) {
91-
let value = parser.ParseArg(name);
71+
CancelMethods.CancelTo = function(parser: TexParser, name: string) {
9272
const attr = parser.GetBrackets(name, '');
73+
let value = parser.ParseArg(name);
9374
const math = parser.ParseArg(name);
94-
const def = setAttributes(
95-
{notation: TexConstant.Notation.UPDIAGONALSTRIKE + ' ' +
96-
TexConstant.Notation.UPDIAGONALARROW}, attr);
75+
const def = ParseUtil.splitPackageOptions(attr, ALLOWED);
76+
def ['notation'] = TexConstant.Notation.UPDIAGONALSTRIKE + ' ' +
77+
TexConstant.Notation.UPDIAGONALARROW;
9778
value = parser.create('node', 'mpadded', [value],
9879
{depth: '-.1em', height: ' + .1em', voffset: '.1em'});
9980
parser.Push(parser.create('node', 'msup',

0 commit comments

Comments
 (0)