MCPcopy Create free account
hub / github.com/KaTeX/KaTeX / MathNode

Class MathNode

src/mathMLTree.ts:47–146  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45 * `"mspace"`, corresponding to `<mo>` and `<mspace>` tags).
46 */
47export class MathNode implements MathDomNode {
48 type: MathNodeType;
49 attributes: Record<string, string>;
50 children: MathDomNode[];
51 classes: string[];
52
53 constructor(
54 type: MathNodeType,
55 children?: MathDomNode[],
56 classes?: string[]
57 ) {
58 this.type = type;
59 this.attributes = {};
60 this.children = children || [];
61 this.classes = classes || [];
62 }
63
64 /**
65 * Sets an attribute on a MathML node. MathML depends on attributes to convey a
66 * semantic content, so this is used heavily.
67 */
68 setAttribute(name: string, value: string) {
69 this.attributes[name] = value;
70 }
71
72 /**
73 * Gets an attribute on a MathML node.
74 */
75 getAttribute(name: string): string {
76 return this.attributes[name];
77 }
78
79 /**
80 * Converts the math node into a MathML-namespaced DOM element.
81 */
82 toNode(): Node {
83 const node = document.createElementNS(
84 "http://www.w3.org/1998/Math/MathML", this.type);
85
86 for (const [attr, value] of Object.entries(this.attributes)) {
87 node.setAttribute(attr, value);
88 }
89
90 if (this.classes.length > 0) {
91 node.className = createClass(this.classes);
92 }
93
94 for (let i = 0; i < this.children.length; i++) {
95 // Combine multiple TextNodes into one TextNode, to prevent
96 // screen readers from reading each as a separate word [#3995]
97 if (this.children[i] instanceof TextNode &&
98 this.children[i + 1] instanceof TextNode) {
99 let text = this.children[i].toText() + this.children[++i].toText();
100 while (this.children[i + 1] instanceof TextNode) {
101 text += this.children[++i].toText();
102 }
103 node.appendChild(new TextNode(text).toNode());
104 } else {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected