Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ve.Node.js

raw symbols SCIP
1 /*!
2 * VisualEditor Node class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Generic node.
9 *
10 * @abstract
11 * @mixes OO.EventEmitter
12 *
13 * @constructor
14 */
15 ve.Node = function VeNode() {
16 // Properties
17 this.type = this.constructor.static.name;
18 this.parent = null;
19 this.root = null;
20 this.doc = null;
21 };
22
23 /**
24 * @event ve.Node#attach
25 * @param {ve.Node} New parent
26 */
27
28 /**
29 * @event ve.Node#detach
30 * @param {ve.Node} Old parent
31 */
32
33 /**
34 * The node has a new root assigned.
35 *
36 * The root will be consistent with that set in descendants and ancestors, but other parts of the
37 * tree may be inconsistent.
38 *
39 * @event ve.Node#root
40 * @param {ve.Node} New root
41 */
42
43 /**
44 * The node root has been set to null.
45 *
46 * The root will be consistent with that set in descendants and ancestors, but other parts of the
47 * tree may be inconsistent.
48 *
49 * @event ve.Node#unroot
50 * @param {ve.Node} Old root
51 */
52
53 /* Abstract Methods */
54
55 /**
56 * Get allowed child node types.
57 *
58 * @abstract
59 * @return {string[]|null} List of node types allowed as children or null if any type is allowed
60 */
61 ve.Node.prototype.getChildNodeTypes = null;
62
63 /**
64 * Get allowed parent node types.
65 *
66 * @abstract
67 * @return {string[]|null} List of node types allowed as parents or null if any type is allowed
68 */
69 ve.Node.prototype.getParentNodeTypes = null;
70
71 /**
72 * Check if the specified type is an allowed child node type
73 *
74 * @param {string} type Node type
75 * @return {boolean} The type is allowed
76 */
77 ve.Node.prototype.isAllowedChildNodeType = function ( type ) {
78 const childTypes = this.getChildNodeTypes();
79 return childTypes === null || childTypes.indexOf( type ) !== -1;
80 };
81
82 /**
83 * Check if the specified type is an allowed child node type
84 *
85 * @param {string} type Node type
86 * @return {boolean} The type is allowed
87 */
88 ve.Node.prototype.isAllowedParentNodeType = function ( type ) {
89 const parentTypes = this.getParentNodeTypes();
90 return parentTypes === null || parentTypes.indexOf( type ) !== -1;
91 };
92
93 /**
94 * Get suggested parent node types.
95 *
96 * @abstract
97 * @return {string[]|null} List of node types suggested as parents or null if any type is suggested
98 */
99 ve.Node.prototype.getSuggestedParentNodeTypes = null;
100
101 /**
102 * Check if the node can have children.
103 *
104 * @abstract
105 * @return {boolean} Node can have children
106 */
107 ve.Node.prototype.canHaveChildren = null;
108
109 /**
110 * Check if the node can have children but not content nor be content.
111 *
112 * @abstract
113 * @return {boolean} Node can have children but not content nor be content
114 */
115 ve.Node.prototype.canHaveChildrenNotContent = null;
116
117 /**
118 * Check if the node can contain content.
119 *
120 * @abstract
121 * @return {boolean} Node can contain content
122 */
123 ve.Node.prototype.canContainContent = null;
124
125 /**
126 * Check if the node is content.
127 *
128 * @abstract
129 * @return {boolean} Node is content
130 */
131 ve.Node.prototype.isContent = null;
132
133 /**
134 * Check if the node is an internal node
135 *
136 * @abstract
137 * @return {boolean} Node is an internal node
138 */
139 ve.Node.prototype.isInternal = null;
140
141 /**
142 * Check if the node is a meta data node
143 *
144 * @abstract
145 * @return {boolean} Node is a meta data node
146 */
147 ve.Node.prototype.isMetaData = null;
148
149 /**
150 * Check if the node has a wrapped element in the document data.
151 *
152 * @abstract
153 * @return {boolean} Node represents a wrapped element
154 */
155 ve.Node.prototype.isWrapped = null;
156
157 /**
158 * Check if the node can be unwrapped.
159 *
160 * Can only be true of the node is wrapped.
161 *
162 * @abstract
163 * @return {boolean} Node represents a unwrappable element
164 */
165 ve.Node.prototype.isUnwrappable = null;
166
167 /**
168 * Check if the node is focusable
169 *
170 * @abstract
171 * @return {boolean} Node is focusable
172 */
173 ve.Node.prototype.isFocusable = null;
174
175 /**
176 * Check if the node is alignable
177 *
178 * @abstract
179 * @return {boolean} Node is alignable
180 */
181 ve.Node.prototype.isAlignable = null;
182
183 /**
184 * Check if the node can behave as a table cell
185 *
186 * @abstract
187 * @return {boolean} Node can behave as a table cell
188 */
189 ve.Node.prototype.isCellable = null;
190
191 /**
192 * Check the node, behaving as a table cell, can be edited in place
193 *
194 * @abstract
195 * @return {boolean} Node can be edited in place
196 */
197 ve.Node.prototype.isCellEditable = null;
198
199 /**
200 * Check if the node behaves like a list for diffing
201 *
202 * @abstract
203 * @return {boolean} Node behaves like a list
204 */
205 ve.Node.prototype.isDiffedAsList = null;
206
207 /**
208 * Check if the node behaves like a leaf for diffing
209 *
210 * @abstract
211 * @return {boolean} Node behaves like a leaf
212 */
213 ve.Node.prototype.isDiffedAsLeaf = null;
214
215 /**
216 * Check if the node behaves like a document for diffing
217 *
218 * @abstract
219 * @return {boolean} Node behaves like a document
220 */
221 ve.Node.prototype.isDiffedAsDocument = null;
222
223 /**
224 * Check if the node behaves like a tree branch for diffing
225 *
226 * This is the fallback behaviour if the node is not diffed as
227 * a list,leaf or document.
228 *
229 * @abstract
230 * @return {boolean} Node behaves like a tree branch
231 */
232 ve.Node.prototype.isDiffedAsTree = function () {
233 return !this.isDiffedAsList() && !this.isDiffedAsLeaf() && !this.isDiffedAsDocument();
234 };
235
236 /**
237 * Check if the node has significant whitespace.
238 *
239 * Can only be true if canContainContent is also true.
240 *
241 * @abstract
242 * @return {boolean} Node has significant whitespace
243 */
244 ve.Node.prototype.hasSignificantWhitespace = null;
245
246 /**
247 * Check if the node handles its own children
248 *
249 * @abstract
250 * @return {boolean} Node handles its own children
251 */
252 ve.Node.prototype.handlesOwnChildren = null;
253
254 /**
255 * Check if the node's children should be ignored.
256 *
257 * @abstract
258 * @return {boolean} Node's children should be ignored
259 */
260 ve.Node.prototype.shouldIgnoreChildren = null;
261
262 /**
263 * Get the length of the node.
264 *
265 * @abstract
266 * @return {number} Node length
267 */
268 ve.Node.prototype.getLength = null;
269
270 /**
271 * Get the offset of the node within the document.
272 *
273 * If the node has no parent than the result will always be 0.
274 *
275 * @abstract
276 * @return {number} Offset of node
277 * @throws {Error} Node not found in parent's children array
278 */
279 ve.Node.prototype.getOffset = null;
280
281 /**
282 * Get the range inside the node.
283 *
284 * @param {boolean} backwards Return a backwards range
285 * @return {ve.Range} Inner node range
286 */
287 ve.Node.prototype.getRange = function ( backwards ) {
288 const offset = this.getOffset() + ( this.isWrapped() ? 1 : 0 ),
289 range = new ve.Range( offset, offset + this.getLength() );
290 return backwards ? range.flip() : range;
291 };
292
293 /**
294 * Get the outer range of the node, which includes wrappers if present.
295 *
296 * @param {boolean} backwards Return a backwards range
297 * @return {ve.Range} Node outer range
298 */
299 ve.Node.prototype.getOuterRange = function ( backwards ) {
300 const range = new ve.Range( this.getOffset(), this.getOffset() + this.getOuterLength() );
301 return backwards ? range.flip() : range;
302 };
303
304 /**
305 * Get the outer length of the node, which includes wrappers if present.
306 *
307 * @return {number} Node outer length
308 */
309 ve.Node.prototype.getOuterLength = function () {
310 return this.getLength() + ( this.isWrapped() ? 2 : 0 );
311 };
312
313 /* Methods */
314
315 /**
316 * Get the symbolic node type name.
317 *
318 * @return {string} Symbolic name of element type
319 */
320 ve.Node.prototype.getType = function () {
321 return this.type;
322 };
323
324 /**
325 * Get a reference to the node's parent.
326 *
327 * @return {ve.Node|null} Reference to the node's parent, null if detached
328 */
329 ve.Node.prototype.getParent = function () {
330 return this.parent;
331 };
332
333 /**
334 * Get the root node of the tree the node is currently attached to.
335 *
336 * @return {ve.Node|null} Root node, null if detached
337 */
338 ve.Node.prototype.getRoot = function () {
339 return this.root;
340 };
341
342 /**
343 * Set the root node.
344 *
345 * This method is overridden by nodes with children.
346 *
347 * @param {ve.Node|null} root Node to use as root
348 * @fires ve.Node#root
349 * @fires ve.Node#unroot
350 */
351 ve.Node.prototype.setRoot = function ( root ) {
352 const oldRoot = this.root;
353 if ( root === oldRoot ) {
354 return;
355 }
356 if ( oldRoot ) {
357 this.root = null;
358 this.emit( 'unroot', oldRoot );
359 }
360 this.root = root;
361 if ( root ) {
362 this.emit( 'root', root );
363 }
364 };
365
366 /**
367 * Get the document the node is a part of.
368 *
369 * @return {ve.Document|null} Document the node is a part of, null if detached
370 */
371 ve.Node.prototype.getDocument = function () {
372 return this.doc;
373 };
374
375 /**
376 * Set the document the node is a part of.
377 *
378 * This method is overridden by nodes with children.
379 *
380 * @param {ve.Document|null} doc Document this node is a part of
381 */
382 ve.Node.prototype.setDocument = function ( doc ) {
383 const oldDoc = this.doc;
384 if ( doc === oldDoc ) {
385 return;
386 }
387 if ( oldDoc ) {
388 this.doc = null;
389 oldDoc.nodeDetached( this );
390 }
391 this.doc = doc;
392 if ( doc ) {
393 doc.nodeAttached( this );
394 }
395 };
396
397 /**
398 * Attach the node to another as a child.
399 *
400 * @param {ve.Node} parent Node to attach to
401 * @fires ve.Node#attach
402 */
403 ve.Node.prototype.attach = function ( parent ) {
404 this.parent = parent;
405 this.setDocument( parent.getDocument() );
406 this.setRoot( parent.getRoot() );
407 this.emit( 'attach', parent );
408 };
409
410 /**
411 * Detach the node from its parent.
412 *
413 * @fires ve.Node#detach
414 */
415 ve.Node.prototype.detach = function () {
416 const parent = this.parent;
417 this.parent = null;
418 this.setRoot( null );
419 this.setDocument( null );
420 this.emit( 'detach', parent );
421 };
422
423 /**
424 * Traverse tree of nodes (model or view) upstream.
425 *
426 * For each traversed node, the callback function will be passed the traversed node as a parameter.
427 *
428 * @param {Function} callback Callback method to be called for every traversed node. Returning false stops the traversal.
429 * @return {ve.Node|null} Node which caused the traversal to stop, or null if it didn't
430 */
431 ve.Node.prototype.traverseUpstream = function ( callback ) {
432 let node = this;
433 while ( node ) {
434 if ( callback( node ) === false ) {
435 return node;
436 }
437 node = node.getParent();
438 }
439 return null;
440 };
441
442 /**
443 * Traverse upstream until a parent of a specific type is found
444 *
445 * @param {Function} type Node type to find
446 * @return {ve.Node|null} Ancestor of this node matching the specified type
447 */
448 ve.Node.prototype.findParent = function ( type ) {
449 return this.traverseUpstream( ( node ) => !( node instanceof type ) );
450 };
451
452 /**
453 * Traverse upstream and collect all nodes, including the node itself.
454 *
455 * @return {ve.Node[]} List of nodes which are upstream of the current node
456 */
457 ve.Node.prototype.collectUpstream = function () {
458 const nodes = [];
459 this.traverseUpstream( ( node ) => {
460 nodes.push( node );
461 } );
462 return nodes;
463 };
464
465 /**
466 * Check if the current node is a descendant of (or equal to) a specific node.
467 *
468 * @param {ve.Node} upstreamNode Parent node to check for
469 * @return {boolean} Current node is a descendant
470 */
471 ve.Node.prototype.isDownstreamOf = function ( upstreamNode ) {
472 return this.traverseUpstream( ( node ) => node !== upstreamNode ) !== null;
473 };
474
475 /**
476 * Get the offset path from the document node to this node.
477 *
478 * @return {number[]|null} The offset path, or null if not attached to a DocumentNode
479 */
480 ve.Node.prototype.getOffsetPath = function () {
481 let node = this;
482 const path = [];
483
484 while ( true ) {
485 if ( node.type === 'document' ) {
486 // We reached the ve.dm.DocumentNode/ve.ce.DocumentNode that this node is attached to
487 return path;
488 }
489 const parent = node.getParent();
490 if ( !parent ) {
491 return null;
492 }
493 path.unshift( parent.indexOf( node ) );
494 node = parent;
495 }
496 };