JavaScriptNode

Node

Python

Navigate the document tree and read node content through read-only Node handles.

Nodeclass#

A read-only handle into a Document.

Node carries navigation and accessor members but no editing methods — all edits go through the owning document. It is a cheap reference, not a copy of the subtree; reads delegate back into the document. Obtain handles from Document.root, Document.errors, Document.findCommands, the create* staging constructors, and the navigation members below.

TypeScript
const doc = new Parser().parse(String.raw`\frac{x}{y}`).document!;
const cmd = doc.root().children()[0];
cmd.kind; // 'command'
cmd.isCommand('frac'); // true

Kind#

kindattribute#

NodeKind

The node's kind, read as a property (not a method).

Its value is one of the lowercase NodeKind strings ('root', 'group', 'command', 'scripted', ...).

Predicates#

isCommand#

isCommand(name: string | null | undefined): boolean

Whether the node is a command; with name, whether it is that specific command.

ParameterTypeDescription
namestring | null | undefined
  • Optional command name to match; omit/null matches any command.
Returns
boolean —

true if the node is a (matching) command.

TypeScript
node.isCommand();       // any command
node.isCommand('frac'); // specifically \frac

isChar#

isChar(value: string | null | undefined): boolean

Whether the node is a char; with value, whether it holds that specific character.

ParameterTypeDescription
valuestring | null | undefined
  • Optional character to match; omit/null matches any char.
Returns
boolean —

true if the node is a (matching) char.

isError#

isError(): boolean

Whether the node is an Error placeholder.

Returns
boolean —

true for an Error node.

isKnown#

isKnown(): boolean | null

Whether the knowledge base knows this command or environment; null for other kinds.

Returns
boolean | null

Navigation#

parent#

parent(): Node | null

The parent Node, or null for the root.

Returns
Node | null —

The parent handle, or null.

See also

childrenattribute#

Node[]

The child Nodes, as a read-only property.

nextSibling#

nextSibling(): Node | null

The following sibling Node, or null.

Returns
Node | null —

The next sibling handle, or null.

See also

prevSibling#

prevSibling(): Node | null

The preceding sibling Node, or null.

Returns
Node | null —

The previous sibling handle, or null.

See also

ancestors#

ancestors(): Node[]

The ancestor Nodes from parent upward.

Returns
Node[] —

The ancestor handles, nearest first.

See also

descendants#

descendants(): Node[]

The descendant Nodes in document order.

Returns
Node[] —

The descendant handles.

See also

path#

path(): string | null

The node's current path from the root, or null if detached. Edits change it; Document.nodeAt resolves it.

Returns
string | null

slot#

slot(): NodeSlot | null

The slot this node fills in its parent, or null for the root and detached nodes.

Returns
NodeSlot | null

Content accessors#

commandNameattribute#

string | null

The command name for a command / declarative node, else null (read-only property).

envNameattribute#

string | null

The environment name for an environment node, else null (read-only property).

textattribute#

string | null

The string value for a text node, else null (read-only property).

charattribute#

string | null

The character for a char node, else null (read-only property).

primeCount#

primeCount(): number | null

The prime count (greater than zero) for a prime node, else null.

Returns
number | null —

The number of consecutive prime symbols, or null. Scripted determines their binding.

TypeScript
const doc = new Parser().parse(String.raw`f''`).document;
const prime = doc.root().descendants().find((n) => n.kind === 'prime');
prime.primeCount(); // 2

errorParts#

errorParts(): { message: string; snippet: string } | null

For an error node, its captured { message, snippet }, else null.

Returns
{ message: string; snippet: string } | null —

The error message and the original source snippet, or null.

TypeScript
const doc = new Parser().parse(String.raw`\sqrt[`).document;
const err = doc.root().descendants().find((n) => n.isError());
err.errorParts(); // { message: 'unclosed bracket argument', snippet: '\\sqrt[' }

contentMode#

contentMode(): RuntimeContentMode | null

The runtime content mode ("math" or "text") where applicable, else null.

Returns
RuntimeContentMode | null —

The node's content mode, or null.

Groups#

groupKind#

groupKind(): GroupKindRef | null

The GroupKindRef for a group node, else null.

The result carries a capitalized kind discriminator ('Explicit', 'Implicit', 'InlineMath', 'Delimited'). A Delimited group also carries left and right delimiter strings such as '(' or '\\langle'.

Returns
GroupKindRef | null —

The group kind view, or null.

TypeScript
const doc = new Parser().parse(String.raw`{x}`).document;
const group = doc.root().descendants().find((n) => n.kind === 'group');
group.groupKind(); // { kind: 'Explicit' }
See also

Command arguments#

argCount#

argCount(): number

The number of argument slots for a command-like node.

Returns
number —

The slot count.

arg#

arg(index: number): ArgRef | null

The argument at index, or null if the slot is empty.

ParameterTypeDescription
indexnumber
  • The zero-based slot index.
Returns
ArgRef | null —

The ArgRef for the slot, or null.

TypeScript
const doc = new Parser().parse(String.raw`\frac{x}{y}`).document;
const frac = doc.root().descendants().find((n) => n.isCommand('frac'));
frac.argCount(); // 2
frac.arg(0);     // { kind: 'Math', node: <Node> }
See also

argKind#

argKind(index: number): ArgForm | null

The delimiter form of the argument slot at index, or null if there is no such slot.

ParameterType
indexnumber
Returns
ArgForm | null

argSlots#

argSlots(): Array<ArgRef | null>

The full array of argument slots, with null for any empty slot.

Returns
Array<ArgRef | null> —

The ArgRef per slot, null where empty.

See also

Scripts and infix#

scriptBase#

scriptBase(): Node | null

The base of a scripted node, else null.

Returns
Node | null —

The base node handle, or null.

subscript#

subscript(): Node | null

The subscript slot of a scripted node, else null.

Returns
Node | null —

The subscript node handle, or null.

TypeScript
const doc = new Parser().parse(String.raw`x_i^2`).document;
const sc = doc.root().descendants().find((n) => n.kind === 'scripted');
[sc.subscript().kind, sc.superscript().kind]; // ['char', 'char']

superscript#

superscript(): Node | null

The superscript slot of a scripted node, else null.

Returns
Node | null —

The superscript node handle, or null.

infixLeft#

infixLeft(): Node | null

The left operand of an infix node, else null.

Returns
Node | null —

The left operand handle, or null.

infixRight#

infixRight(): Node | null

The right operand of an infix node, else null.

Returns
Node | null —

The right operand handle, or null.

envBody#

envBody(): Node | null

The body group of an environment node, else null.

Returns
Node | null —

The body group handle, or null.

Spans#

span#

span(): Span | null

The source byte Span for the node, or null if it has no recorded span.

Returns
Span | null —

The span, or null.

TypeScript
const doc = new Parser().parse(String.raw`a + b`).document;
doc.root().children[0].span(); // { start: 0, end: 1 }
See also

Identity#

document#

document(): Document
Returns

isSameNode#

isSameNode(other: Node): boolean
ParameterType
otherNode
Returns
boolean

Lifecycle#

free#

free(): void

Release this node handle's WASM wrapper immediately.

This does not free the owning Document. Prefer a using declaration so [Symbol.dispose]() runs at scope exit. After free(), further use of this handle is invalid. Skipping free() is safe: garbage collection still reclaims the wrapper.

See also

[Symbol.dispose]#

[Symbol.dispose](): void

Release this handle. Same as Node.free; used by using.

See also

NodeKindtype#

"root" | "group" | "command" | "infix" | "declarative" | "environment" | "scripted" | "prime" | "text" | "char" | "activeSpace" | "alignmentTab" | "error"

The runtime kind of a Node, read from the lowercase Node.kind property.

Note the casing convention: node kinds are lowercase strings ('command', 'scripted'), unlike the capitalized discriminators inside argument and group views (arg.kind === 'Math', groupKind().kind === 'Explicit').

SyntaxContentModetype#

"Math" | "Text"

Content mode as it appears in a SyntaxNode snapshot: "Math" or "Text" (capitalized snapshot form).

RuntimeContentModetype#

"math" | "text"

Content mode as reported by the runtime tree API: "math" or "text" (lowercase form used by Node.contentMode and the mode arguments of lookup methods).

GroupKindtype#

"Explicit" | "Implicit" | { Delimited: { left: Delimiter; right: Delimiter } } | "InlineMath"

The kind of a group in SyntaxNode snapshot form: an Explicit brace group, an Implicit group, a Delimited group carrying its left/right delimiters, or an InlineMath group.

GroupKindReftype#

{ kind: "Explicit" } | { kind: "Implicit" } | { kind: "Delimited"; left: string; right: string } | { kind: "InlineMath" }

The kind of a group in runtime view form, with a kind discriminator. Returned by Node.groupKind; the discriminator keeps its capitalized form ('Explicit', 'Delimited', ...). A Delimited group carries left and right delimiter strings.

Delimitertype#

"None" | { Char: string } | { Control: string }

A delimiter token in SyntaxNode snapshot form: the literal "None", a single character ({ Char }), or a control sequence ({ Control }). Runtime views use delimiter strings instead: ., one character, or a backslash-prefixed control sequence such as \langle.

See also

Argumentinterface#

A present command/environment argument in SyntaxNode snapshot form, pairing its syntactic ArgumentKind with its parsed ArgumentValue.

FieldTypeDescription
kindArgumentKind

The argument's syntactic kind (mandatory, optional, delimited, ...).

no_leading_spaceboolean | undefined

Whether the argspec slot was prefixed with !.

valueArgumentValue

The argument's parsed value.

ArgumentKindtype#

"Mandatory" | "Optional" | "Star" | "Group" | { Until: { close: Delimiter } } | { Delimited: { open: Delimiter; close: Delimiter } } | { Paired: { open: Delimiter; close: Delimiter } }

The syntactic kind of a command argument in SyntaxNode snapshot form (mandatory, optional, starred, group, until, or delimited/paired). Until forms carry close; delimited/paired forms carry open and close.

ArgumentValuetype#

{ MathContent: SyntaxNode } | { TextContent: SyntaxNode } | { OperatorNameContent: SyntaxNode } | { Delimiter: Delimiter } | { CSName: string } | { Dimension: string } | { Integer: string } | { KeyVal: string } | { Column: string } | { Boolean: boolean }

The parsed value carried by an Argument in snapshot form: math or text content, a delimiter, a control-sequence name, a dimension, an integer, a key=value list, a column spec, or a boolean flag.

ArgumentSlottype#

Argument | null | undefined

An argument slot of a Node's argument list: either a present Argument or an empty slot (null/undefined).

Optional argument slots that were not supplied in the source surface as the empty value rather than being dropped, so a slot's index stays stable.

See also

ArgReftype#

{ form: ArgForm } & ( | { kind: "Math"; node: Node } | { kind: "Text"; node: Node } | { kind: "OperatorName"; node: Node } | { kind: "Delimiter"; value: string } | { kind: "CSName"; value: string } | { kind: "Dimension"; value: string } | { kind: "Integer"; value: string } | { kind: "KeyVal"; value: string } | { kind: "Column"; value: string } | { kind: "Boolean"; value: boolean })

A command/environment argument in runtime view form, returned by Node.arg and Node.argSlots.

Content kinds (Math, Text, OperatorName) carry a live node Node handle; leaf kinds (Delimiter, CSName, Dimension, Integer, KeyVal, Column, Boolean) carry a value. Delimiter values use source strings. The kind discriminator keeps its capitalized form.

ArgFormtype#

{ kind: "mandatory" | "optional" | "star" | "group" | "until" | "delimited" | "paired"; open?: string; close?: string }

The delimiter form of an argument slot; close is present for until, and both open and close for delimited and paired forms.

NodeSlottype#

{ kind: "child" | "arg" | "script_base" | "subscript" | "superscript" | "infix_left" | "infix_right" | "env_body"; index: number | null }

The parent slot a Node fills; index is set for indexed slots such as children and arguments.

See also

Argtype#

Node | string | boolean | null | { value: Arg; open: string; close: string }

A slot value accepted by construction and editing methods: a string is the source written inside the slot's boundaries, a detached Node is content, boolean sets a star slot, null omits an optional slot, and { value, open, close } selects an explicit delimiter pair.

See also