Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

Fragment

A fragment represents a node's collection of child nodes.

Like nodes, fragments are persistent data structures, and you should not mutate them or their content. Rather, you create new instances whenever needed. The API tries to make this easy.

class Fragment {
get firstChild(): Node | null;
get lastChild(): Node | null;
get childCount(): number;
readonly size: number;
 
addToEnd(node: Node): Fragment;
addToStart(node: Node): Fragment;
append(other: Fragment): Fragment;
child(index: number): Node;
cut(from: number, to?: number): Fragment;
descendants(f: (
node: Node,
pos: number,
parent: Node | null,
index: number,
) => boolean | void
): void;
eq(other: Fragment): boolean;
findDiffEnd(
other: Fragment,
pos?: number,
otherPos?: number,
): {
a: number;
b: number;
}
| null
;
findDiffStart(other: Fragment, pos?: number): number | null;
findIndex(pos: number, round?: number): {
index: number;
offset: number;
}
;
forEach(f: (
node: Node,
offset: number,
index: number,
) => void
): void;
maybeChild(index: number): Node | null;
nodesBetween(
from: number,
to: number,
f: (
node: Node,
start: number,
parent: Node | null,
index: number,
) => boolean | void
,
nodeStart?: number,
parent?: Node,
): void;
replaceChild(index: number, node: Node): Fragment;
textBetween(
from: number,
to: number,
blockSeparator?: string | null,
leafText?: string | null | ((leafNode: Node) => string),
): string;
toJSON(): any;
toString(): string;
 
static empty: Fragment;
 
static from(nodes?:
| Node
| readonly Node[]
| null
): Fragment;
static fromArray(array: readonly Node[]): Fragment;
static fromJSON(schema: Schema, value: any): Fragment;
}

§Properties

§
firstChild: Node | null readonly
[src]

The first child of the fragment, or null if it is empty.

§
lastChild: Node | null readonly
[src]

The last child of the fragment, or null if it is empty.

§
childCount: number readonly
[src]

The number of child nodes in this fragment.

§
size: number
[src]

The size of the fragment, which is the total of the size of its content nodes.

§Methods

§
addToEnd(node: Node): Fragment
[src]

Create a new fragment by appending the given node to this fragment.

§
addToStart(node: Node): Fragment
[src]

Create a new fragment by prepending the given node to this fragment.

§
append(other: Fragment): Fragment
[src]

Create a new fragment containing the combined content of this fragment and the other.

§
child(index: number): Node
[src]

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

§
cut(from: number, to?: number): Fragment
[src]

Cut out the sub-fragment between the two given positions.

§
descendants(f: (node: Node, pos: number, parent: Node | null, index: number) => boolean | void): void
[src]

Call the given callback for every descendant node. pos will be relative to the start of the fragment. The callback may return false to prevent traversal of a given node's children.

§
eq(other: Fragment): boolean
[src]

Compare this fragment to another one.

§
findDiffEnd(other: Fragment, pos?: number, otherPos?: number): {
a: number;
b: number;
}
| null
[src]

Find the first position, searching from the end, at which this fragment and the given fragment differ, or null if they are the same. Since this position will not be the same in both nodes, an object with two separate positions is returned.

§
findDiffStart(other: Fragment, pos?: number): number | null
[src]

Find the first position at which this fragment and another fragment differ, or null if they are the same.

§
findIndex(pos: number, round?: number): {
index: number;
offset: number;
}
[src]

Find the index and inner offset corresponding to a given relative position in this fragment. The result object will be reused (overwritten) the next time the function is called. (Not public.)

§
forEach(f: (node: Node, offset: number, index: number) => void): void
[src]

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

§
maybeChild(index: number): Node | null
[src]

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

§
nodesBetween(from: number, to: number, f: (node: Node, start: number, parent: Node | null, index: number) => boolean | void, nodeStart?: number, parent?: Node): void
[src]

Invoke a callback for all descendant nodes between the given two positions (relative to start of this fragment). Doesn't descend into a node when the callback returns false.

§
replaceChild(index: number, node: Node): Fragment
[src]

Create a new fragment in which the node at the given index is replaced by the given node.

§
textBetween(from: number, to: number, blockSeparator?: string | null, leafText?: string | null | ((leafNode: Node) => string)): string
[src]

Extract the text between from and to. See the same method on Node.

§
toJSON(): any
[src]

Create a JSON-serializeable representation of this fragment.

§
toString(): string
[src]

Return a debugging string that describes this fragment.

§Static Properties

§

An empty fragment. Intended to be reused whenever a node doesn't contain anything (rather than allocating a new empty fragment for each leaf node).

§Static Methods

§
from(nodes?: Fragment | Node | readonly Node[] | null): Fragment
[src]

Create a fragment from something that can be interpreted as a set of nodes. For null, it returns the empty fragment. For a fragment, the fragment itself. For a node or array of nodes, a fragment containing those nodes.

§
fromArray(array: readonly Node[]): Fragment
[src]

Build a fragment from an array of nodes. Ensures that adjacent text nodes with the same marks are joined together.

§
fromJSON(schema: Schema, value: any): Fragment
[src]

Deserialize a fragment from its JSON representation.