| 45 | * `"mspace"`, corresponding to `<mo>` and `<mspace>` tags). |
| 46 | */ |
| 47 | export 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 in this.attributes) { |
| 87 | if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) { |
| 88 | node.setAttribute(attr, this.attributes[attr]); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (this.classes.length > 0) { |
| 93 | node.className = createClass(this.classes); |
| 94 | } |
| 95 | |
| 96 | for (let i = 0; i < this.children.length; i++) { |
| 97 | // Combine multiple TextNodes into one TextNode, to prevent |
| 98 | // screen readers from reading each as a separate word [#3995] |
| 99 | if (this.children[i] instanceof TextNode && |
| 100 | this.children[i + 1] instanceof TextNode) { |
| 101 | let text = this.children[i].toText() + this.children[++i].toText(); |
| 102 | while (this.children[i + 1] instanceof TextNode) { |
| 103 | text += this.children[++i].toText(); |
| 104 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…