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
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
| Parameter | Type | Description |
|---|---|---|
type? | NodeType | The type of node that this is. |
attrs? | Attrs | An object mapping attribute names to values. The kind of attributes allowed and required are determined by the node type. |
content? | Fragment | A 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? | string | For text nodes, this contains the node's text content. |
Returns
Node
Properties
| Property | Modifier | Type | Default value | Description | Defined in |
|---|---|---|---|---|---|
_attrs | readonly | Attrs | undefined | - | packages/model/src/elements/Node.ts:39 |
_content | readonly | Fragment | undefined | A container holding the node's children. | packages/model/src/elements/Node.ts:38 |
_marks | readonly | readonly Mark[] | undefined | - | packages/model/src/elements/Node.ts:40 |
_tag | protected | Record<string, number> | {} | - | packages/model/src/elements/Node.ts:42 |
_text | readonly | string | undefined | For text nodes, this contains the node's text content. | packages/model/src/elements/Node.ts:46 |
nodeType | readonly | NodeType | undefined | - | packages/model/src/elements/Node.ts:41 |
Accessors
attrs
Get Signature
get attrs(): Attrs;Defined in: packages/model/src/elements/Node.ts:115
An object mapping attribute names to values.
Returns
childCount
Get Signature
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
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
get content(): Fragment;Defined in: packages/model/src/elements/Node.ts:94
A fragment containing the node's children.
Returns
elementType
Get Signature
get elementType(): ElementType;Defined in: packages/model/src/elements/Node.ts:87
Returns
Implementation of
firstChild
Get Signature
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
get inlineContent(): boolean;Defined in: packages/model/src/elements/Node.ts:195
True when this node allows inline content.
Returns
boolean
isAtom
Get Signature
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
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
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
get isLeaf(): boolean;Defined in: packages/model/src/elements/Node.ts:217
True when this is a leaf node.
Returns
boolean
isText
Get Signature
get isText(): boolean;Defined in: packages/model/src/elements/Node.ts:210
True when this is a text node.
Returns
boolean
isTextblock
Get Signature
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
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
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
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
get tag(): Record<string, number>;Defined in: packages/model/src/elements/Node.ts:79
Returns
Record<string, number>
Set Signature
set tag(tag): void;Defined in: packages/model/src/elements/Node.ts:83
Parameters
| Parameter | Type |
|---|---|
tag | Record<string, number> |
Returns
void
text
Get Signature
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
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
get type(): NodeType;Defined in: packages/model/src/elements/Node.ts:108
The type of node that this is.
Returns
Methods
canAppend()
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
| Parameter | Type | Description |
|---|---|---|
other | Node | The node whose content to test for appending. |
Returns
boolean
true if the content can be appended, false otherwise.
canReplace()
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
| Parameter | Type | Default value | Description |
|---|---|---|---|
from | number | undefined | The starting child index. |
to | number | undefined | The ending child index. |
replacement | Fragment | Fragment.empty | The replacement fragment. Defaults to an empty fragment. |
start | number | 0 | The start index in the replacement fragment. Defaults to 0. |
end | number | replacement.childCount | The 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()
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
| Parameter | Type | Description |
|---|---|---|
from | number | The starting child index. |
to | number | The ending child index. |
type | NodeType | The 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()
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()
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
| Parameter | Type | Description |
|---|---|---|
index | number | The 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()
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
| Parameter | Type | Description |
|---|---|---|
pos | number | The position to search from. |
Returns
{
index: number;
node: Node;
offset: number;
}An object containing the node, its index, and its offset.
| Name | Type | Defined in |
|---|---|---|
index | number | packages/model/src/elements/Node.ts:581 |
node | Node | packages/model/src/elements/Node.ts:581 |
offset | number | packages/model/src/elements/Node.ts:581 |
childBefore()
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
| Parameter | Type | Description |
|---|---|---|
pos | number | The position to search from. |
Returns
{
index: number;
node: Node;
offset: number;
}An object containing the node, its index, and its offset.
| Name | Type | Defined in |
|---|---|---|
index | number | packages/model/src/elements/Node.ts:594 |
node | Node | packages/model/src/elements/Node.ts:594 |
offset | number | packages/model/src/elements/Node.ts:594 |
contentMatchAt()
contentMatchAt(index): ContentMatch;Defined in: packages/model/src/elements/Node.ts:674
Get the content match in this node at the given index.
Parameters
| Parameter | Type | Description |
|---|---|---|
index | number | The child index to get the content match at. |
Returns
The content match at the specified index.
Throws
If the node has invalid content.
copy()
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
| Parameter | Type | Default value | Description |
|---|---|---|---|
content | Fragment | null | The 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()
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
| Parameter | Type | Description |
|---|---|---|
from | number | The starting position. |
to | number | The ending position. Defaults to the end of the node's content. |
Returns
Node
A new node containing only the specified content range.
descendants()
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
| Parameter | Type | Description |
|---|---|---|
callbackFunc | (node, pos, parent, index) => boolean | void | The callback function to invoke for each descendant node. Return false to skip recursing into a node's children. |
Returns
void
eq()
eq(other): boolean;Defined in: packages/model/src/elements/Node.ts:400
Test whether two nodes represent the same piece of document.
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Node | The node to compare with this node. |
Returns
boolean
true if the nodes are equal, false otherwise.
forEach()
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
| Parameter | Type | Description |
|---|---|---|
callbackFunc | (node, offset, index) => void | The callback function to invoke for each child node. Receives the node, its offset position, and its index. |
Returns
void
hasMarkup()
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
| Parameter | Type | Description |
|---|---|---|
type | NodeType | The 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()
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
| Parameter | Type | Description |
|---|---|---|
marks | readonly 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()
maybeChild(index): Node;Defined in: packages/model/src/elements/Node.ts:314
Get the child node at the given index, if it exists.
Parameters
| Parameter | Type | Description |
|---|---|---|
index | number | The 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()
nodeAt(pos?): Node;Defined in: packages/model/src/elements/Node.ts:553
Find the node directly after the given position.
Parameters
| Parameter | Type | Description |
|---|---|---|
pos? | number | The position to search from. |
Returns
Node
The node at the specified position, or null if no node is found.
nodesBetween()
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
| Parameter | Type | Default value | Description |
|---|---|---|---|
from | number | undefined | The starting position (inclusive). |
to | number | undefined | The ending position (exclusive). |
callbackFunc | (node, pos, parent, index) => boolean | void | undefined | The callback function to invoke for each node. Return false to skip recursing into a node's children. |
startPos | number | 0 | The starting position offset for counting. Defaults to 0. |
Returns
void
rangeHasMark()
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
| Parameter | Type | Description |
|---|---|---|
from | number | The starting position. |
to | number | The ending position. |
type | | Mark | MarkType | The mark or mark type to search for. |
Returns
boolean
true if the mark is found in the range, false otherwise.
replace()
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
| Parameter | Type | Description |
|---|---|---|
from | number | The starting position of the range to replace. |
to | number | The ending position of the range to replace. |
slice | Slice | The 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()
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
| Parameter | Type | Description |
|---|---|---|
pos | number | The position to resolve. |
Returns
A ResolvedPos object with information about the position's context.
resolveNoCache()
resolveNoCache(pos): ResolvedPos;Defined in: packages/model/src/elements/Node.ts:625
Resolve the given position without using the cache.
Parameters
| Parameter | Type | Description |
|---|---|---|
pos | number | The position to resolve. |
Returns
A ResolvedPos object with information about the position's context.
sameMarkup()
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
| Parameter | Type | Description |
|---|---|---|
other | Node | The node to compare markup with. |
Returns
boolean
true if both nodes have the same markup, false otherwise.
slice()
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
| Parameter | Type | Default value | Description |
|---|---|---|---|
from | number | undefined | The starting position. |
to | number | ... | The ending position. Defaults to the end of the node's content. |
includeParents | boolean | false | Whether to include parent nodes in the slice. Defaults to false. |
Returns
A Slice object representing the content between the positions.
textBetween()
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
| Parameter | Type | Description |
|---|---|---|
from | number | The starting position. |
to | number | The ending position. |
blockSeparator? | string | Optional string to insert between block nodes. |
leafText? | string | (leafNode) => string | Optional string or function to provide text for leaf nodes. |
Returns
string
The concatenated text content between the positions.
toJSON()
toJSON(): NodeJSON;Defined in: packages/model/src/elements/Node.ts:791
Return a JSON-serializable representation of this node.
Returns
A JSON representation of this node.
toString()
toString(): string;Defined in: packages/model/src/elements/Node.ts:654
Return a string representation of this node for debugging purposes.
Returns
string
wrapMarks()
protected wrapMarks(marks, content): string;Defined in: packages/model/src/elements/Node.ts:832
Parameters
| Parameter | Type |
|---|---|
marks | readonly Mark[] |
content | string |
Returns
string
fromJSON()
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
| Parameter | Type | Description |
|---|---|---|
schema | Schema | The 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.