Skip to content

Commit 794b6ac

Browse files
committed
Rewrites StackItemFactory to use Factory interface.
1 parent 9011c67 commit 794b6ac

File tree

5 files changed

+29
-53
lines changed

5 files changed

+29
-53
lines changed

mathjax3-ts/input/tex/Stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default class Stack {
8787
continue;
8888
}
8989
const item = NodeUtil.isNode(node) ?
90-
this._factory.create('mml', node) : node;
90+
this._factory.create('mml', node) : node as StackItem;
9191
item.global = this.global;
9292
const [top, success] =
9393
this.stack.length ? this.Top().checkItem(item) : [null, true];

mathjax3-ts/input/tex/StackItem.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
import {MmlNode, TextNode} from '../../core/MmlTree/MmlNode.js';
26+
import {FactoryNodeClass} from '../../core/Tree/Factory.js';
2627
import TexError from './TexError.js';
2728
import StackItemFactory from './StackItemFactory.js';
2829

@@ -273,6 +274,13 @@ export interface StackItem extends NodeStack {
273274
*/
274275
setProperty(key: string, value: Prop): StackItem;
275276

277+
/**
278+
* Sets a list of properties.
279+
* @param {PropList} def The properties to set.
280+
* @return {StackItem} Returns the stack item object for pipelining.
281+
*/
282+
setProperties(def: PropList): StackItem;
283+
276284
/**
277285
* Convenience method for returning the string property "name".
278286
* @return {string} The value for the name property.
@@ -306,8 +314,8 @@ export interface StackItem extends NodeStack {
306314

307315
}
308316

309-
export interface StackItemClass {
310-
new (factory: StackItemFactory, ...args: any[]): StackItem;
317+
export interface StackItemClass extends FactoryNodeClass<StackItem> {
318+
// new (factory: StackItemFactory, ...args: any[]): StackItem;
311319
}
312320

313321

@@ -469,9 +477,7 @@ export abstract class BaseItem extends MmlStack implements StackItem {
469477

470478

471479
/**
472-
* Sets a list of properties.
473-
* @param {PropList} def The properties to set.
474-
* @return {StackItem} Returns the stack item object for pipelining.
480+
* @override
475481
*/
476482
public setProperties(def: PropList) {
477483
Object.assign(this._properties, def);

mathjax3-ts/input/tex/StackItemFactory.ts

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import {StackItemClass, StackItem, BaseItem} from './StackItem.js';
2727
import {MmlNode, TextNode, TEXCLASS} from '../../core/MmlTree/MmlNode.js';
2828
import ParseOptions from './ParseOptions.js';
29+
import {AbstractFactory} from '../../core/Tree/Factory.js';
2930

3031

3132
class DummyItem extends BaseItem {}
@@ -35,71 +36,39 @@ class DummyItem extends BaseItem {}
3536
* classes. They can be changed, deleted or added to, if and when necessary.
3637
*
3738
* @constructor
39+
* @extends {AbstractFactory}
3840
*/
39-
export default class StackItemFactory {
41+
export default class StackItemFactory extends AbstractFactory<StackItem, StackItemClass>{
4042

41-
private static DefaultStackItems: {[kind: string]: StackItemClass} = {
43+
/**
44+
* @override
45+
*/
46+
public static DefaultStackItems: {[kind: string]: StackItemClass} = {
4247
[DummyItem.prototype.kind]: DummyItem
4348
};
4449

50+
4551
/**
46-
* A default item.
52+
* @override
4753
*/
48-
public defaultKind: string = 'dummy';
54+
public defaultKind = 'dummy';
55+
4956

5057
/**
5158
* The parser configuration.
5259
* @type {ParseOptions}
5360
*/
5461
public configuration: ParseOptions = null;
5562

56-
private itemMap: Map<string, StackItemClass> = new Map();
57-
58-
private item: {[kind: string]: (factory: StackItemFactory, ...args: any[]) => StackItem} = {};
59-
60-
/**
61-
* @constructor
62-
*/
63-
constructor() {
64-
this.addStackItems(StackItemFactory.DefaultStackItems);
65-
}
66-
6763

6864
/**
6965
* Adds a list of stack items to the current factory.
7066
* @param {Object.<string, StackItemClass>} stackItems A list of stackitems.
7167
*/
7268
public addStackItems(stackItems: {[kind: string]: StackItemClass}) {
7369
for (const kind of Object.keys(stackItems)) {
74-
this.itemMap.set(kind, stackItems[kind]);
75-
let constr = this.itemMap.get(kind);
76-
this.item[kind] = (factory: StackItemFactory, ...args: any[]) => {
77-
return new constr(factory, ...args);
78-
};
70+
this.setNodeClass(kind, stackItems[kind]);
7971
}
8072
}
8173

82-
83-
/**
84-
* Removes stack item classs from the factory.
85-
* @param {string[]} keys The classes to remove.
86-
*/
87-
public removeStackItems(keys: string[]) {
88-
for (const key of keys) {
89-
this.itemMap.delete(key);
90-
}
91-
}
92-
93-
94-
/**
95-
*
96-
* @param {string} kind The type of
97-
* @return {}
98-
*/
99-
public create(kind: string, ...parameters: any[]) {
100-
// return (this.item[kind] || this.item[this.defaultKind])(this, ...parameters);
101-
return (this.item[kind] || this.item[this.defaultKind])
102-
.apply(null, [this].concat(...parameters));
103-
}
104-
10574
}

mathjax3-ts/input/tex/ams/AmsMethods.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ AmsMethods.Multline = function (parser: TexParser, begin: StackItem, numbered: b
117117
// @test Shove*, Multline
118118
parser.Push(begin);
119119
ParseUtil.checkEqnEnv(parser);
120-
const item = parser.itemFactory.create('multline', numbered, parser.stack);
120+
const item = parser.itemFactory.create('multline', numbered, parser.stack) as ArrayItem;
121121
item.arraydef = {
122122
displaystyle: true,
123123
rowspacing: '.5em',

mathjax3-ts/input/tex/base/BaseMethods.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ BaseMethods.Matrix = function(parser: TexParser, name: string,
11091109
parser.i = 0;
11101110
}
11111111
// @test Matrix Braces, Matrix Columns, Matrix Rows.
1112-
const array = parser.itemFactory.create('array').setProperty('requireClose', true);
1112+
const array = parser.itemFactory.create('array').setProperty('requireClose', true) as sitem.ArrayItem;
11131113
array.arraydef = {
11141114
rowspacing: (vspacing || '4pt'),
11151115
columnspacing: (spacing || '1em')
@@ -1395,7 +1395,7 @@ BaseMethods.Array = function(parser: TexParser, begin: StackItem,
13951395
let lines = ('c' + align).replace(/[^clr|:]/g, '').replace(/[^|:]([|:])+/g, '$1');
13961396
align = align.replace(/[^clr]/g, '').split('').join(' ');
13971397
align = align.replace(/l/g, 'left').replace(/r/g, 'right').replace(/c/g, 'center');
1398-
const array = parser.itemFactory.create('array');
1398+
const array = parser.itemFactory.create('array') as sitem.ArrayItem;
13991399
array.arraydef = {
14001400
columnalign: align,
14011401
columnspacing: (spacing || '1em'),
@@ -1492,7 +1492,8 @@ BaseMethods.EqnArray = function(parser: TexParser, begin: StackItem,
14921492
}
14931493
align = align.replace(/[^clr]/g, '').split('').join(' ');
14941494
align = align.replace(/l/g, 'left').replace(/r/g, 'right').replace(/c/g, 'center');
1495-
let newItem = parser.itemFactory.create('eqnarray', begin.getName(), numbered, taggable, parser.stack.global);
1495+
let newItem = parser.itemFactory.create('eqnarray', begin.getName(),
1496+
numbered, taggable, parser.stack.global) as sitem.ArrayItem;
14961497
newItem.arraydef = {
14971498
displaystyle: true,
14981499
columnalign: align,

0 commit comments

Comments
 (0)