forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildHTML.js
386 lines (342 loc) · 13.8 KB
/
buildHTML.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// @flow
/**
* This file does the main work of building a domTree structure from a parse
* tree. The entry point is the `buildHTML` function, which takes a parse tree.
* Then, the buildExpression, buildGroup, and various groupBuilders functions
* are called, to produce a final HTML tree.
*/
import ParseError from "./ParseError";
import Style from "./Style";
import buildCommon from "./buildCommon";
import {Anchor} from "./domTree";
import utils, {assert} from "./utils";
import {checkNodeType} from "./parseNode";
import {spacings, tightSpacings} from "./spacingData";
import {_htmlGroupBuilders as groupBuilders} from "./defineFunction";
import {DocumentFragment} from "./tree";
import type Options from "./Options";
import type {AnyParseNode} from "./parseNode";
import type {HtmlDomNode, DomSpan} from "./domTree";
const makeSpan = buildCommon.makeSpan;
// Binary atoms (first class `mbin`) change into ordinary atoms (`mord`)
// depending on their surroundings. See TeXbook pg. 442-446, Rules 5 and 6,
// and the text before Rule 19.
const isBinLeftCanceller = function(
node: ?HtmlDomNode,
isRealGroup: boolean,
): boolean {
// TODO: This code assumes that a node's math class is the first element
// of its `classes` array. A later cleanup should ensure this, for
// instance by changing the signature of `makeSpan`.
if (node) {
return utils.contains(["mbin", "mopen", "mrel", "mop", "mpunct"],
getTypeOfDomTree(node, "right"));
} else {
return isRealGroup;
}
};
const isBinRightCanceller = function(
node: ?HtmlDomNode,
isRealGroup: boolean,
): boolean {
if (node) {
return utils.contains(["mrel", "mclose", "mpunct"],
getTypeOfDomTree(node, "left"));
} else {
return isRealGroup;
}
};
const styleMap = {
"display": Style.DISPLAY,
"text": Style.TEXT,
"script": Style.SCRIPT,
"scriptscript": Style.SCRIPTSCRIPT,
};
type Side = "left" | "right";
const DomEnum = {
mord: "mord",
mop: "mop",
mbin: "mbin",
mrel: "mrel",
mopen: "mopen",
mclose: "mclose",
mpunct: "mpunct",
minner: "minner",
};
export type DomType = $Keys<typeof DomEnum>;
/**
* Take a list of nodes, build them in order, and return a list of the built
* nodes. documentFragments are flattened into their contents, so the
* returned list contains no fragments. `isRealGroup` is true if `expression`
* is a real group (no atoms will be added on either side), as opposed to
* a partial group (e.g. one created by \color). `surrounding` is an array
* consisting type of nodes that will be added to the left and right.
*/
export const buildExpression = function(
expression: AnyParseNode[],
options: Options,
isRealGroup: boolean,
surrounding: [?DomType, ?DomType] = [null, null],
): HtmlDomNode[] {
// Parse expressions into `groups`.
const rawGroups: HtmlDomNode[] = [];
for (let i = 0; i < expression.length; i++) {
const output = buildGroup(expression[i], options);
if (output instanceof DocumentFragment) {
const children: HtmlDomNode[] = output.children;
rawGroups.push(...children);
} else {
rawGroups.push(output);
}
}
// At this point `rawGroups` consists entirely of `symbolNode`s and `span`s.
// Ignore explicit spaces (e.g., \;, \,) when determining what implicit
// spacing should go between atoms of different classes, and add dummy
// spans for determining spacings between surrounding atoms.
const nonSpaces: (?HtmlDomNode)[] = [
surrounding[0] ? makeSpan([surrounding[0]], [], options) : null,
...rawGroups.filter(group => group && group.classes[0] !== "mspace"),
surrounding[1] ? makeSpan([surrounding[1]], [], options) : null,
];
// Before determining what spaces to insert, perform bin cancellation.
// Binary operators change to ordinary symbols in some contexts.
for (let i = 1; i < nonSpaces.length - 1; i++) {
const nonSpacesI: HtmlDomNode = assert(nonSpaces[i]);
const left = getOutermostNode(nonSpacesI, "left");
if (left.classes[0] === "mbin" &&
isBinLeftCanceller(nonSpaces[i - 1], isRealGroup)) {
left.classes[0] = "mord";
}
const right = getOutermostNode(nonSpacesI, "right");
if (right.classes[0] === "mbin" &&
isBinRightCanceller(nonSpaces[i + 1], isRealGroup)) {
right.classes[0] = "mord";
}
}
const groups = [];
let j = 0;
for (let i = 0; i < rawGroups.length; i++) {
groups.push(rawGroups[i]);
// For any group that is not a space, get the next non-space. Then
// lookup what implicit space should be placed between those atoms and
// add it to groups.
if (rawGroups[i].classes[0] !== "mspace" && j < nonSpaces.length - 1) {
// if current non-space node is left dummy span, add a glue before
// first real non-space node
if (j === 0) {
groups.pop();
i--;
}
// Get the type of the current non-space node. If it's a document
// fragment, get the type of the rightmost node in the fragment.
const left = getTypeOfDomTree(nonSpaces[j], "right");
// Get the type of the next non-space node. If it's a document
// fragment, get the type of the leftmost node in the fragment.
const right = getTypeOfDomTree(nonSpaces[j + 1], "left");
// We use buildExpression inside of sizingGroup, but it returns a
// document fragment of elements. sizingGroup sets `isRealGroup`
// to false to avoid processing spans multiple times.
if (left && right && isRealGroup) {
const nonSpacesJp1: HtmlDomNode = assert(nonSpaces[j + 1]);
const space = isLeftTight(nonSpacesJp1)
? tightSpacings[left][right]
: spacings[left][right];
if (space) {
let glueOptions = options;
if (expression.length === 1) {
const node =
checkNodeType(expression[0], "sizing") ||
checkNodeType(expression[0], "styling");
if (!node) {
// No match.
} else if (node.type === "sizing") {
glueOptions = options.havingSize(node.size);
} else if (node.type === "styling") {
glueOptions = options.havingStyle(styleMap[node.style]);
}
}
groups.push(buildCommon.makeGlue(space, glueOptions));
}
}
j++;
}
}
return groups;
};
// Return the outermost node of a domTree.
const getOutermostNode = function(
node: HtmlDomNode,
side: Side,
): HtmlDomNode {
if (node instanceof DocumentFragment ||
node instanceof Anchor) {
const children = node.children;
if (children.length) {
if (side === "right") {
return getOutermostNode(children[children.length - 1], "right");
} else if (side === "left") {
return getOutermostNode(children[0], "right");
}
}
}
return node;
};
// Return math atom class (mclass) of a domTree.
export const getTypeOfDomTree = function(
node: ?HtmlDomNode,
side: Side,
): ?DomType {
if (!node) {
return null;
}
node = getOutermostNode(node, side);
// This makes a lot of assumptions as to where the type of atom
// appears. We should do a better job of enforcing this.
return DomEnum[node.classes[0]] || null;
};
// If `node` is an atom return whether it's been assigned the mtight class.
// If `node` is a document fragment, return the value of isLeftTight() for the
// leftmost node in the fragment.
// 'mtight' indicates that the node is script or scriptscript style.
export const isLeftTight = function(node: HtmlDomNode): boolean {
node = getOutermostNode(node, "left");
return node.hasClass("mtight");
};
export const makeNullDelimiter = function(
options: Options,
classes: string[],
): DomSpan {
const moreClasses = ["nulldelimiter"].concat(options.baseSizingClasses());
return makeSpan(classes.concat(moreClasses));
};
/**
* buildGroup is the function that takes a group and calls the correct groupType
* function for it. It also handles the interaction of size and style changes
* between parents and children.
*/
export const buildGroup = function(
group: ?AnyParseNode,
options: Options,
baseOptions?: Options,
): HtmlDomNode {
if (!group) {
return makeSpan();
}
if (groupBuilders[group.type]) {
// Call the groupBuilders function
// $FlowFixMe
let groupNode: HtmlDomNode = groupBuilders[group.type](group, options);
// If the size changed between the parent and the current group, account
// for that size difference.
if (baseOptions && options.size !== baseOptions.size) {
groupNode = makeSpan(options.sizingClasses(baseOptions),
[groupNode], options);
const multiplier =
options.sizeMultiplier / baseOptions.sizeMultiplier;
groupNode.height *= multiplier;
groupNode.depth *= multiplier;
}
return groupNode;
} else {
throw new ParseError(
"Got group of unknown type: '" + group.type + "'");
}
};
/**
* Combine an array of HTML DOM nodes (e.g., the output of `buildExpression`)
* into an unbreakable HTML node of class .base, with proper struts to
* guarantee correct vertical extent. `buildHTML` calls this repeatedly to
* make up the entire expression as a sequence of unbreakable units.
*/
function buildHTMLUnbreakable(children, options) {
// Compute height and depth of this chunk.
const body = makeSpan(["base"], children, options);
// Add strut, which ensures that the top of the HTML element falls at
// the height of the expression, and the bottom of the HTML element
// falls at the depth of the expression.
// We used to have separate top and bottom struts, where the bottom strut
// would like to use `vertical-align: top`, but in IE 9 this lowers the
// baseline of the box to the bottom of this strut (instead of staying in
// the normal place) so we use an absolute value for vertical-align instead.
const strut = makeSpan(["strut"]);
strut.style.height = (body.height + body.depth) + "em";
strut.style.verticalAlign = -body.depth + "em";
body.children.unshift(strut);
return body;
}
/**
* Take an entire parse tree, and build it into an appropriate set of HTML
* nodes.
*/
export default function buildHTML(tree: AnyParseNode[], options: Options): DomSpan {
// Strip off outer tag wrapper for processing below.
let tag = null;
if (tree.length === 1 && tree[0].type === "tag") {
tag = tree[0].tag;
tree = tree[0].body;
}
// Build the expression contained in the tree
const expression = buildExpression(tree, options, true);
const children = [];
// Create one base node for each chunk between potential line breaks.
// The TeXBook [p.173] says "A formula will be broken only after a
// relation symbol like $=$ or $<$ or $\rightarrow$, or after a binary
// operation symbol like $+$ or $-$ or $\times$, where the relation or
// binary operation is on the ``outer level'' of the formula (i.e., not
// enclosed in {...} and not part of an \over construction)."
let parts = [];
for (let i = 0; i < expression.length; i++) {
parts.push(expression[i]);
if (expression[i].hasClass("mbin") ||
expression[i].hasClass("mrel") ||
expression[i].hasClass("allowbreak")) {
// Put any post-operator glue on same line as operator.
// Watch for \nobreak along the way.
let nobreak = false;
while (i < expression.length - 1 &&
expression[i + 1].hasClass("mspace")) {
i++;
parts.push(expression[i]);
if (expression[i].hasClass("nobreak")) {
nobreak = true;
}
}
// Don't allow break if \nobreak among the post-operator glue.
if (!nobreak) {
children.push(buildHTMLUnbreakable(parts, options));
parts = [];
}
} else if (expression[i].hasClass("newline")) {
// Write the line except the newline
parts.pop();
if (parts.length > 0) {
children.push(buildHTMLUnbreakable(parts, options));
parts = [];
}
// Put the newline at the top level
children.push(expression[i]);
}
}
if (parts.length > 0) {
children.push(buildHTMLUnbreakable(parts, options));
}
// Now, if there was a tag, build it too and append it as a final child.
let tagChild;
if (tag) {
tagChild = buildHTMLUnbreakable(
buildExpression(tag, options, true)
);
tagChild.classes = ["tag"];
children.push(tagChild);
}
const htmlNode = makeSpan(["katex-html"], children);
htmlNode.setAttribute("aria-hidden", "true");
// Adjust the strut of the tag to be the maximum height of all children
// (the height of the enclosing htmlNode) for proper vertical alignment.
if (tagChild) {
const strut = tagChild.children[0];
strut.style.height = (htmlNode.height + htmlNode.depth) + "em";
strut.style.verticalAlign = (-htmlNode.depth) + "em";
}
return htmlNode;
}