UNPKG

2.69 kBJavaScriptView Raw
1// @flow
2import {defineFunctionBuilders} from "../defineFunction";
3import buildCommon from "../buildCommon";
4import mathMLTree from "../mathMLTree";
5import ParseError from "../ParseError";
6
7// A map of CSS-based spacing functions to their CSS class.
8const cssSpace: {[string]: string} = {
9 "\\nobreak": "nobreak",
10 "\\allowbreak": "allowbreak",
11};
12
13// A lookup table to determine whether a spacing function/symbol should be
14// treated like a regular space character. If a symbol or command is a key
15// in this table, then it should be a regular space character. Furthermore,
16// the associated value may have a `className` specifying an extra CSS class
17// to add to the created `span`.
18const regularSpace: {[string]: { className?: string }} = {
19 " ": {},
20 "\\ ": {},
21 "~": {
22 className: "nobreak",
23 },
24 "\\space": {},
25 "\\nobreakspace": {
26 className: "nobreak",
27 },
28};
29
30// ParseNode<"spacing"> created in Parser.js from the "spacing" symbol Groups in
31// src/symbols.js.
32defineFunctionBuilders({
33 type: "spacing",
34 htmlBuilder(group, options) {
35 if (regularSpace.hasOwnProperty(group.text)) {
36 const className = regularSpace[group.text].className || "";
37 // Spaces are generated by adding an actual space. Each of these
38 // things has an entry in the symbols table, so these will be turned
39 // into appropriate outputs.
40 if (group.mode === "text") {
41 const ord = buildCommon.makeOrd(group, options, "textord");
42 ord.classes.push(className);
43 return ord;
44 } else {
45 return buildCommon.makeSpan(["mspace", className],
46 [buildCommon.mathsym(group.text, group.mode, options)],
47 options);
48 }
49 } else if (cssSpace.hasOwnProperty(group.text)) {
50 // Spaces based on just a CSS class.
51 return buildCommon.makeSpan(
52 ["mspace", cssSpace[group.text]],
53 [], options);
54 } else {
55 throw new ParseError(`Unknown type of space "${group.text}"`);
56 }
57 },
58 mathmlBuilder(group, options) {
59 let node;
60
61 if (regularSpace.hasOwnProperty(group.text)) {
62 node = new mathMLTree.MathNode(
63 "mtext", [new mathMLTree.TextNode("\u00a0")]);
64 } else if (cssSpace.hasOwnProperty(group.text)) {
65 // CSS-based MathML spaces (\nobreak, \allowbreak) are ignored
66 return new mathMLTree.MathNode("mspace");
67 } else {
68 throw new ParseError(`Unknown type of space "${group.text}"`);
69 }
70
71 return node;
72 },
73});