-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
documentfragment.ts
303 lines (256 loc) · 8.1 KB
/
documentfragment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module engine/view/documentfragment
*/
import TypeCheckable from './typecheckable.js';
import Text from './text.js';
import TextProxy from './textproxy.js';
import { EmitterMixin, isIterable } from '@ckeditor/ckeditor5-utils';
import type { default as Document, ChangeType } from './document.js';
import type Item from './item.js';
import type Node from './node.js';
/**
* Document fragment.
*
* To create a new document fragment instance use the
* {@link module:engine/view/upcastwriter~UpcastWriter#createDocumentFragment `UpcastWriter#createDocumentFragment()`}
* method.
*/
export default class DocumentFragment extends EmitterMixin( TypeCheckable ) implements Iterable<Node> {
/**
* The document to which this document fragment belongs.
*/
public readonly document: Document;
/**
* Array of child nodes.
*/
private readonly _children: Array<Node> = [];
/**
* Map of custom properties.
* Custom properties can be added to document fragment instance.
*/
private readonly _customProperties = new Map<string | symbol, unknown>();
/**
* Creates new DocumentFragment instance.
*
* @internal
* @param document The document to which this document fragment belongs.
* @param children A list of nodes to be inserted into the created document fragment.
*/
constructor( document: Document, children?: Node | Iterable<Node> ) {
super();
this.document = document;
if ( children ) {
this._insertChild( 0, children );
}
}
/**
* Iterable interface.
*
* Iterates over nodes added to this document fragment.
*/
public [ Symbol.iterator ](): Iterator<Node> {
return this._children[ Symbol.iterator ]();
}
/**
* Number of child nodes in this document fragment.
*/
public get childCount(): number {
return this._children.length;
}
/**
* Is `true` if there are no nodes inside this document fragment, `false` otherwise.
*/
public get isEmpty(): boolean {
return this.childCount === 0;
}
/**
* Artificial root of `DocumentFragment`. Returns itself. Added for compatibility reasons.
*/
public get root(): this {
return this;
}
/**
* Artificial parent of `DocumentFragment`. Returns `null`. Added for compatibility reasons.
*/
public get parent(): null {
return null;
}
/**
* Artificial element name. Returns `undefined`. Added for compatibility reasons.
*/
public get name(): undefined {
return undefined;
}
/**
* Artificial element getFillerOffset. Returns `undefined`. Added for compatibility reasons.
*/
public get getFillerOffset(): undefined {
return undefined;
}
/**
* Returns the custom property value for the given key.
*/
public getCustomProperty( key: string | symbol ): unknown {
return this._customProperties.get( key );
}
/**
* Returns an iterator which iterates over this document fragment's custom properties.
* Iterator provides `[ key, value ]` pairs for each stored property.
*/
public* getCustomProperties(): Iterable<[ string | symbol, unknown ]> {
yield* this._customProperties.entries();
}
/**
* {@link module:engine/view/documentfragment~DocumentFragment#_insertChild Insert} a child node or a list of child nodes at the end
* and sets the parent of these nodes to this fragment.
*
* @internal
* @param items Items to be inserted.
* @returns Number of appended nodes.
*/
public _appendChild( items: Item | string | Iterable<Item | string> ): number {
return this._insertChild( this.childCount, items );
}
/**
* Gets child at the given index.
*
* @param index Index of child.
* @returns Child node.
*/
public getChild( index: number ): Node {
return this._children[ index ];
}
/**
* Gets index of the given child node. Returns `-1` if child node is not found.
*
* @param node Child node.
* @returns Index of the child node.
*/
public getChildIndex( node: Node ): number {
return this._children.indexOf( node );
}
/**
* Gets child nodes iterator.
*
* @returns Child nodes iterator.
*/
public getChildren(): IterableIterator<Node> {
return this._children[ Symbol.iterator ]();
}
/**
* Inserts a child node or a list of child nodes on the given index and sets the parent of these nodes to
* this fragment.
*
* @internal
* @param index Position where nodes should be inserted.
* @param items Items to be inserted.
* @returns Number of inserted nodes.
*/
public _insertChild( index: number, items: Item | string | Iterable<Item | string> ): number {
this._fireChange( 'children', this );
let count = 0;
const nodes = normalize( this.document, items );
for ( const node of nodes ) {
// If node that is being added to this element is already inside another element, first remove it from the old parent.
if ( node.parent !== null ) {
node._remove();
}
( node as any ).parent = this;
this._children.splice( index, 0, node );
index++;
count++;
}
return count;
}
/**
* Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
*
* @internal
* @param index Number of the first node to remove.
* @param howMany Number of nodes to remove.
* @returns The array of removed nodes.
*/
public _removeChildren( index: number, howMany: number = 1 ): Array<Node> {
this._fireChange( 'children', this );
for ( let i = index; i < index + howMany; i++ ) {
( this._children[ i ] as any ).parent = null;
}
return this._children.splice( index, howMany );
}
/**
* Fires `change` event with given type of the change.
*
* @internal
* @param type Type of the change.
* @param node Changed node.
*/
public _fireChange( type: ChangeType, node: Node | DocumentFragment ): void {
this.fire( 'change:' + type, node );
}
/**
* Sets a custom property. They can be used to add special data to elements.
*
* @see module:engine/view/downcastwriter~DowncastWriter#setCustomProperty
* @internal
*/
public _setCustomProperty( key: string | symbol, value: unknown ): void {
this._customProperties.set( key, value );
}
/**
* Removes the custom property stored under the given key.
*
* @see module:engine/view/downcastwriter~DowncastWriter#removeCustomProperty
* @internal
* @returns Returns true if property was removed.
*/
public _removeCustomProperty( key: string | symbol ): boolean {
return this._customProperties.delete( key );
}
// @if CK_DEBUG_ENGINE // public printTree(): string {
// @if CK_DEBUG_ENGINE // let string = 'ViewDocumentFragment: [';
// @if CK_DEBUG_ENGINE // for ( const child of this.getChildren() as any ) {
// @if CK_DEBUG_ENGINE // if ( child.is( '$text' ) ) {
// @if CK_DEBUG_ENGINE // string += '\n' + '\t'.repeat( 1 ) + child.data;
// @if CK_DEBUG_ENGINE // } else {
// @if CK_DEBUG_ENGINE // string += '\n' + child.printTree( 1 );
// @if CK_DEBUG_ENGINE // }
// @if CK_DEBUG_ENGINE // }
// @if CK_DEBUG_ENGINE // string += '\n]';
// @if CK_DEBUG_ENGINE // return string;
// @if CK_DEBUG_ENGINE // }
// @if CK_DEBUG_ENGINE // public logTree(): void {
// @if CK_DEBUG_ENGINE // console.log( this.printTree() );
// @if CK_DEBUG_ENGINE // }
}
// The magic of type inference using `is` method is centralized in `TypeCheckable` class.
// Proper overload would interfere with that.
DocumentFragment.prototype.is = function( type: string ): boolean {
return type === 'documentFragment' || type === 'view:documentFragment';
};
/**
* Converts strings to Text and non-iterables to arrays.
*/
function normalize( document: Document, nodes: Item | string | Iterable<Item | string> ): Array<Node> {
// Separate condition because string is iterable.
if ( typeof nodes == 'string' ) {
return [ new Text( document, nodes ) ];
}
if ( !isIterable( nodes ) ) {
nodes = [ nodes ];
}
// Array.from to enable .map() on non-arrays.
return Array.from( nodes )
.map( node => {
if ( typeof node == 'string' ) {
return new Text( document, node );
}
if ( node instanceof TextProxy ) {
return new Text( document, node.data );
}
return node;
} );
}