Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor BranchNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Branch node mixin.
9 *
10 * Extenders are expected to inherit from ve.Node.
11 *
12 * Branch nodes are immutable, which is why there are no methods for adding or removing children.
13 * DataModel classes will add this functionality, and other subclasses will implement behavior that
14 * mimics changes made to DataModel nodes.
15 *
16 * @class
17 * @abstract
18 * @constructor
19 * @param {ve.Node[]} children Array of children to add
20 */
21 ve.BranchNode = function VeBranchNode( children ) {
22 this.children = Array.isArray( children ) ? children : [];
23 };
24
25 /* Setup */
26
27 OO.initClass( ve.BranchNode );
28
29 /* Methods */
30
31 /**
32 * Traverse a branch node depth-first.
33 *
34 * @param {Function} callback Callback to execute for each traversed node
35 * @param {ve.Node} callback.node Node being traversed
36 */
37 ve.BranchNode.prototype.traverse = function ( callback ) {
38 const children = this.getChildren();
39
40 for ( let i = 0, len = children.length; i < len; i++ ) {
41 callback.call( this, children[ i ] );
42 if ( children[ i ].hasChildren() ) {
43 children[ i ].traverse( callback );
44 }
45 }
46 };
47
48 /**
49 * Check if the node has children.
50 *
51 * @return {boolean} Whether the node has children
52 */
53 ve.BranchNode.prototype.hasChildren = function () {
54 return true;
55 };
56
57 /**
58 * Get child nodes.
59 *
60 * @return {ve.Node[]} List of child nodes
61 */
62 ve.BranchNode.prototype.getChildren = function () {
63 return this.children;
64 };
65
66 /**
67 * Get the index of a child node.
68 *
69 * @param {ve.dm.Node} node Child node to find index of
70 * @return {number} Index of child node or -1 if node was not found
71 */
72 ve.BranchNode.prototype.indexOf = function ( node ) {
73 return this.children.indexOf( node );
74 };
75
76 /**
77 * Set the root node.
78 *
79 * @see ve.Node#setRoot
80 * @param {ve.BranchNode|null} root Node to use as root
81 */
82 ve.BranchNode.prototype.setRoot = function ( root ) {
83 const oldRoot = this.root;
84 if ( root === oldRoot ) {
85 // Nothing to do, don't recurse into all descendants
86 return;
87 }
88 if ( oldRoot ) {
89 // Null the root, then recurse into children, then emit unroot.
90 // That way, at emit time, all this node's ancestors and descendants have
91 // null root.
92 this.root = null;
93 for ( let i = 0, len = this.children.length; i < len; i++ ) {
94 this.children[ i ].setRoot( null );
95 }
96 this.emit( 'unroot', oldRoot );
97 }
98 this.root = root;
99 if ( root ) {
100 // We've set the new root, so recurse into children, then emit root.
101 // That way, at emit time, all this node's ancestors and descendants have
102 // the new root.
103 for ( let i = 0, len = this.children.length; i < len; i++ ) {
104 this.children[ i ].setRoot( root );
105 }
106 this.emit( 'root', root );
107 }
108 };
109
110 /**
111 * Set the document the node is a part of.
112 *
113 * @see ve.Node#setDocument
114 * @param {ve.Document} doc Document this node is a part of
115 */
116 ve.BranchNode.prototype.setDocument = function ( doc ) {
117 const oldDoc = this.doc;
118 if ( doc === this.doc ) {
119 // Nothing to do, don't recurse into all descendants
120 return;
121 }
122 let i, len;
123 if ( oldDoc ) {
124 // Null the doc, then recurse into children, then notify the doc.
125 // That way, at notify time, all this node's ancestors and descendants have
126 // null doc.
127 this.doc = null;
128 for ( i = 0, len = this.children.length; i < len; i++ ) {
129 this.children[ i ].setDocument( null );
130 }
131 oldDoc.nodeDetached( this );
132 }
133 this.doc = doc;
134 if ( doc ) {
135 // We've set the new doc, so recurse into children, then notify the doc.
136 // That way, at notify time, all this node's ancestors and descendants have
137 // the new doc.
138 for ( i = 0, len = this.children.length; i < len; i++ ) {
139 this.children[ i ].setDocument( doc );
140 }
141 doc.nodeAttached( this );
142 }
143 };
144
145 /**
146 * Get a node from an offset.
147 *
148 * This method is pretty expensive. If you need to get different slices of the same content, get
149 * the content first, then slice it up locally.
150 *
151 * @param {number} offset Offset get node for
152 * @param {boolean} [shallow] Do not iterate into child nodes of child nodes
153 * @return {ve.Node|null} Node at offset, or null if none was found
154 * @throws {Error} If offset is out of bounds
155 */
156 ve.BranchNode.prototype.getNodeFromOffset = function ( offset, shallow ) {
157 let currentNode = this;
158 if ( typeof offset !== 'number' ) {
159 throw new Error( 'Offset must be a number' );
160 }
161 if ( offset === 0 ) {
162 return currentNode;
163 }
164 if ( offset < 0 ) {
165 throw new Error( 'Offset out of bounds' );
166 }
167 let nodeOffset = 0;
168 // TODO a lot of logic is duplicated in selectNodes(), abstract that into a traverser or something
169 SIBLINGS:
170 while ( currentNode.children.length ) {
171 for ( let i = 0, length = currentNode.children.length; i < length; i++ ) {
172 const childNode = currentNode.children[ i ];
173 if ( offset === nodeOffset ) {
174 // The requested offset is right before childNode, so it's not
175 // inside any of currentNode's children, but is inside currentNode
176 return currentNode;
177 }
178 if ( childNode instanceof ve.ce.InternalListNode ) {
179 break SIBLINGS;
180 }
181 const nodeLength = childNode.getOuterLength();
182 if ( offset >= nodeOffset && offset < nodeOffset + nodeLength ) {
183 if ( !shallow && childNode.hasChildren() && childNode.getChildren().length ) {
184 // One of the children contains the node; increment to
185 // enter the node, then iterate through children
186 nodeOffset += 1;
187 currentNode = childNode;
188 continue SIBLINGS;
189 } else {
190 return childNode;
191 }
192 }
193 nodeOffset += nodeLength;
194 }
195 if ( offset === nodeOffset ) {
196 // The requested offset is right before currentNode.children[i], so it's
197 // not inside any of currentNode's children, but is inside currentNode
198 return currentNode;
199 }
200 }
201 return null;
202 };