Skip to content

Type Editor


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

ts
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

ParameterTypeDescription
typeNodeTypeThe node type (should be the text node type from the schema).
attrsAttrsThe node's attributes.
contentstringThe 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

Node.constructor

Properties

PropertyModifierTypeDefault valueDescriptionInherited fromDefined in
_attrsreadonlyAttrsundefined-Node._attrspackages/model/src/elements/Node.ts:39
_contentreadonlyFragmentundefinedA container holding the node's children.Node._contentpackages/model/src/elements/Node.ts:38
_marksreadonlyreadonly Mark[]undefined-Node._markspackages/model/src/elements/Node.ts:40
_tagprotectedRecord<string, number>{}-Node._tagpackages/model/src/elements/Node.ts:42
_textreadonlystringundefinedFor text nodes, this contains the node's text content.Node._textpackages/model/src/elements/Node.ts:46
nodeTypereadonlyNodeTypeundefined-Node.nodeTypepackages/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

Inherited from

Node.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

Inherited from

Node.childCount


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[]

Inherited from

Node.children


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

Inherited from

Node.content


elementType

Get Signature

ts
get elementType(): ElementType;

Defined in: packages/model/src/elements/TextNode.ts:41

Returns

ElementType

Implementation of

PmElement.elementType

Overrides

Node.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

Inherited from

Node.firstChild


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

Inherited from

Node.inlineContent


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

Inherited from

Node.isAtom


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

Inherited from

Node.isBlock


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

Inherited from

Node.isInline


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

Inherited from

Node.isLeaf


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

Inherited from

Node.isText


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

Inherited from

Node.isTextblock


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

Inherited from

Node.lastChild


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[]

Inherited from

Node.marks


nodeSize

Get Signature

ts
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

Node.nodeSize


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

Inherited from

Node.tag


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

Inherited from

Node.text


textContent

Get Signature

ts
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

Node.textContent


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

Inherited from

Node.type

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.

Inherited from

Node.canAppend


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.

Inherited from

Node.canReplace


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.

Inherited from

Node.canReplaceWith


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.

Inherited from

Node.check


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.

Inherited from

Node.child


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

Inherited from

Node.childAfter


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

Inherited from

Node.childBefore


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.

Inherited from

Node.contentMatchAt


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.

Inherited from

Node.copy


cut()

ts
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

ParameterTypeDefault valueDescription
fromnumber0The starting position. Defaults to 0.
to?numberundefinedThe 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

Node.cut


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

Inherited from

Node.descendants


eq()

ts
eq(other): boolean;

Defined in: packages/model/src/elements/TextNode.ts:161

Test whether this text node is equal to another node.

Parameters

ParameterTypeDescription
otherNodeThe node to compare with.

Returns

boolean

true if the nodes have the same markup and text content, false otherwise.

Overrides

Node.eq


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

Inherited from

Node.forEach


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.

Inherited from

Node.hasMarkup


mark()

ts
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

ParameterTypeDescription
marksreadonly 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

Node.mark


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.

Inherited from

Node.maybeChild


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.

Inherited from

Node.nodeAt


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

Inherited from

Node.nodesBetween


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.

Inherited from

Node.rangeHasMark


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.

Inherited from

Node.replace


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.

Inherited from

Node.resolve


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.

Inherited from

Node.resolveNoCache


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.

Inherited from

Node.sameMarkup


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.

Inherited from

Node.slice


textBetween()

ts
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

ParameterTypeDescription
fromnumberThe starting position within the text.
tonumberThe ending position within the text.

Returns

string

The text content between the positions.

Overrides

Node.textBetween


toJSON()

ts
toJSON(): TextNodeJSON;

Defined in: packages/model/src/elements/TextNode.ts:170

Return a JSON-serializable representation of this text node.

Returns

TextNodeJSON

A JSON representation of this text node.

Overrides

Node.toJSON


toString()

ts
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

Node.toString


withText()

ts
withText(text): TextNode;

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

Create a copy of this text node with different text content.

Parameters

ParameterTypeDescription
textstringThe new text content.

Returns

TextNode

A new text node with the specified text, or this node if text is unchanged.


wrapMarks()

ts
protected wrapMarks(marks, content): string;

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

Parameters

ParameterType
marksreadonly Mark[]
contentstring

Returns

string

Inherited from

Node.wrapMarks


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.

Inherited from

Node.fromJSON