UNPKG

2.18 kBJavaScriptView Raw
1// @flow
2import defineFunction, {ordargument} from "../defineFunction";
3import buildCommon from "../buildCommon";
4import mathMLTree from "../mathMLTree";
5import {assertNodeType} from "../parseNode";
6
7import * as html from "../buildHTML";
8import * as mml from "../buildMathML";
9
10const htmlBuilder = (group, options) => {
11 const elements = html.buildExpression(
12 group.body,
13 options.withColor(group.color),
14 false
15 );
16
17 // \color isn't supposed to affect the type of the elements it contains.
18 // To accomplish this, we wrap the results in a fragment, so the inner
19 // elements will be able to directly interact with their neighbors. For
20 // example, `\color{red}{2 +} 3` has the same spacing as `2 + 3`
21 return buildCommon.makeFragment(elements);
22};
23
24const mathmlBuilder = (group, options) => {
25 const inner = mml.buildExpression(group.body,
26 options.withColor(group.color));
27
28 const node = new mathMLTree.MathNode("mstyle", inner);
29
30 node.setAttribute("mathcolor", group.color);
31
32 return node;
33};
34
35defineFunction({
36 type: "color",
37 names: ["\\textcolor"],
38 props: {
39 numArgs: 2,
40 allowedInText: true,
41 greediness: 3,
42 argTypes: ["color", "original"],
43 },
44 handler({parser}, args) {
45 const color = assertNodeType(args[0], "color-token").color;
46 const body = args[1];
47 return {
48 type: "color",
49 mode: parser.mode,
50 color,
51 body: ordargument(body),
52 };
53 },
54 htmlBuilder,
55 mathmlBuilder,
56});
57
58defineFunction({
59 type: "color",
60 names: ["\\color"],
61 props: {
62 numArgs: 1,
63 allowedInText: true,
64 greediness: 3,
65 argTypes: ["color"],
66 },
67 handler({parser, breakOnTokenText}, args) {
68 const color = assertNodeType(args[0], "color-token").color;
69
70 // If we see a styling function, parse out the implicit body
71 const body = parser.parseExpression(true, breakOnTokenText);
72
73 return {
74 type: "color",
75 mode: parser.mode,
76 color,
77 body,
78 };
79 },
80 htmlBuilder,
81 mathmlBuilder,
82});