Skip to content

Type Editor


Type Editor / @type-editor/model / elements/Node / Node

Class: Node

Defined in: packages/model/src/elements/Node.ts:33

This class represents a node in the tree that makes up a ProseMirror document. So a document is an instance of Node, with children that are also instances of Node.

Nodes are persistent data structures. Instead of changing them, you create new ones with the content you want. Old ones keep pointing at the old document shape. This is made cheaper by sharing structure between the old and new data as much as possible, which a tree shape like this (without back pointers) makes easy.

Do not directly mutate the properties of a Node object. See the guide for more information.

Extended by

Implements

Constructors

Constructor

ts
new Node(
   type?,
   attrs?,
   content?,
   marks?,
   text?): Node;

Defined in: packages/model/src/elements/Node.ts:61

Create a node. For most use cases, you should use NodeType.create or Schema's node() method instead of calling this directly.

Parameters

ParameterTypeDescription
type?NodeTypeThe type of node that this is.
attrs?AttrsAn object mapping attribute names to values. The kind of attributes allowed and required are determined by the node type.
content?FragmentA fragment holding the node's children. If null or undefined, defaults to an empty fragment.
marks?readonly Mark[]The marks (things like whether it is emphasized or part of a link) applied to this node. Defaults to an empty mark set.
text?stringFor text nodes, this contains the node's text content.

Returns

Node

Properties

PropertyModifierTypeDefault valueDescriptionDefined in
_attrsreadonlyAttrsundefined-packages/model/src/elements/Node.ts:39
_contentreadonlyFragmentundefinedA container holding the node's children.packages/model/src/elements/Node.ts:38
_marksreadonlyreadonly Mark[]undefined-packages/model/src/elements/Node.ts:40
_tagprotectedRecord<string, number>{}-packages/model/src/elements/Node.ts:42
_textreadonlystringundefinedFor text nodes, this contains the node's text content.packages/model/src/elements/Node.ts:46
nodeTypereadonlyNodeTypeundefined-packages/model/src/elements/Node.ts:41

Accessors

attrs

Get Signature

ts
get attrs(): Attrs;

Defined in: packages/model/src/elements/Node.ts:115

An object mapping attribute names to values.

Returns

Attrs


childCount

Get Signature

ts
get childCount(): number;

Defined in: packages/model/src/elements/Node.ts:147

The number of children that the node has.

Returns

number


children

Get Signature

ts
get children(): readonly Node[];

Defined in: packages/model/src/elements/Node.ts:129

The array of this node's child nodes.

Returns

readonly Node[]


content

Get Signature

ts
get content(): Fragment;

Defined in: packages/model/src/elements/Node.ts:94

A fragment containing the node's children.

Returns

Fragment


elementType

Get Signature

ts
get elementType(): ElementType;

Defined in: packages/model/src/elements/Node.ts:87

Returns

ElementType

Implementation of

PmElement.elementType


firstChild

Get Signature

ts
get firstChild(): Node;

Defined in: packages/model/src/elements/Node.ts:165

Returns this node's first child, or null if there are no children.

Returns

Node


inlineContent

Get Signature

ts
get inlineContent(): boolean;

Defined in: packages/model/src/elements/Node.ts:195

True when this node allows inline content.

Returns

boolean


isAtom

Get Signature

ts
get isAtom(): boolean;

Defined in: packages/model/src/elements/Node.ts:228

True when this is an atom, i.e. when it does not have directly editable content. This is usually the same as isLeaf, but can be configured with the atom property on a node's spec (typically used when the node is displayed as an uneditable node view).

Returns

boolean


isBlock

Get Signature

ts
get isBlock(): boolean;

Defined in: packages/model/src/elements/Node.ts:180

True when this is a block (non-inline node)

Returns

boolean


isInline

Get Signature

ts
get isInline(): boolean;

Defined in: packages/model/src/elements/Node.ts:203

True when this is an inline node (a text node or a node that can appear among text).

Returns

boolean


isLeaf

Get Signature

ts
get isLeaf(): boolean;

Defined in: packages/model/src/elements/Node.ts:217

True when this is a leaf node.

Returns

boolean


isText

Get Signature

ts
get isText(): boolean;

Defined in: packages/model/src/elements/Node.ts:210

True when this is a text node.

Returns

boolean


isTextblock

Get Signature

ts
get isTextblock(): boolean;

Defined in: packages/model/src/elements/Node.ts:188

True when this is a textblock node, a block node with inline content.

Returns

boolean


lastChild

Get Signature

ts
get lastChild(): Node;

Defined in: packages/model/src/elements/Node.ts:173

Returns this node's last child, or null if there are no children.

Returns

Node


marks

Get Signature

ts
get marks(): readonly Mark[];

Defined in: packages/model/src/elements/Node.ts:122

The marks applied to this node.

Returns

readonly Mark[]


nodeSize

Get Signature

ts
get nodeSize(): number;

Defined in: packages/model/src/elements/Node.ts:140

The size of this node, as defined by the integer-based indexing scheme. For text nodes, this is the amount of characters. For other leaf nodes, it is one. For non-leaf nodes, it is the size of the content plus two (the start and end token).

Returns

number


tag

Get Signature

ts
get tag(): Record<string, number>;

Defined in: packages/model/src/elements/Node.ts:79

Returns

Record<string, number>

Set Signature

ts
set tag(tag): void;

Defined in: packages/model/src/elements/Node.ts:83

Parameters
ParameterType
tagRecord<string, number>
Returns

void


text

Get Signature

ts
get text(): string;

Defined in: packages/model/src/elements/Node.ts:101

For text nodes, this contains the node's text content. For other nodes, returns null.

Returns

string


textContent

Get Signature

ts
get textContent(): string;

Defined in: packages/model/src/elements/Node.ts:155

Concatenates all the text nodes found in this node and its children.

Returns

string


type

Get Signature

ts
get type(): NodeType;

Defined in: packages/model/src/elements/Node.ts:108

The type of node that this is.

Returns

NodeType

Methods

canAppend()

ts
canAppend(other): boolean;

Defined in: packages/model/src/elements/Node.ts:747

Test whether the given node's content could be appended to this node. If that node is empty, this will only return true if there is at least one node type that can appear in both nodes (to avoid merging completely incompatible nodes).

Parameters

ParameterTypeDescription
otherNodeThe node whose content to test for appending.

Returns

boolean

true if the content can be appended, false otherwise.


canReplace()

ts
canReplace(
   from,
   to,
   replacement?,
   start?,
   end?): boolean;

Defined in: packages/model/src/elements/Node.ts:696

Test whether replacing the range between from and to (by child index) with the given replacement fragment (which defaults to the empty fragment) would leave the node's content valid. You can optionally pass start and end indices into the replacement fragment.

Parameters

ParameterTypeDefault valueDescription
fromnumberundefinedThe starting child index.
tonumberundefinedThe ending child index.
replacementFragmentFragment.emptyThe replacement fragment. Defaults to an empty fragment.
startnumber0The start index in the replacement fragment. Defaults to 0.
endnumberreplacement.childCountThe end index in the replacement fragment. Defaults to the fragment's child count.

Returns

boolean

true if the replacement would be valid, false otherwise.


canReplaceWith()

ts
canReplaceWith(
   from,
   to,
   type,
   marks?): boolean;

Defined in: packages/model/src/elements/Node.ts:725

Test whether replacing the range from to to (by index) with a node of the given type would leave the node's content valid.

Parameters

ParameterTypeDescription
fromnumberThe starting child index.
tonumberThe ending child index.
typeNodeTypeThe node type to test.
marks?readonly Mark[]Optional marks to apply to the node.

Returns

boolean

true if the replacement would be valid, false otherwise.


check()

ts
check(): void;

Defined in: packages/model/src/elements/Node.ts:761

Check whether this node and its descendants conform to the schema, and raise an exception when they do not.

Returns

void

Throws

If the node or its descendants don't conform to the schema.


child()

ts
child(index): Node;

Defined in: packages/model/src/elements/Node.ts:304

Get the child node at the given index. Raises an error when the index is out of range.

Parameters

ParameterTypeDescription
indexnumberThe index of the child node to retrieve (0-based).

Returns

Node

The child node at the specified index.

Throws

If the index is out of range.


childAfter()

ts
childAfter(pos): {
  index: number;
  node: Node;
  offset: number;
};

Defined in: packages/model/src/elements/Node.ts:581

Find the (direct) child node after the given offset, if any, and return it along with its index and offset relative to this node.

Parameters

ParameterTypeDescription
posnumberThe position to search from.

Returns

ts
{
  index: number;
  node: Node;
  offset: number;
}

An object containing the node, its index, and its offset.

NameTypeDefined in
indexnumberpackages/model/src/elements/Node.ts:581
nodeNodepackages/model/src/elements/Node.ts:581
offsetnumberpackages/model/src/elements/Node.ts:581

childBefore()

ts
childBefore(pos): {
  index: number;
  node: Node;
  offset: number;
};

Defined in: packages/model/src/elements/Node.ts:594

Find the (direct) child node before the given offset, if any, and return it along with its index and offset relative to this node.

Parameters

ParameterTypeDescription
posnumberThe position to search from.

Returns

ts
{
  index: number;
  node: Node;
  offset: number;
}

An object containing the node, its index, and its offset.

NameTypeDefined in
indexnumberpackages/model/src/elements/Node.ts:594
nodeNodepackages/model/src/elements/Node.ts:594
offsetnumberpackages/model/src/elements/Node.ts:594

contentMatchAt()

ts
contentMatchAt(index): ContentMatch;

Defined in: packages/model/src/elements/Node.ts:674

Get the content match in this node at the given index.

Parameters

ParameterTypeDescription
indexnumberThe child index to get the content match at.

Returns

ContentMatch

The content match at the specified index.

Throws

If the node has invalid content.


copy()

ts
copy(content?): Node;

Defined in: packages/model/src/elements/Node.ts:471

Create a new node with the same markup as this node, containing the given content (or empty, if no content is given).

Parameters

ParameterTypeDefault valueDescription
contentFragmentnullThe content for the new node. If null, creates an empty node.

Returns

Node

A new node with the same markup but different content, or this node if content is unchanged.


cut()

ts
cut(from, to?): Node;

Defined in: packages/model/src/elements/Node.ts:498

Create a copy of this node with only the content between the given positions. If to is not given, it defaults to the end of the node.

Parameters

ParameterTypeDescription
fromnumberThe starting position.
tonumberThe ending position. Defaults to the end of the node's content.

Returns

Node

A new node containing only the specified content range.


descendants()

ts
descendants(callbackFunc): void;

Defined in: packages/model/src/elements/Node.ts:366

Call the given callback for every descendant node. Doesn't descend into a node when the callback returns false.

Parameters

ParameterTypeDescription
callbackFunc(node, pos, parent, index) => boolean | voidThe callback function to invoke for each descendant node. Return false to skip recursing into a node's children.

Returns

void


eq()

ts
eq(other): boolean;

Defined in: packages/model/src/elements/Node.ts:400

Test whether two nodes represent the same piece of document.

Parameters

ParameterTypeDescription
otherNodeThe node to compare with this node.

Returns

boolean

true if the nodes are equal, false otherwise.


forEach()

ts
forEach(callbackFunc): void;

Defined in: packages/model/src/elements/Node.ts:325

Call f for every child node, passing the node, it's offset into this parent node, and its index.

Parameters

ParameterTypeDescription
callbackFunc(node, offset, index) => voidThe callback function to invoke for each child node. Receives the node, its offset position, and its index.

Returns

void


hasMarkup()

ts
hasMarkup(
   type,
   attrs?,
   marks?): boolean;

Defined in: packages/model/src/elements/Node.ts:424

Check whether this node's markup correspond to the given type, attributes, and marks.

Parameters

ParameterTypeDescription
typeNodeTypeThe node type to check against.
attrs?Readonly<Record<string, any>>The attributes to check against (defaults to type's default attributes).
marks?readonly Mark[]The marks to check against (defaults to no marks).

Returns

boolean

true if the markup matches, false otherwise.


mark()

ts
mark(marks): Node;

Defined in: packages/model/src/elements/Node.ts:485

Create a copy of this node, with the given set of marks instead of the node's own marks.

Parameters

ParameterTypeDescription
marksreadonly Mark[]The marks to apply to the new node.

Returns

Node

A new node with the specified marks, or this node if marks are unchanged.


maybeChild()

ts
maybeChild(index): Node;

Defined in: packages/model/src/elements/Node.ts:314

Get the child node at the given index, if it exists.

Parameters

ParameterTypeDescription
indexnumberThe index of the child node to retrieve (0-based).

Returns

Node

The child node at the specified index, or null if the index is out of range.


nodeAt()

ts
nodeAt(pos?): Node;

Defined in: packages/model/src/elements/Node.ts:553

Find the node directly after the given position.

Parameters

ParameterTypeDescription
pos?numberThe position to search from.

Returns

Node

The node at the specified position, or null if no node is found.


nodesBetween()

ts
nodesBetween(
   from,
   to,
   callbackFunc,
   startPos?): void;

Defined in: packages/model/src/elements/Node.ts:348

Invoke a callback for all descendant nodes recursively overlapping the given two positions that are relative to start of this node's content. This includes all ancestors of the nodes containing the two positions. The callback is invoked with the node, its position relative to the original node (method receiver), its parent node, and its child index. When the callback returns false for a given node, that node's children will not be recursed over. The last parameter can be used to specify a starting position to count from.

Parameters

ParameterTypeDefault valueDescription
fromnumberundefinedThe starting position (inclusive).
tonumberundefinedThe ending position (exclusive).
callbackFunc(node, pos, parent, index) => boolean | voidundefinedThe callback function to invoke for each node. Return false to skip recursing into a node's children.
startPosnumber0The starting position offset for counting. Defaults to 0.

Returns

void


rangeHasMark()

ts
rangeHasMark(
   from,
   to,
   type): boolean;

Defined in: packages/model/src/elements/Node.ts:638

Test whether a given mark or mark type occurs in this document between the two given positions.

Parameters

ParameterTypeDescription
fromnumberThe starting position.
tonumberThe ending position.
type| Mark | MarkTypeThe mark or mark type to search for.

Returns

boolean

true if the mark is found in the range, false otherwise.


replace()

ts
replace(
   from,
   to,
   slice): Node;

Defined in: packages/model/src/elements/Node.ts:543

Replace the part of the document between the given positions with the given slice. The slice must 'fit', meaning its open sides must be able to connect to the surrounding content, and its content nodes must be valid children for the node they are placed into. If any of this is violated, an error of type ReplaceError is thrown.

Parameters

ParameterTypeDescription
fromnumberThe starting position of the range to replace.
tonumberThe ending position of the range to replace.
sliceSliceThe slice to insert at the specified position.

Returns

Node

A new node with the replacement applied.

Throws

If the slice doesn't fit in the specified location.


resolve()

ts
resolve(pos): ResolvedPos;

Defined in: packages/model/src/elements/Node.ts:615

Resolve the given position in the document, returning an object with information about its context.

Parameters

ParameterTypeDescription
posnumberThe position to resolve.

Returns

ResolvedPos

A ResolvedPos object with information about the position's context.


resolveNoCache()

ts
resolveNoCache(pos): ResolvedPos;

Defined in: packages/model/src/elements/Node.ts:625

Resolve the given position without using the cache.

Parameters

ParameterTypeDescription
posnumberThe position to resolve.

Returns

ResolvedPos

A ResolvedPos object with information about the position's context.


sameMarkup()

ts
sameMarkup(other): boolean;

Defined in: packages/model/src/elements/Node.ts:411

Compare the markup (type, attributes, and marks) of this node to those of another. Returns true if both have the same markup.

Parameters

ParameterTypeDescription
otherNodeThe node to compare markup with.

Returns

boolean

true if both nodes have the same markup, false otherwise.


slice()

ts
slice(
   from,
   to?,
   includeParents?): Slice;

Defined in: packages/model/src/elements/Node.ts:514

Cut out the part of the document between the given positions, and return it as a Slice object.

Parameters

ParameterTypeDefault valueDescription
fromnumberundefinedThe starting position.
tonumber...The ending position. Defaults to the end of the node's content.
includeParentsbooleanfalseWhether to include parent nodes in the slice. Defaults to false.

Returns

Slice

A Slice object representing the content between the positions.


textBetween()

ts
textBetween(
   from,
   to,
   blockSeparator?,
   leafText?): string;

Defined in: packages/model/src/elements/Node.ts:387

Get all text between positions from and to. When blockSeparator is given, it will be inserted to separate text from different block nodes. If leafText is given, it'll be inserted for every non-text leaf node encountered, otherwise leafText will be used.

Parameters

ParameterTypeDescription
fromnumberThe starting position.
tonumberThe ending position.
blockSeparator?stringOptional string to insert between block nodes.
leafText?string | (leafNode) => stringOptional string or function to provide text for leaf nodes.

Returns

string

The concatenated text content between the positions.


toJSON()

ts
toJSON(): NodeJSON;

Defined in: packages/model/src/elements/Node.ts:791

Return a JSON-serializable representation of this node.

Returns

NodeJSON

A JSON representation of this node.


toString()

ts
toString(): string;

Defined in: packages/model/src/elements/Node.ts:654

Return a string representation of this node for debugging purposes.

Returns

string


wrapMarks()

ts
protected wrapMarks(marks, content): string;

Defined in: packages/model/src/elements/Node.ts:832

Parameters

ParameterType
marksreadonly Mark[]
contentstring

Returns

string


fromJSON()

ts
static fromJSON(schema, json): Node;

Defined in: packages/model/src/elements/Node.ts:257

Deserialize a node from its JSON representation.

Note: NodeJSON as an array is not supported and will throw an error. It is only for backward compatibility.

Parameters

ParameterTypeDescription
schemaSchemaThe schema to use for deserializing the node.
json| NodeJSON | NodeJSON[]The JSON object representing the node.

Returns

Node

A new Node instance.

Throws

If the JSON is invalid or the node type doesn't exist in the schema.