UNPKG

3.51 kBJavaScriptView Raw
1// @flow
2import defineFunction, {ordargument} from "../defineFunction";
3import buildCommon from "../buildCommon";
4import mathMLTree from "../mathMLTree";
5
6import * as html from "../buildHTML";
7import * as mml from "../buildMathML";
8
9defineFunction({
10 type: "phantom",
11 names: ["\\phantom"],
12 props: {
13 numArgs: 1,
14 allowedInText: true,
15 },
16 handler: ({parser}, args) => {
17 const body = args[0];
18 return {
19 type: "phantom",
20 mode: parser.mode,
21 body: ordargument(body),
22 };
23 },
24 htmlBuilder: (group, options) => {
25 const elements = html.buildExpression(
26 group.body,
27 options.withPhantom(),
28 false
29 );
30
31 // \phantom isn't supposed to affect the elements it contains.
32 // See "color" for more details.
33 return buildCommon.makeFragment(elements);
34 },
35 mathmlBuilder: (group, options) => {
36 const inner = mml.buildExpression(group.body, options);
37 return new mathMLTree.MathNode("mphantom", inner);
38 },
39});
40
41defineFunction({
42 type: "hphantom",
43 names: ["\\hphantom"],
44 props: {
45 numArgs: 1,
46 allowedInText: true,
47 },
48 handler: ({parser}, args) => {
49 const body = args[0];
50 return {
51 type: "hphantom",
52 mode: parser.mode,
53 body,
54 };
55 },
56 htmlBuilder: (group, options) => {
57 let node = buildCommon.makeSpan(
58 [], [html.buildGroup(group.body, options.withPhantom())]);
59 node.height = 0;
60 node.depth = 0;
61 if (node.children) {
62 for (let i = 0; i < node.children.length; i++) {
63 node.children[i].height = 0;
64 node.children[i].depth = 0;
65 }
66 }
67
68 // See smash for comment re: use of makeVList
69 node = buildCommon.makeVList({
70 positionType: "firstBaseline",
71 children: [{type: "elem", elem: node}],
72 }, options);
73
74 // For spacing, TeX treats \smash as a math group (same spacing as ord).
75 return buildCommon.makeSpan(["mord"], [node], options);
76 },
77 mathmlBuilder: (group, options) => {
78 const inner = mml.buildExpression(ordargument(group.body), options);
79 const phantom = new mathMLTree.MathNode("mphantom", inner);
80 const node = new mathMLTree.MathNode("mpadded", [phantom]);
81 node.setAttribute("height", "0px");
82 node.setAttribute("depth", "0px");
83 return node;
84 },
85});
86
87defineFunction({
88 type: "vphantom",
89 names: ["\\vphantom"],
90 props: {
91 numArgs: 1,
92 allowedInText: true,
93 },
94 handler: ({parser}, args) => {
95 const body = args[0];
96 return {
97 type: "vphantom",
98 mode: parser.mode,
99 body,
100 };
101 },
102 htmlBuilder: (group, options) => {
103 const inner = buildCommon.makeSpan(
104 ["inner"],
105 [html.buildGroup(group.body, options.withPhantom())]);
106 const fix = buildCommon.makeSpan(["fix"], []);
107 return buildCommon.makeSpan(
108 ["mord", "rlap"], [inner, fix], options);
109 },
110 mathmlBuilder: (group, options) => {
111 const inner = mml.buildExpression(ordargument(group.body), options);
112 const phantom = new mathMLTree.MathNode("mphantom", inner);
113 const node = new mathMLTree.MathNode("mpadded", [phantom]);
114 node.setAttribute("width", "0px");
115 return node;
116 },
117});