Type Editor / @type-editor/model / elements/TextNode / TextNode
Class: TextNode
Defined in: packages/model/src/elements/TextNode.ts:17
A text node represents a piece of text in the document. Text nodes are leaf nodes that contain only text content and cannot have children. They can have marks applied to them (like bold, italic, links, etc.).
Extends
Implements
Constructors
Constructor
new TextNode(
type,
attrs,
content,
marks?): TextNode;Defined in: packages/model/src/elements/TextNode.ts:29
Create a text node. For most use cases, you should use Schema's text() method instead of calling this directly.
Parameters
| Parameter | Type | Description |
|---|---|---|
type | NodeType | The node type (should be the text node type from the schema). |
attrs | Attrs | The node's attributes. |
content | string | The text content of this node. |
marks? | readonly Mark[] | The marks applied to this text node. |
Returns
TextNode
Throws
If the content is empty (empty text nodes are not allowed).
Overrides
Properties
| Property | Modifier | Type | Default value | Description | Inherited from | Defined in |
|---|---|---|---|---|---|---|
_attrs | readonly | Attrs | undefined | - | Node._attrs | packages/model/src/elements/Node.ts:39 |
_content | readonly | Fragment | undefined | A container holding the node's children. | Node._content | packages/model/src/elements/Node.ts:38 |
_marks | readonly | readonly Mark[] | undefined | - | Node._marks | packages/model/src/elements/Node.ts:40 |
_tag | protected | Record<string, number> | {} | - | Node._tag | packages/model/src/elements/Node.ts:42 |
_text | readonly | string | undefined | For text nodes, this contains the node's text content. | Node._text | packages/model/src/elements/Node.ts:46 |
nodeType | readonly | NodeType | undefined | - | Node.nodeType | 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
Inherited from
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
Inherited from
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[]
Inherited from
content
Get Signature
get content(): Fragment;Defined in: packages/model/src/elements/Node.ts:94
A fragment containing the node's children.
Returns
Inherited from
elementType
Get Signature
get elementType(): ElementType;Defined in: packages/model/src/elements/TextNode.ts:41
Returns
Implementation of
Overrides
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
Inherited from
inlineContent
Get Signature
get inlineContent(): boolean;Defined in: packages/model/src/elements/Node.ts:195
True when this node allows inline content.
Returns
boolean
Inherited from
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
Inherited from
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
Inherited from
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
Inherited from
isLeaf
Get Signature
get isLeaf(): boolean;Defined in: packages/model/src/elements/Node.ts:217
True when this is a leaf node.
Returns
boolean
Inherited from
isText
Get Signature
get isText(): boolean;Defined in: packages/model/src/elements/Node.ts:210
True when this is a text node.
Returns
boolean
Inherited from
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
Inherited from
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
Inherited from
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[]
Inherited from
nodeSize
Get Signature
get nodeSize(): number;Defined in: packages/model/src/elements/TextNode.ts:58
The size of this text node, which equals the length of its text content.
Returns
number
Overrides
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
Inherited from
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
Inherited from
textContent
Get Signature
get textContent(): string;Defined in: packages/model/src/elements/TextNode.ts:51
The text content of this text node. For text nodes, this returns the actual text string contained in the node.
Returns
string
The text content as a string.
Overrides
type
Get Signature
get type(): NodeType;Defined in: packages/model/src/elements/Node.ts:108
The type of node that this is.
Returns
Inherited from
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.
Inherited from
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.
Inherited from
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.
Inherited from
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.
Inherited from
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
The child node at the specified index.
Throws
If the index is out of range.
Inherited from
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 |
Inherited from
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 |
Inherited from
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.
Inherited from
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
A new node with the same markup but different content, or this node if content is unchanged.
Inherited from
cut()
cut(from?, to?): TextNode;Defined in: packages/model/src/elements/TextNode.ts:144
Create a copy of this text node containing only the text between the given positions.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
from | number | 0 | The starting position. Defaults to 0. |
to? | number | undefined | The ending position. Defaults to the end of the text. |
Returns
TextNode
A new text node with the specified text range, or this node if the range is unchanged.
Overrides
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
Inherited from
eq()
eq(other): boolean;Defined in: packages/model/src/elements/TextNode.ts:161
Test whether this text node is equal to another node.
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Node | The node to compare with. |
Returns
boolean
true if the nodes have the same markup and text content, false otherwise.
Overrides
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
Inherited from
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.
Inherited from
mark()
mark(marks): TextNode;Defined in: packages/model/src/elements/TextNode.ts:119
Create a copy of this text node with the given marks applied.
Parameters
| Parameter | Type | Description |
|---|---|---|
marks | readonly Mark[] | The marks to apply to the new text node. |
Returns
TextNode
A new text node with the specified marks, or this node if marks are unchanged.
Overrides
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
The child node at the specified index, or null if the index is out of range.
Inherited from
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
The node at the specified position, or null if no node is found.
Inherited from
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
Inherited from
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.
Inherited from
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
A new node with the replacement applied.
Throws
If the slice doesn't fit in the specified location.
Inherited from
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.
Inherited from
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.
Inherited from
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.
Inherited from
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.
Inherited from
textBetween()
textBetween(from, to): string;Defined in: packages/model/src/elements/TextNode.ts:109
Get the text content between the given positions within this text node.
Parameters
| Parameter | Type | Description |
|---|---|---|
from | number | The starting position within the text. |
to | number | The ending position within the text. |
Returns
string
The text content between the positions.
Overrides
toJSON()
toJSON(): TextNodeJSON;Defined in: packages/model/src/elements/TextNode.ts:170
Return a JSON-serializable representation of this text node.
Returns
A JSON representation of this text node.
Overrides
toString()
toString(): string;Defined in: packages/model/src/elements/TextNode.ts:95
Return a string representation of this text node for debugging purposes.
Returns
string
A debug string representation of this node.
Overrides
withText()
withText(text): TextNode;Defined in: packages/model/src/elements/TextNode.ts:129
Create a copy of this text node with different text content.
Parameters
| Parameter | Type | Description |
|---|---|---|
text | string | The new text content. |
Returns
TextNode
A new text node with the specified text, or this node if text is unchanged.
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
Inherited from
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
A new Node instance.
Throws
If the JSON is invalid or the node type doesn't exist in the schema.