From 70a3b196ed59f594ac23851352cda83f43e58128 Mon Sep 17 00:00:00 2001 From: zorkow Date: Sat, 2 Dec 2023 14:50:04 +0100 Subject: [PATCH 1/2] Rewrites mml tags into an enum. --- ts/enrich_mathml/case_double_script.ts | 5 +- ts/enrich_mathml/case_embellished.ts | 5 +- ts/enrich_mathml/case_empheq.ts | 5 +- ts/enrich_mathml/case_limit.ts | 11 ++-- ts/enrich_mathml/case_multiindex.ts | 3 +- ts/enrich_mathml/case_multiscripts.ts | 3 +- ts/enrich_mathml/case_table.ts | 3 +- ts/enrich_mathml/enrich_mathml.ts | 3 +- ts/semantic_tree/semantic_heuristics.ts | 7 +- ts/semantic_tree/semantic_mathml.ts | 3 +- ts/semantic_tree/semantic_processor.ts | 17 ++--- ts/semantic_tree/semantic_util.ts | 87 ++++++++++++++++++++----- 12 files changed, 107 insertions(+), 45 deletions(-) diff --git a/ts/enrich_mathml/case_double_script.ts b/ts/enrich_mathml/case_double_script.ts index 9e5bbaf0b..c4b9f2af0 100644 --- a/ts/enrich_mathml/case_double_script.ts +++ b/ts/enrich_mathml/case_double_script.ts @@ -21,6 +21,7 @@ import * as DomUtil from '../common/dom_util.js'; import { SemanticRole } from '../semantic_tree/semantic_meaning.js'; import { SemanticNode } from '../semantic_tree/semantic_node.js'; +import { MMLTAGS } from '../semantic_tree/semantic_util.js'; import { AbstractEnrichCase } from './abstract_enrich_case.js'; import * as EnrichMathml from './enrich_mathml.js'; @@ -45,8 +46,8 @@ export class CaseDoubleScript extends AbstractEnrichCase { const mmlTag = DomUtil.tagName(semantic.mathmlTree); const role = semantic.childNodes[0].role; return ( - (mmlTag === 'MSUBSUP' && role === SemanticRole.SUBSUP) || - (mmlTag === 'MUNDEROVER' && role === SemanticRole.UNDEROVER) + (mmlTag === MMLTAGS.MSUBSUP && role === SemanticRole.SUBSUP) || + (mmlTag === MMLTAGS.MUNDEROVER && role === SemanticRole.UNDEROVER) ); } diff --git a/ts/enrich_mathml/case_embellished.ts b/ts/enrich_mathml/case_embellished.ts index 5526236d7..204975092 100644 --- a/ts/enrich_mathml/case_embellished.ts +++ b/ts/enrich_mathml/case_embellished.ts @@ -24,6 +24,7 @@ import { SemanticType } from '../semantic_tree/semantic_meaning.js'; import { SemanticNode } from '../semantic_tree/semantic_node.js'; +import { MMLTAGS } from '../semantic_tree/semantic_util.js'; import { AbstractEnrichCase } from './abstract_enrich_case.js'; import { CaseDoubleScript } from './case_double_script.js'; @@ -300,10 +301,10 @@ export class CaseEmbellished extends AbstractEnrichCase { const mmlTag = DomUtil.tagName(mml); let parent = null; let caller; - if (mmlTag === 'MSUBSUP') { + if (mmlTag === MMLTAGS.MSUBSUP) { parent = semantic.childNodes[0]; caller = CaseDoubleScript; - } else if (mmlTag === 'MMULTISCRIPTS') { + } else if (mmlTag === MMLTAGS.MMULTISCRIPTS) { if ( semantic.type === SemanticType.SUPERSCRIPT || semantic.type === SemanticType.SUBSCRIPT diff --git a/ts/enrich_mathml/case_empheq.ts b/ts/enrich_mathml/case_empheq.ts index 8daf5f8cc..a1718c43b 100644 --- a/ts/enrich_mathml/case_empheq.ts +++ b/ts/enrich_mathml/case_empheq.ts @@ -19,6 +19,7 @@ */ import { SemanticNode } from '../semantic_tree/semantic_node.js'; +import { MMLTAGS } from '../semantic_tree/semantic_util.js'; import { AbstractEnrichCase } from './abstract_enrich_case.js'; import * as EnrichMathml from './enrich_mathml.js'; import { addMrow, setAttributes } from './enrich_attr.js'; @@ -86,7 +87,7 @@ export class CaseEmpheq extends AbstractEnrichCase { } if ( !node.mathmlTree || - (DomUtil.tagName(node.mathmlTree) === 'MTABLE' && + (DomUtil.tagName(node.mathmlTree) === MMLTAGS.MTABLE && node.annotation['Emph']?.length && node.annotation['Emph'][0] !== 'table') ) { @@ -96,7 +97,7 @@ export class CaseEmpheq extends AbstractEnrichCase { this.mrows.unshift(newNode); } else { if ( - DomUtil.tagName(node.mathmlTree) === 'MTABLE' && + DomUtil.tagName(node.mathmlTree) === MMLTAGS.MTABLE && node.annotation['Emph']?.length && node.annotation['Emph'][0] === 'table' ) { diff --git a/ts/enrich_mathml/case_limit.ts b/ts/enrich_mathml/case_limit.ts index 20192d06a..eb8103ed3 100644 --- a/ts/enrich_mathml/case_limit.ts +++ b/ts/enrich_mathml/case_limit.ts @@ -22,6 +22,7 @@ import * as DomUtil from '../common/dom_util.js'; import { SemanticType } from '../semantic_tree/semantic_meaning.js'; import { SemanticNode } from '../semantic_tree/semantic_node.js'; +import { MMLTAGS } from '../semantic_tree/semantic_util.js'; import { AbstractEnrichCase } from './abstract_enrich_case.js'; import * as EnrichMathml from './enrich_mathml.js'; @@ -46,12 +47,12 @@ export class CaseLimit extends AbstractEnrichCase { const type = semantic.type; return ( ((type === SemanticType.LIMUPPER || type === SemanticType.LIMLOWER) && - (mmlTag === 'MSUBSUP' || mmlTag === 'MUNDEROVER')) || + (mmlTag === MMLTAGS.MSUBSUP || mmlTag === MMLTAGS.MUNDEROVER)) || (type === SemanticType.LIMBOTH && - (mmlTag === 'MSUB' || - mmlTag === 'MUNDER' || - mmlTag === 'MSUP' || - mmlTag === 'MOVER')) + (mmlTag === MMLTAGS.MSUB || + mmlTag === MMLTAGS.MUNDER || + mmlTag === MMLTAGS.MSUP || + mmlTag === MMLTAGS.MOVER)) ); } diff --git a/ts/enrich_mathml/case_multiindex.ts b/ts/enrich_mathml/case_multiindex.ts index 8da5f2b36..9f025f8e3 100644 --- a/ts/enrich_mathml/case_multiindex.ts +++ b/ts/enrich_mathml/case_multiindex.ts @@ -25,6 +25,7 @@ import { } from '../semantic_tree/semantic_meaning.js'; import { SemanticNode } from '../semantic_tree/semantic_node.js'; import { Sexp } from '../semantic_tree/semantic_skeleton.js'; +import { MMLTAGS } from '../semantic_tree/semantic_util.js'; import { AbstractEnrichCase } from './abstract_enrich_case.js'; import * as EnrichMathml from './enrich_mathml.js'; @@ -120,7 +121,7 @@ export abstract class CaseMultiindex extends AbstractEnrichCase { // mprescripts if ( children[childCounter] && - DomUtil.tagName(children[childCounter]) !== 'MPRESCRIPTS' + DomUtil.tagName(children[childCounter]) !== MMLTAGS.MPRESCRIPTS ) { this.mml.insertBefore( children[childCounter], diff --git a/ts/enrich_mathml/case_multiscripts.ts b/ts/enrich_mathml/case_multiscripts.ts index 070e20026..d2c90b1fa 100644 --- a/ts/enrich_mathml/case_multiscripts.ts +++ b/ts/enrich_mathml/case_multiscripts.ts @@ -25,6 +25,7 @@ import { } from '../semantic_tree/semantic_meaning.js'; import { SemanticNode } from '../semantic_tree/semantic_node.js'; import { SemanticSkeleton } from '../semantic_tree/semantic_skeleton.js'; +import { MMLTAGS } from '../semantic_tree/semantic_util.js'; import { CaseMultiindex } from './case_multiindex.js'; import * as EnrichMathml from './enrich_mathml.js'; import { setAttributes, Attribute } from './enrich_attr.js'; @@ -42,7 +43,7 @@ export class CaseMultiscripts extends CaseMultiindex { } const mmlTag = DomUtil.tagName(semantic.mathmlTree); return ( - mmlTag === 'MMULTISCRIPTS' && + mmlTag === MMLTAGS.MMULTISCRIPTS && (semantic.type === SemanticType.SUPERSCRIPT || semantic.type === SemanticType.SUBSCRIPT) ); diff --git a/ts/enrich_mathml/case_table.ts b/ts/enrich_mathml/case_table.ts index 27f5bb450..50a094b17 100644 --- a/ts/enrich_mathml/case_table.ts +++ b/ts/enrich_mathml/case_table.ts @@ -21,6 +21,7 @@ import * as DomUtil from '../common/dom_util.js'; import { SemanticType } from '../semantic_tree/semantic_meaning.js'; import { SemanticNode } from '../semantic_tree/semantic_node.js'; +import { MMLTAGS } from '../semantic_tree/semantic_util.js'; import { AbstractEnrichCase } from './abstract_enrich_case.js'; import * as EnrichMathml from './enrich_mathml.js'; @@ -78,7 +79,7 @@ export class CaseTable extends AbstractEnrichCase { [lfence].concat(this.inner, [rfence]), this.semantic ); - } else if (DomUtil.tagName(this.mml) === 'MFENCED') { + } else if (DomUtil.tagName(this.mml) === MMLTAGS.MFENCED) { const children = this.mml.childNodes; this.mml.insertBefore(lfence, children[0] || null); rfence && this.mml.appendChild(rfence); diff --git a/ts/enrich_mathml/enrich_mathml.ts b/ts/enrich_mathml/enrich_mathml.ts index 347be5f45..e89b4cd26 100644 --- a/ts/enrich_mathml/enrich_mathml.ts +++ b/ts/enrich_mathml/enrich_mathml.ts @@ -34,6 +34,7 @@ import { SemanticNode } from '../semantic_tree/semantic_node.js'; import { SemanticSkeleton, Sexp } from '../semantic_tree/semantic_skeleton.js'; import { SemanticTree } from '../semantic_tree/semantic_tree.js'; import * as SemanticUtil from '../semantic_tree/semantic_util.js'; +import { MMLTAGS } from '../semantic_tree/semantic_util.js'; import * as EnrichAttr from './enrich_attr.js'; import { getCase } from './enrich_case.js'; @@ -799,7 +800,7 @@ export function cloneContentNode(content: SemanticNode): Element { * @returns The rewritten element. */ export function rewriteMfenced(mml: Element): Element { - if (DomUtil.tagName(mml) !== 'MFENCED') { + if (DomUtil.tagName(mml) !== MMLTAGS.MFENCED) { return mml; } const newNode = EnrichAttr.addMrow(); diff --git a/ts/semantic_tree/semantic_heuristics.ts b/ts/semantic_tree/semantic_heuristics.ts index 32f6d07a3..617acb596 100644 --- a/ts/semantic_tree/semantic_heuristics.ts +++ b/ts/semantic_tree/semantic_heuristics.ts @@ -34,6 +34,7 @@ import * as SemanticPred from './semantic_pred.js'; import SemanticProcessor from './semantic_processor.js'; import * as SemanticUtil from './semantic_util.js'; import { SemanticSkeleton } from './semantic_skeleton.js'; +import { MMLTAGS } from '../semantic_tree/semantic_util.js'; import * as DomUtil from '../common/dom_util.js'; @@ -587,14 +588,14 @@ function eligibleNode(node: Element) { return ( node.childNodes[0] && node.childNodes[0].childNodes[0] && - DomUtil.tagName(node.childNodes[0] as Element) === 'MPADDED' && + DomUtil.tagName(node.childNodes[0] as Element) === MMLTAGS.MPADDED && DomUtil.tagName(node.childNodes[0].childNodes[0] as Element) === - 'MPADDED' && + MMLTAGS.MPADDED && DomUtil.tagName( node.childNodes[0].childNodes[ node.childNodes[0].childNodes.length - 1 ] as Element - ) === 'MPHANTOM' + ) === MMLTAGS.MPHANTOM ); } diff --git a/ts/semantic_tree/semantic_mathml.ts b/ts/semantic_tree/semantic_mathml.ts index 288b54497..8f611e731 100644 --- a/ts/semantic_tree/semantic_mathml.ts +++ b/ts/semantic_tree/semantic_mathml.ts @@ -30,6 +30,7 @@ import { SemanticAbstractParser } from './semantic_parser.js'; import * as SemanticPred from './semantic_pred.js'; import SemanticProcessor from './semantic_processor.js'; import * as SemanticUtil from './semantic_util.js'; +import { MMLTAGS } from '../semantic_tree/semantic_util.js'; export class SemanticMathml extends SemanticAbstractParser { private parseMap_: { @@ -517,7 +518,7 @@ export class SemanticMathml extends SemanticAbstractParser { let prescripts = false; let scriptcount = 0; for (let i = 0, child; (child = children[i]); i++) { - if (DomUtil.tagName(child) === 'MPRESCRIPTS') { + if (DomUtil.tagName(child) === MMLTAGS.MPRESCRIPTS) { prescripts = true; scriptcount = 0; continue; diff --git a/ts/semantic_tree/semantic_processor.ts b/ts/semantic_tree/semantic_processor.ts index 2530fda01..649176874 100644 --- a/ts/semantic_tree/semantic_processor.ts +++ b/ts/semantic_tree/semantic_processor.ts @@ -35,6 +35,7 @@ import { SemanticNode } from './semantic_node.js'; import { SemanticNodeFactory } from './semantic_node_factory.js'; import * as SemanticPred from './semantic_pred.js'; import * as SemanticUtil from './semantic_util.js'; +import { MMLTAGS } from '../semantic_tree/semantic_util.js'; interface BoundsType { type: SemanticType; @@ -391,12 +392,12 @@ export default class SemanticProcessor { * @returns The space element if it exists. */ private static getSpacer_(node: Element): Element { - if (DomUtil.tagName(node) === 'MSPACE') { + if (DomUtil.tagName(node) === MMLTAGS.MSPACE) { return node; } while (SemanticUtil.hasEmptyTag(node) && node.childNodes.length === 1) { node = node.childNodes[0] as Element; - if (DomUtil.tagName(node) === 'MSPACE') { + if (DomUtil.tagName(node) === MMLTAGS.MSPACE) { return node; } } @@ -1200,15 +1201,15 @@ export default class SemanticProcessor { public text(leaf: SemanticNode, type: string): SemanticNode { SemanticProcessor.exprFont_(leaf); leaf.type = SemanticType.TEXT; - if (type === 'ANNOTATION-XML') { + if (type === MMLTAGS.ANNOTATIONXML) { leaf.role = SemanticRole.ANNOTATION; return leaf; } - if (type === 'MS') { + if (type === MMLTAGS.MS) { leaf.role = SemanticRole.STRING; return leaf; } - if (type === 'MSPACE' || leaf.textContent.match(/^\s*$/)) { + if (type === MMLTAGS.MSPACE || leaf.textContent.match(/^\s*$/)) { leaf.role = SemanticRole.SPACE; return leaf; } @@ -1549,7 +1550,7 @@ export default class SemanticProcessor { if (!nonEmptySub && !nonEmptySup) { return base; } - const mmlTag = nonEmptySub ? (nonEmptySup ? 'MSUBSUP' : 'MSUB') : 'MSUP'; + const mmlTag = nonEmptySub ? (nonEmptySup ? MMLTAGS.MSUBSUP : MMLTAGS.MSUB) : MMLTAGS.MSUP; const mmlchild = [base]; if (nonEmptySub) { mmlchild.push( @@ -3865,8 +3866,8 @@ export default class SemanticProcessor { } for (let i = 0, node; (node = nodes[i]); i++) { const tag = DomUtil.tagName(node); - if (tag !== 'MSPACE') { - if (tag === 'MROW') { + if (tag !== MMLTAGS.MSPACE) { + if (tag === MMLTAGS.MROW) { return SemanticProcessor.getInstance().findNestedRow_( DomUtil.toArray(node.childNodes), semantic, diff --git a/ts/semantic_tree/semantic_util.ts b/ts/semantic_tree/semantic_util.ts index 9b34610e5..fc4f568f0 100644 --- a/ts/semantic_tree/semantic_util.ts +++ b/ts/semantic_tree/semantic_util.ts @@ -23,42 +23,93 @@ import * as DomUtil from '../common/dom_util.js'; import { SemanticNode } from './semantic_node.js'; +export enum MMLTAGS { + ANNOTATION = 'ANNOTATION', + ANNOTATIONXML = 'ANNOTATION-XML', + MACTION = 'MACTION', + MALIGNGROUP = 'MALIGNGROUP', + MALIGNMARK = 'MALIGNMARK', + MATH = 'MATH', + MENCLOSE = 'MENCLOSE', + MERROR = 'MERROR', + MFENCED = 'MFENCED', + MFRAC = 'MFRAC', + MGLYPH = 'MGLYPH', + MI = 'MI', + MLABELEDTR = 'MLABELEDTR', + MMULTISCRIPTS = 'MMULTISCRIPTS', + MN = 'MN', + MO = 'MO', + MOVER = 'MOVER', + MPADDED = 'MPADDED', + MPHANTOM = 'MPHANTOM', + MPRESCRIPTS = 'MPRESCRIPTS', + MROOT = 'MROOT', + MROW = 'MROW', + MS = 'MS', + MSPACE = 'MSPACE', + MSQRT = 'MSQRT', + MSTYLE = 'MSTYLE', + MSUB = 'MSUB', + MSUBSUP = 'MSUBSUP', + MSUP = 'MSUP', + MTABLE = 'MTABLE', + MTD = 'MTD', + MTEXT = 'MTEXT', + MTR = 'MTR', + MUNDER = 'MUNDER', + MUNDEROVER = 'MUNDEROVER', + NONE = 'NONE', + SEMANTICS = 'SEMANTICS', +} + + /** * List of MathML Tags that are considered to be leafs. */ -const LEAFTAGS: string[] = ['MO', 'MI', 'MN', 'MTEXT', 'MS', 'MSPACE']; +const LEAFTAGS: string[] = [ + MMLTAGS.MO, + MMLTAGS.MI, + MMLTAGS.MN, + MMLTAGS.MTEXT, + MMLTAGS.MS, + MMLTAGS.MSPACE +]; /** * List of MathML Tags that are to be ignored. */ const IGNORETAGS: string[] = [ - 'MERROR', - 'MPHANTOM', - 'MALIGNGROUP', - 'MALIGNMARK', - 'MPRESCRIPTS', - 'ANNOTATION', - 'ANNOTATION-XML' + MMLTAGS.MERROR, + MMLTAGS.MPHANTOM, + MMLTAGS.MALIGNGROUP, + MMLTAGS.MALIGNMARK, + MMLTAGS.MPRESCRIPTS, + MMLTAGS.ANNOTATION, + MMLTAGS.ANNOTATIONXML ]; /** * List of MathML Tags to be ignore if they have no children. */ const EMPTYTAGS: string[] = [ - 'MATH', - 'MROW', - 'MPADDED', - 'MACTION', - 'NONE', - 'MSTYLE', - 'SEMANTICS' + MMLTAGS.MATH, + MMLTAGS.MROW, + MMLTAGS.MPADDED, + MMLTAGS.MACTION, + MMLTAGS.NONE, + MMLTAGS.MSTYLE, + MMLTAGS.SEMANTICS ]; /** * List of MathML Tags that draw something and can therefore not be ignored if * they have no children. */ -const DISPLAYTAGS: string[] = ['MROOT', 'MSQRT']; +const DISPLAYTAGS: string[] = [ + MMLTAGS.MROOT, + MMLTAGS.MSQRT +]; /** * List of potential attributes that should be used as speech directly. @@ -72,7 +123,7 @@ const directSpeechKeys: string[] = ['aria-label', 'exact-speech', 'alt']; * @returns True if element is an math node. */ export function hasMathTag(node: Element): boolean { - return !!node && DomUtil.tagName(node) === 'MATH'; + return !!node && DomUtil.tagName(node) === MMLTAGS.MATH; } /** @@ -124,7 +175,7 @@ export function hasDisplayTag(node: Element): boolean { export function isOrphanedGlyph(node: Element): boolean { return ( !!node && - DomUtil.tagName(node) === 'MGLYPH' && + DomUtil.tagName(node) === MMLTAGS.MGLYPH && !hasLeafTag(node.parentNode as Element) ); } From 15b1ee577e4866b7d7f5aeda3ee4c06cf77c7409 Mon Sep 17 00:00:00 2001 From: zorkow Date: Sat, 2 Dec 2023 15:12:59 +0100 Subject: [PATCH 2/2] Rewrites object to map. --- ts/semantic_tree/semantic_mathml.ts | 84 ++++++++++++++------------ ts/semantic_tree/semantic_processor.ts | 24 ++++---- 2 files changed, 56 insertions(+), 52 deletions(-) diff --git a/ts/semantic_tree/semantic_mathml.ts b/ts/semantic_tree/semantic_mathml.ts index 8f611e731..053718362 100644 --- a/ts/semantic_tree/semantic_mathml.ts +++ b/ts/semantic_tree/semantic_mathml.ts @@ -33,9 +33,7 @@ import * as SemanticUtil from './semantic_util.js'; import { MMLTAGS } from '../semantic_tree/semantic_util.js'; export class SemanticMathml extends SemanticAbstractParser { - private parseMap_: { - [key: string]: (p1: Element, p2: Element[]) => SemanticNode; - }; + private parseMap_: Map SemanticNode>; /** * Get an attribute from a node and provide a default if it does not exist. It @@ -66,40 +64,39 @@ export class SemanticMathml extends SemanticAbstractParser { */ constructor() { super('MathML'); - this.parseMap_ = { - SEMANTICS: this.semantics_.bind(this), - MATH: this.rows_.bind(this), - MROW: this.rows_.bind(this), - MPADDED: this.rows_.bind(this), - MSTYLE: this.rows_.bind(this), - MFRAC: this.fraction_.bind(this), - MSUB: this.limits_.bind(this), - MSUP: this.limits_.bind(this), - MSUBSUP: this.limits_.bind(this), - MOVER: this.limits_.bind(this), - MUNDER: this.limits_.bind(this), - MUNDEROVER: this.limits_.bind(this), - MROOT: this.root_.bind(this), - MSQRT: this.sqrt_.bind(this), - MTABLE: this.table_.bind(this), - MLABELEDTR: this.tableLabeledRow_.bind(this), - MTR: this.tableRow_.bind(this), - MTD: this.tableCell_.bind(this), - MS: this.text_.bind(this), - MTEXT: this.text_.bind(this), - MSPACE: this.space_.bind(this), - 'ANNOTATION-XML': this.text_.bind(this), - MI: this.identifier_.bind(this), - MN: this.number_.bind(this), - MO: this.operator_.bind(this), - MFENCED: this.fenced_.bind(this), - MENCLOSE: this.enclosed_.bind(this), - MMULTISCRIPTS: this.multiscripts_.bind(this), - ANNOTATION: this.empty_.bind(this), - NONE: this.empty_.bind(this), - MACTION: this.action_.bind(this) - }; - + this.parseMap_ = new Map([ + [MMLTAGS.SEMANTICS, this.semantics_.bind(this)], + [MMLTAGS.MATH, this.rows_.bind(this)], + [MMLTAGS.MROW, this.rows_.bind(this)], + [MMLTAGS.MPADDED, this.rows_.bind(this)], + [MMLTAGS.MSTYLE, this.rows_.bind(this)], + [MMLTAGS.MFRAC, this.fraction_.bind(this)], + [MMLTAGS.MSUB, this.limits_.bind(this)], + [MMLTAGS.MSUP, this.limits_.bind(this)], + [MMLTAGS.MSUBSUP, this.limits_.bind(this)], + [MMLTAGS.MOVER, this.limits_.bind(this)], + [MMLTAGS.MUNDER, this.limits_.bind(this)], + [MMLTAGS.MUNDEROVER, this.limits_.bind(this)], + [MMLTAGS.MROOT, this.root_.bind(this)], + [MMLTAGS.MSQRT, this.sqrt_.bind(this)], + [MMLTAGS.MTABLE, this.table_.bind(this)], + [MMLTAGS.MLABELEDTR, this.tableLabeledRow_.bind(this)], + [MMLTAGS.MTR, this.tableRow_.bind(this)], + [MMLTAGS.MTD, this.tableCell_.bind(this)], + [MMLTAGS.MS, this.text_.bind(this)], + [MMLTAGS.MTEXT, this.text_.bind(this)], + [MMLTAGS.MSPACE, this.space_.bind(this)], + [MMLTAGS.ANNOTATIONXML, this.text_.bind(this)], + [MMLTAGS.MI, this.identifier_.bind(this)], + [MMLTAGS.MN, this.number_.bind(this)], + [MMLTAGS.MO, this.operator_.bind(this)], + [MMLTAGS.MFENCED, this.fenced_.bind(this)], + [MMLTAGS.MENCLOSE, this.enclosed_.bind(this)], + [MMLTAGS.MMULTISCRIPTS, this.multiscripts_.bind(this)], + [MMLTAGS.ANNOTATION, this.empty_.bind(this)], + [MMLTAGS.NONE, this.empty_.bind(this)], + [MMLTAGS.MACTION, this.action_.bind(this)], + ]); const meaning = { type: SemanticType.IDENTIFIER, role: SemanticRole.NUMBERSET, @@ -131,12 +128,19 @@ export class SemanticMathml extends SemanticAbstractParser { public parse(mml: Element) { SemanticProcessor.getInstance().setNodeFactory(this.getFactory()); const children = DomUtil.toArray(mml.childNodes); - const tag = DomUtil.tagName(mml); - const func = this.parseMap_[tag]; + const tag = DomUtil.tagName(mml) as MMLTAGS; + const func = this.parseMap_.get(tag); const newNode = (func ? func : this.dummy_.bind(this))(mml, children); SemanticUtil.addAttributes(newNode, mml); if ( - ['MATH', 'MROW', 'MPADDED', 'MSTYLE', 'SEMANTICS', 'MACTION'].indexOf( + [ + MMLTAGS.MATH, + MMLTAGS.MROW, + MMLTAGS.MPADDED, + MMLTAGS.MSTYLE, + MMLTAGS.SEMANTICS, + MMLTAGS.MACTION + ].indexOf( tag ) !== -1 ) { diff --git a/ts/semantic_tree/semantic_processor.ts b/ts/semantic_tree/semantic_processor.ts index 649176874..2f489324d 100644 --- a/ts/semantic_tree/semantic_processor.ts +++ b/ts/semantic_tree/semantic_processor.ts @@ -52,12 +52,12 @@ export default class SemanticProcessor { }; private static readonly MML_TO_LIMIT_: { [key: string]: BoundsType } = { - MSUB: { type: SemanticType.LIMLOWER, length: 1 }, - MUNDER: { type: SemanticType.LIMLOWER, length: 1 }, - MSUP: { type: SemanticType.LIMUPPER, length: 1 }, - MOVER: { type: SemanticType.LIMUPPER, length: 1 }, - MSUBSUP: { type: SemanticType.LIMBOTH, length: 2 }, - MUNDEROVER: { type: SemanticType.LIMBOTH, length: 2 } + [MMLTAGS.MSUB]: { type: SemanticType.LIMLOWER, length: 1 }, + [MMLTAGS.MUNDER]: { type: SemanticType.LIMLOWER, length: 1 }, + [MMLTAGS.MSUP]: { type: SemanticType.LIMUPPER, length: 1 }, + [MMLTAGS.MOVER]: { type: SemanticType.LIMUPPER, length: 1 }, + [MMLTAGS.MSUBSUP]: { type: SemanticType.LIMBOTH, length: 2 }, + [MMLTAGS.MUNDEROVER]: { type: SemanticType.LIMBOTH, length: 2 } }; /** @@ -66,12 +66,12 @@ export default class SemanticProcessor { * */ private static readonly MML_TO_BOUNDS_: { [key: string]: BoundsType } = { - MSUB: { type: SemanticType.SUBSCRIPT, length: 1, accent: false }, - MSUP: { type: SemanticType.SUPERSCRIPT, length: 1, accent: false }, - MSUBSUP: { type: SemanticType.SUBSCRIPT, length: 2, accent: false }, - MUNDER: { type: SemanticType.UNDERSCORE, length: 1, accent: true }, - MOVER: { type: SemanticType.OVERSCORE, length: 1, accent: true }, - MUNDEROVER: { type: SemanticType.UNDERSCORE, length: 2, accent: true } + [MMLTAGS.MSUB]: { type: SemanticType.SUBSCRIPT, length: 1, accent: false }, + [MMLTAGS.MSUP]: { type: SemanticType.SUPERSCRIPT, length: 1, accent: false }, + [MMLTAGS.MSUBSUP]: { type: SemanticType.SUBSCRIPT, length: 2, accent: false }, + [MMLTAGS.MUNDER]: { type: SemanticType.UNDERSCORE, length: 1, accent: true }, + [MMLTAGS.MOVER]: { type: SemanticType.OVERSCORE, length: 1, accent: true }, + [MMLTAGS.MUNDEROVER]: { type: SemanticType.UNDERSCORE, length: 2, accent: true } }; private static readonly CLASSIFY_FUNCTION_: { [key: string]: string } = {