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

Class SpaceNode

src/mathMLTree.ts:186–255  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

184 * depending on the width.
185 */
186export class SpaceNode implements MathDomNode {
187 width: number;
188 character: string | null | undefined;
189
190 /**
191 * Create a Space node with width given in CSS ems.
192 */
193 constructor(width: number) {
194 this.width = width;
195 // See https://www.w3.org/TR/2000/WD-MathML2-20000328/chapter6.html
196 // for a table of space-like characters. We use Unicode
197 // representations instead of &LongNames; as it's not clear how to
198 // make the latter via document.createTextNode.
199 if (width >= 0.05555 && width <= 0.05556) {
200 this.character = "\u200a"; // &VeryThinSpace;
201 } else if (width >= 0.1666 && width <= 0.1667) {
202 this.character = "\u2009"; // &ThinSpace;
203 } else if (width >= 0.2222 && width <= 0.2223) {
204 this.character = "\u2005"; // &MediumSpace;
205 } else if (width >= 0.2777 && width <= 0.2778) {
206 this.character = "\u2005\u200a"; // &ThickSpace;
207 } else if (width >= -0.05556 && width <= -0.05555) {
208 this.character = "\u200a\u2063"; // &NegativeVeryThinSpace;
209 } else if (width >= -0.1667 && width <= -0.1666) {
210 this.character = "\u2009\u2063"; // &NegativeThinSpace;
211 } else if (width >= -0.2223 && width <= -0.2222) {
212 this.character = "\u205f\u2063"; // &NegativeMediumSpace;
213 } else if (width >= -0.2778 && width <= -0.2777) {
214 this.character = "\u2005\u2063"; // &NegativeThickSpace;
215 } else {
216 this.character = null;
217 }
218 }
219
220 /**
221 * Converts the math node into a MathML-namespaced DOM element.
222 */
223 toNode(): Node {
224 if (this.character) {
225 return document.createTextNode(this.character);
226 } else {
227 const node = document.createElementNS(
228 "http://www.w3.org/1998/Math/MathML", "mspace");
229 node.setAttribute("width", makeEm(this.width));
230 return node;
231 }
232 }
233
234 /**
235 * Converts the math node into an HTML markup string.
236 */
237 toMarkup(): string {
238 if (this.character) {
239 return `<mtext>${this.character}</mtext>`;
240 } else {
241 return `<mspace width="${makeEm(this.width)}"/>`;
242 }
243 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected