UNPKG

2.49 kBJavaScriptView Raw
1//@flow
2// Row breaks within tabular environments, and line breaks at top level
3
4import defineFunction from "../defineFunction";
5import buildCommon from "../buildCommon";
6import mathMLTree from "../mathMLTree";
7import {calculateSize} from "../units";
8import ParseError from "../ParseError";
9import {assertNodeType} from "../parseNode";
10
11// \\ is a macro mapping to either \cr or \newline. Because they have the
12// same signature, we implement them as one megafunction, with newRow
13// indicating whether we're in the \cr case, and newLine indicating whether
14// to break the line in the \newline case.
15
16defineFunction({
17 type: "cr",
18 names: ["\\cr", "\\newline"],
19 props: {
20 numArgs: 0,
21 numOptionalArgs: 1,
22 argTypes: ["size"],
23 allowedInText: true,
24 },
25
26 handler({parser, funcName}, args, optArgs) {
27 const size = optArgs[0];
28 const newRow = (funcName === "\\cr");
29 let newLine = false;
30 if (!newRow) {
31 if (parser.settings.displayMode &&
32 parser.settings.useStrictBehavior(
33 "newLineInDisplayMode", "In LaTeX, \\\\ or \\newline " +
34 "does nothing in display mode")) {
35 newLine = false;
36 } else {
37 newLine = true;
38 }
39 }
40 return {
41 type: "cr",
42 mode: parser.mode,
43 newLine,
44 newRow,
45 size: size && assertNodeType(size, "size").value,
46 };
47 },
48
49 // The following builders are called only at the top level,
50 // not within tabular/array environments.
51
52 htmlBuilder(group, options) {
53 if (group.newRow) {
54 throw new ParseError(
55 "\\cr valid only within a tabular/array environment");
56 }
57 const span = buildCommon.makeSpan(["mspace"], [], options);
58 if (group.newLine) {
59 span.classes.push("newline");
60 if (group.size) {
61 span.style.marginTop =
62 calculateSize(group.size, options) + "em";
63 }
64 }
65 return span;
66 },
67
68 mathmlBuilder(group, options) {
69 const node = new mathMLTree.MathNode("mspace");
70 if (group.newLine) {
71 node.setAttribute("linebreak", "newline");
72 if (group.size) {
73 node.setAttribute("height",
74 calculateSize(group.size, options) + "em");
75 }
76 }
77 return node;
78 },
79});