This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
documentfragment.js
335 lines (295 loc) · 9.69 KB
/
documentfragment.js
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module module:engine/model/documentfragment
*/
import NodeList from './nodelist';
import Element from './element';
import Text from './text';
import TextProxy from './textproxy';
import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
/**
* DocumentFragment represents a part of model which does not have a common root but it's top-level nodes
* can be seen as siblings. In other words, it is a detached part of model tree, without a root.
*
* DocumentFragment has own {@link module:engine/model/markercollection~MarkerCollection}. Markers from this collection
* will be set to the {@link module:engine/model/model~Model#markers model markers} by a
* {@link module:engine/model/writer~Writer#insert} function.
*/
export default class DocumentFragment {
/**
* Creates an empty `DocumentFragment`.
*
* @param {module:engine/model/node~Node|Iterable.<module:engine/model/node~Node>} [children]
* Nodes to be contained inside the `DocumentFragment`.
*/
constructor( children ) {
/**
* DocumentFragment static markers map. This is a list of names and {@link module:engine/model/range~Range ranges}
* which will be set as Markers to {@link module:engine/model/model~Model#markers model markers collection}
* when DocumentFragment will be inserted to the document.
*
* @member {Map<String,module:engine/model/range~Range>} module:engine/model/documentfragment~DocumentFragment#markers
*/
this.markers = new Map();
/**
* List of nodes contained inside the document fragment.
*
* @private
* @member {module:engine/model/nodelist~NodeList} module:engine/model/documentfragment~DocumentFragment#_children
*/
this._children = new NodeList();
if ( children ) {
this.insertChildren( 0, children );
}
}
/**
* Returns an iterator that iterates over all nodes contained inside this document fragment.
*
* @returns {Iterable.<module:engine/model/node~Node>}
*/
[ Symbol.iterator ]() {
return this.getChildren();
}
/**
* Number of this document fragment's children.
*
* @readonly
* @type {Number}
*/
get childCount() {
return this._children.length;
}
/**
* Sum of {module:engine/model/node~Node#offsetSize offset sizes} of all of this document fragment's children.
*
* @readonly
* @type {Number}
*/
get maxOffset() {
return this._children.maxOffset;
}
/**
* Is `true` if there are no nodes inside this document fragment, `false` otherwise.
*
* @readonly
* @type {Boolean}
*/
get isEmpty() {
return this.childCount === 0;
}
/**
* Artificial root of `DocumentFragment`. Returns itself. Added for compatibility reasons.
*
* @readonly
* @type {module:engine/model/documentfragment~DocumentFragment}
*/
get root() {
return this;
}
/**
* Artificial parent of `DocumentFragment`. Returns `null`. Added for compatibility reasons.
*
* @readonly
* @type {null}
*/
get parent() {
return null;
}
/**
* Checks whether given model tree object is of given type.
*
* Read more in {@link module:engine/model/node~Node#is}.
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'documentFragment';
}
/**
* Gets the child at the given index. Returns `null` if incorrect index was passed.
*
* @param {Number} index Index of child.
* @returns {module:engine/model/node~Node|null} Child node.
*/
getChild( index ) {
return this._children.getNode( index );
}
/**
* Returns an iterator that iterates over all of this document fragment's children.
*
* @returns {Iterable.<module:engine/model/node~Node>}
*/
getChildren() {
return this._children[ Symbol.iterator ]();
}
/**
* Returns an index of the given child node. Returns `null` if given node is not a child of this document fragment.
*
* @param {module:engine/model/node~Node} node Child node to look for.
* @returns {Number|null} Child node's index.
*/
getChildIndex( node ) {
return this._children.getNodeIndex( node );
}
/**
* Returns the starting offset of given child. Starting offset is equal to the sum of
* {module:engine/model/node~Node#offsetSize offset sizes} of all node's siblings that are before it. Returns `null` if
* given node is not a child of this document fragment.
*
* @param {module:engine/model/node~Node} node Child node to look for.
* @returns {Number|null} Child node's starting offset.
*/
getChildStartOffset( node ) {
return this._children.getNodeStartOffset( node );
}
/**
* Returns path to a `DocumentFragment`, which is an empty array. Added for compatibility reasons.
*
* @returns {Array}
*/
getPath() {
return [];
}
/**
* Returns a descendant node by its path relative to this element.
*
* // <this>a<b>c</b></this>
* this.getNodeByPath( [ 0 ] ); // -> "a"
* this.getNodeByPath( [ 1 ] ); // -> <b>
* this.getNodeByPath( [ 1, 0 ] ); // -> "c"
*
* @param {Array.<Number>} relativePath Path of the node to find, relative to this element.
* @returns {module:engine/model/node~Node|module:engine/model/documentfragment~DocumentFragment}
*/
getNodeByPath( relativePath ) {
let node = this; // eslint-disable-line consistent-this
for ( const index of relativePath ) {
node = node.getChild( node.offsetToIndex( index ) );
}
return node;
}
/**
* Converts offset "position" to index "position".
*
* Returns index of a node that occupies given offset. If given offset is too low, returns `0`. If given offset is
* too high, returns index after last child}.
*
* const textNode = new Text( 'foo' );
* const pElement = new Element( 'p' );
* const docFrag = new DocumentFragment( [ textNode, pElement ] );
* docFrag.offsetToIndex( -1 ); // Returns 0, because offset is too low.
* docFrag.offsetToIndex( 0 ); // Returns 0, because offset 0 is taken by `textNode` which is at index 0.
* docFrag.offsetToIndex( 1 ); // Returns 0, because `textNode` has `offsetSize` equal to 3, so it occupies offset 1 too.
* docFrag.offsetToIndex( 2 ); // Returns 0.
* docFrag.offsetToIndex( 3 ); // Returns 1.
* docFrag.offsetToIndex( 4 ); // Returns 2. There are no nodes at offset 4, so last available index is returned.
*
* @param {Number} offset Offset to look for.
* @returns {Number} Index of a node that occupies given offset.
*/
offsetToIndex( offset ) {
return this._children.offsetToIndex( offset );
}
/**
* {@link #insertChildren Inserts} one or more nodes at the end of this document fragment.
*
* @param {module:engine/model/item~Item|Iterable.<module:engine/model/item~Item>} items Items to be inserted.
*/
appendChildren( items ) {
this.insertChildren( this.childCount, items );
}
/**
* Inserts one or more nodes at the given index and sets {@link module:engine/model/node~Node#parent parent} of these nodes
* to this document fragment.
*
* @param {Number} index Index at which nodes should be inserted.
* @param {module:engine/model/item~Item|Iterable.<module:engine/model/item~Item>} items Items to be inserted.
*/
insertChildren( index, items ) {
const nodes = normalize( 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.parent = this;
}
this._children.insertNodes( index, nodes );
}
/**
* Removes one or more nodes starting at the given index
* and sets {@link module:engine/model/node~Node#parent parent} of these nodes to `null`.
*
* @param {Number} index Index of the first node to remove.
* @param {Number} [howMany=1] Number of nodes to remove.
* @returns {Array.<module:engine/model/node~Node>} Array containing removed nodes.
*/
removeChildren( index, howMany = 1 ) {
const nodes = this._children.removeNodes( index, howMany );
for ( const node of nodes ) {
node.parent = null;
}
return nodes;
}
/**
* Converts `DocumentFragment` instance to plain object and returns it.
* Takes care of converting all of this document fragment's children.
*
* @returns {Object} `DocumentFragment` instance converted to plain object.
*/
toJSON() {
const json = [];
for ( const node of this._children ) {
json.push( node.toJSON() );
}
return json;
}
/**
* Creates a `DocumentFragment` instance from given plain object (i.e. parsed JSON string).
* Converts `DocumentFragment` children to proper nodes.
*
* @param {Object} json Plain object to be converted to `DocumentFragment`.
* @returns {module:engine/model/documentfragment~DocumentFragment} `DocumentFragment` instance created using given plain object.
*/
static fromJSON( json ) {
const children = [];
for ( const child of json ) {
if ( child.name ) {
// If child has name property, it is an Element.
children.push( Element.fromJSON( child ) );
} else {
// Otherwise, it is a Text node.
children.push( Text.fromJSON( child ) );
}
}
return new DocumentFragment( children );
}
}
// Converts strings to Text and non-iterables to arrays.
//
// @param {String|module:engine/model/item~Item|Iterable.<module:engine/model/item~Item>}
// @return {Iterable.<module:engine/model/node~Node>}
function normalize( nodes ) {
// Separate condition because string is iterable.
if ( typeof nodes == 'string' ) {
return [ new Text( 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( node );
}
if ( node instanceof TextProxy ) {
return new Text( node.data, node.getAttributes() );
}
return node;
} );
}