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
/
node.js
388 lines (338 loc) · 11.7 KB
/
node.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module engine/view/node
*/
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
import { clone } from 'lodash-es';
// To check if component is loaded more than once.
import '@ckeditor/ckeditor5-utils/src/version';
/**
* Abstract tree view node class.
*
* This is an abstract class. Its constructor should not be used directly.
* Use the {@link module:engine/view/element~Element} class to create view elements
* or {@link module:engine/view/text~Text} class to create view text nodes.
*
* @abstract
*/
export default class Node {
/**
* Creates a tree view node.
*/
constructor() {
/**
* Parent element. Null by default. Set by {@link module:engine/view/element~Element#_insertChild}.
*
* @readonly
* @member {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment|null}
*/
this.parent = null;
}
/**
* Index of the node in the parent element or null if the node has no parent.
*
* Accessing this property throws an error if this node's parent element does not contain it.
* This means that view tree got broken.
*
* @readonly
* @type {Number|null}
*/
get index() {
let pos;
if ( !this.parent ) {
return null;
}
// No parent or child doesn't exist in parent's children.
if ( ( pos = this.parent.getChildIndex( this ) ) == -1 ) {
/**
* The node's parent does not contain this node. It means that the document tree is corrupted.
*
* @error view-node-not-found-in-parent
*/
throw new CKEditorError( 'view-node-not-found-in-parent: The node\'s parent does not contain this node.', this );
}
return pos;
}
/**
* Node's next sibling, or `null` if it is the last child.
*
* @readonly
* @type {module:engine/view/node~Node|null}
*/
get nextSibling() {
const index = this.index;
return ( index !== null && this.parent.getChild( index + 1 ) ) || null;
}
/**
* Node's previous sibling, or `null` if it is the first child.
*
* @readonly
* @type {module:engine/view/node~Node|null}
*/
get previousSibling() {
const index = this.index;
return ( index !== null && this.parent.getChild( index - 1 ) ) || null;
}
/**
* Top-most ancestor of the node. If the node has no parent it is the root itself.
*
* @readonly
* @type {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
*/
get root() {
let root = this; // eslint-disable-line consistent-this
while ( root.parent ) {
root = root.parent;
}
return root;
}
/**
* {@link module:engine/view/document~Document View document} that owns this node, or `null` if the node is inside
* {@link module:engine/view/documentfragment~DocumentFragment document fragment}.
*
* @readonly
* @type {module:engine/view/document~Document|null}
*/
get document() {
// Parent might be Node, null or DocumentFragment.
if ( this.parent instanceof Node ) {
return this.parent.document;
} else {
return null;
}
}
/**
* Gets a path to the node. The path is an array containing indices of consecutive ancestors of this node,
* beginning from {@link module:engine/view/node~Node#root root}, down to this node's index.
*
* const abc = downcastWriter.createText( 'abc' );
* const foo = downcastWriter.createText( 'foo' );
* const h1 = downcastWriter.createElement( 'h1', null, downcastWriter.createText( 'header' ) );
* const p = downcastWriter.createElement( 'p', null, [ abc, foo ] );
* const div = downcastWriter.createElement( 'div', null, [ h1, p ] );
* foo.getPath(); // Returns [ 1, 3 ]. `foo` is in `p` which is in `div`. `p` starts at offset 1, while `foo` at 3.
* h1.getPath(); // Returns [ 0 ].
* div.getPath(); // Returns [].
*
* @returns {Array.<Number>} The path.
*/
getPath() {
const path = [];
let node = this; // eslint-disable-line consistent-this
while ( node.parent ) {
path.unshift( node.index );
node = node.parent;
}
return path;
}
/**
* Returns ancestors array of this node.
*
* @param {Object} options Options object.
* @param {Boolean} [options.includeSelf=false] When set to `true` this node will be also included in parent's array.
* @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from node's parent to root element,
* otherwise root element will be the first item in the array.
* @returns {Array} Array with ancestors.
*/
getAncestors( options = { includeSelf: false, parentFirst: false } ) {
const ancestors = [];
let parent = options.includeSelf ? this : this.parent;
while ( parent ) {
ancestors[ options.parentFirst ? 'push' : 'unshift' ]( parent );
parent = parent.parent;
}
return ancestors;
}
/**
* Returns a {@link module:engine/view/element~Element} or {@link module:engine/view/documentfragment~DocumentFragment}
* which is a common ancestor of both nodes.
*
* @param {module:engine/view/node~Node} node The second node.
* @param {Object} options Options object.
* @param {Boolean} [options.includeSelf=false] When set to `true` both nodes will be considered "ancestors" too.
* Which means that if e.g. node A is inside B, then their common ancestor will be B.
* @returns {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment|null}
*/
getCommonAncestor( node, options = {} ) {
const ancestorsA = this.getAncestors( options );
const ancestorsB = node.getAncestors( options );
let i = 0;
while ( ancestorsA[ i ] == ancestorsB[ i ] && ancestorsA[ i ] ) {
i++;
}
return i === 0 ? null : ancestorsA[ i - 1 ];
}
/**
* Returns whether this node is before given node. `false` is returned if nodes are in different trees (for example,
* in different {@link module:engine/view/documentfragment~DocumentFragment}s).
*
* @param {module:engine/view/node~Node} node Node to compare with.
* @returns {Boolean}
*/
isBefore( node ) {
// Given node is not before this node if they are same.
if ( this == node ) {
return false;
}
// Return `false` if it is impossible to compare nodes.
if ( this.root !== node.root ) {
return false;
}
const thisPath = this.getPath();
const nodePath = node.getPath();
const result = compareArrays( thisPath, nodePath );
switch ( result ) {
case 'prefix':
return true;
case 'extension':
return false;
default:
return thisPath[ result ] < nodePath[ result ];
}
}
/**
* Returns whether this node is after given node. `false` is returned if nodes are in different trees (for example,
* in different {@link module:engine/view/documentfragment~DocumentFragment}s).
*
* @param {module:engine/view/node~Node} node Node to compare with.
* @returns {Boolean}
*/
isAfter( node ) {
// Given node is not before this node if they are same.
if ( this == node ) {
return false;
}
// Return `false` if it is impossible to compare nodes.
if ( this.root !== node.root ) {
return false;
}
// In other cases, just check if the `node` is before, and return the opposite.
return !this.isBefore( node );
}
/**
* Removes node from parent.
*
* @protected
*/
_remove() {
this.parent._removeChildren( this.index );
}
/**
* @protected
* @param {module:engine/view/document~ChangeType} type Type of the change.
* @param {module:engine/view/node~Node} node Changed node.
* @fires change
*/
_fireChange( type, node ) {
this.fire( 'change:' + type, node );
if ( this.parent ) {
this.parent._fireChange( type, node );
}
}
/**
* Custom toJSON method to solve child-parent circular dependencies.
*
* @returns {Object} Clone of this object with the parent property removed.
*/
toJSON() {
const json = clone( this );
// Due to circular references we need to remove parent reference.
delete json.parent;
return json;
}
/**
* Checks whether this object is of the given type.
*
* This method is useful when processing view objects that are of unknown type. For example, a function
* may return a {@link module:engine/view/documentfragment~DocumentFragment} or a {@link module:engine/view/node~Node}
* that can be either a text node or an element. This method can be used to check what kind of object is returned.
*
* someObject.is( 'element' ); // -> true if this is an element
* someObject.is( 'node' ); // -> true if this is a node (a text node or an element)
* someObject.is( 'documentFragment' ); // -> true if this is a document fragment
*
* Since this method is also available on a range of model objects, you can prefix the type of the object with
* `model:` or `view:` to check, for example, if this is the model's or view's element:
*
* viewElement.is( 'view:element' ); // -> true
* viewElement.is( 'model:element' ); // -> false
*
* By using this method it is also possible to check a name of an element:
*
* imgElement.is( 'img' ); // -> true
* imgElement.is( 'element', 'img' ); // -> same as above
* imgElement.is( 'view:element', 'img' ); // -> same as above, but more precise
*
* The list of view objects which implement the `is()` method:
*
* * {@link module:engine/view/attributeelement~AttributeElement#is `AttributeElement#is()`}
* * {@link module:engine/view/containerelement~ContainerElement#is `ContainerElement#is()`}
* * {@link module:engine/view/documentfragment~DocumentFragment#is `DocumentFragment#is()`}
* * {@link module:engine/view/documentselection~DocumentSelection#is `DocumentSelection#is()`}
* * {@link module:engine/view/editableelement~EditableElement#is `EditableElement#is()`}
* * {@link module:engine/view/element~Element#is `Element#is()`}
* * {@link module:engine/view/emptyelement~EmptyElement#is `EmptyElement#is()`}
* * {@link module:engine/view/node~Node#is `Node#is()`}
* * {@link module:engine/view/position~Position#is `Position#is()`}
* * {@link module:engine/view/range~Range#is `Range#is()`}
* * {@link module:engine/view/rooteditableelement~RootEditableElement#is `RootEditableElement#is()`}
* * {@link module:engine/view/selection~Selection#is `Selection#is()`}
* * {@link module:engine/view/text~Text#is `Text#is()`}
* * {@link module:engine/view/textproxy~TextProxy#is `TextProxy#is()`}
* * {@link module:engine/view/uielement~UIElement#is `UIElement#is()`}
*
* @method #is
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'node' || type == 'view:node';
}
/**
* Clones this node.
*
* @protected
* @method #_clone
* @returns {module:engine/view/node~Node} Clone of this node.
*/
/**
* Checks if provided node is similar to this node.
*
* @method #isSimilar
* @returns {Boolean} True if nodes are similar.
*/
}
/**
* Fired when list of {@link module:engine/view/element~Element elements} children changes.
*
* Change event is bubbled – it is fired on all ancestors.
*
* @event change:children
* @param {module:engine/view/node~Node} changedNode
*/
/**
* Fired when list of {@link module:engine/view/element~Element elements} attributes changes.
*
* Change event is bubbled – it is fired on all ancestors.
*
* @event change:attributes
* @param {module:engine/view/node~Node} changedNode
*/
/**
* Fired when {@link module:engine/view/text~Text text nodes} data changes.
*
* Change event is bubbled – it is fired on all ancestors.
*
* @event change:text
* @param {module:engine/view/node~Node} changedNode
*/
/**
* @event change
*/
mix( Node, EmitterMixin );