import { CaretNode, h, t, VNode } from "@caret-js/core"; import { DecimalNode } from "./decimal"; import { MathExpressionTag } from "../tags/mathExpression"; import { CanBeAdjacentMultRightSideTag } from "../parselets/adjacentMultiplication"; import { ParenthesesChildTag } from "../parselets/parentheses"; export class NegativeNode extends CaretNode { constructor(public child: CaretNode) { super(); this.addTag(new MathExpressionTag()).addTag( new CanBeAdjacentMultRightSideTag(false) // Negative nodes cannot be on the right side of adjacent multiplication (that would just be subtraction) ); } static from(child: CaretNode): NegativeNode | DecimalNode { if ( child instanceof DecimalNode && !child.value.startsWith("-") && !child.hasTag(ParenthesesChildTag) ) { return new DecimalNode(`-${child.value}`); } return new NegativeNode(child); } toDebugMathML(): VNode { return h("mrow", {}, h("mo", {}, t("-")), this.child.toDebugMathML()); } }