UNPKG

2.01 kBJavaScriptView Raw
1// @flow
2import {defineFunctionBuilders} from "../defineFunction";
3import buildCommon from "../buildCommon";
4import mathMLTree from "../mathMLTree";
5
6import * as mml from "../buildMathML";
7
8import type {ParseNode} from "../parseNode";
9
10// "mathord" and "textord" ParseNodes created in Parser.js from symbol Groups in
11// src/symbols.js.
12
13const defaultVariant: {[string]: string} = {
14 "mi": "italic",
15 "mn": "normal",
16 "mtext": "normal",
17};
18
19defineFunctionBuilders({
20 type: "mathord",
21 htmlBuilder(group, options) {
22 return buildCommon.makeOrd(group, options, "mathord");
23 },
24 mathmlBuilder(group: ParseNode<"mathord">, options) {
25 const node = new mathMLTree.MathNode(
26 "mi",
27 [mml.makeText(group.text, group.mode, options)]);
28
29 const variant = mml.getVariant(group, options) || "italic";
30 if (variant !== defaultVariant[node.type]) {
31 node.setAttribute("mathvariant", variant);
32 }
33 return node;
34 },
35});
36
37defineFunctionBuilders({
38 type: "textord",
39 htmlBuilder(group, options) {
40 return buildCommon.makeOrd(group, options, "textord");
41 },
42 mathmlBuilder(group: ParseNode<"textord">, options) {
43 const text = mml.makeText(group.text, group.mode, options);
44 const variant = mml.getVariant(group, options) || "normal";
45
46 let node;
47 if (group.mode === 'text') {
48 node = new mathMLTree.MathNode("mtext", [text]);
49 } else if (/[0-9]/.test(group.text)) {
50 // TODO(kevinb) merge adjacent <mn> nodes
51 // do it as a post processing step
52 node = new mathMLTree.MathNode("mn", [text]);
53 } else if (group.text === "\\prime") {
54 node = new mathMLTree.MathNode("mo", [text]);
55 } else {
56 node = new mathMLTree.MathNode("mi", [text]);
57 }
58 if (variant !== defaultVariant[node.type]) {
59 node.setAttribute("mathvariant", variant);
60 }
61
62 return node;
63 },
64});