Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/ve.dm.TreeCursor.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel TreeCursor class
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 // TODO identify a core of "trusted" code that is guaranteed to detect tree invalidation.
8 // Probably needs: removeNode insertNode removeText insertText
9
10 /**
11 * DataModel TreeCursor - a tree walker that tracks the path to the current position.
12 *
13 * @class
14 *
15 * @constructor
16 * @param {ve.dm.Node} root A document node or a branch root within which to walk
17 * @param {ve.dm.Node[]} liveIgnoreNodes Live array of nodes to ignore (cross without counting)
18 * @param {number} [linearOffset] The first linear model offset inside root; default 0
19 */
20 ve.dm.TreeCursor = function VeDmTreeCursor( root, liveIgnoreNodes, linearOffset ) {
21 this.root = root;
22 this.liveIgnoreNodes = liveIgnoreNodes;
23 this.path = [];
24 this.offset = 0;
25 this.nodes = [ root ];
26 this.node = root;
27 this.lastStep = null;
28 if ( linearOffset === undefined ) {
29 linearOffset = 0;
30 }
31 this.linearOffset = linearOffset;
32 };
33
34 /* Inheritance */
35
36 OO.initClass( ve.dm.TreeCursor );
37
38 /* Methods */
39
40 /**
41 * Skip past ignored nodes and text node boundaries
42 *
43 * @param {number} [tooShort] Only step into text nodes longer than this
44 */
45 ve.dm.TreeCursor.prototype.normalizeCursor = function ( tooShort ) {
46 if ( !this.node ) {
47 return;
48 }
49 if ( tooShort === undefined ) {
50 tooShort = -1;
51 }
52
53 // If at the end of a text node, step out
54 if ( this.node.type === 'text' && this.offset === this.node.length ) {
55 this.nodes.pop();
56 this.node = this.nodes[ this.nodes.length - 1 ];
57 this.offset = this.path.pop() + 1;
58 }
59 this.crossIgnoredNodes();
60
61 let item;
62 // If at the start of long enough text node, step in
63 if (
64 this.node.hasChildren() &&
65 ( item = this.node.children[ this.offset ] ) &&
66 item.type === 'text' &&
67 item.length > tooShort
68 ) {
69 this.node = item;
70 this.nodes.push( item );
71 this.path.push( this.offset );
72 this.offset = 0;
73 }
74 };
75
76 /**
77 * Cross any immediately following nodes that are in liveIgnoreNodes
78 */
79 ve.dm.TreeCursor.prototype.crossIgnoredNodes = function () {
80 let parent, nextSibling;
81 if (
82 this.node &&
83 this.node.type === 'text' &&
84 this.offset === this.node.length &&
85 ( parent = this.nodes[ this.nodes.length - 2 ] ) &&
86 ( nextSibling = parent.children[ this.path[ this.path.length - 1 ] + 1 ] ) &&
87 this.liveIgnoreNodes.indexOf( nextSibling ) !== -1
88 ) {
89 // At the end of a text node and the next node is ignored
90 this.stepOut();
91 }
92 const len = ( this.node && this.node.hasChildren() && this.node.children.length ) || 0;
93 let item;
94 while (
95 this.offset < len &&
96 ( item = this.node.children[ this.offset ] ) &&
97 this.liveIgnoreNodes.indexOf( item ) !== -1
98 ) {
99 this.offset++;
100 this.linearOffset += item.getOuterLength();
101 }
102 };
103
104 /**
105 * Validate this.linearOffset against this.node and this.offset
106 *
107 * TODO: Improve the unit tests, then move this method to the unit testing framework
108 *
109 * @throws {Error} If this.linearOffset does not match this.node and this.offset
110 */
111 ve.dm.TreeCursor.prototype.checkLinearOffset = function () {
112 let expected = this.node.getOffset();
113 if ( this.node.type === 'text' ) {
114 expected += this.offset;
115 } else {
116 if ( this.node !== this.root ) {
117 // Add 1 for the open tag
118 expected += 1;
119 }
120 if ( this.node.hasChildren() ) {
121 // Add the outer length of each crossed child
122 this.node.children.slice( 0, this.offset ).forEach( ( child ) => {
123 expected += child.getOuterLength();
124 } );
125 }
126 }
127 if ( expected !== this.linearOffset ) {
128 throw new Error( 'Linear offset does not match tree position' );
129 }
130 };
131
132 /**
133 * @typedef {Object} Step
134 * @memberof ve.dm.TreeCursor
135 * @property {string} type open|close|cross|crosstext
136 * @property {number} length Linear length of the step (integer >= 1)
137 * @property {number[]} path Offset path from the root to the node containing the stepped item
138 * @property {ve.dm.Node|null} node The node containing the stepped item
139 * @property {number} offset The offset of the stepped item within its parent
140 * @property {number} [offsetLength] Number of characters 'crosstext' passed
141 * @property {ve.dm.Node} [item] The node stepped into/out of/across (absent for 'crosstext')
142 */
143
144 /**
145 * Take a single step in the walk, consuming no more than a given linear model length
146 *
147 * A "single step" means either stepping across text content, or stepping over a node, or
148 * stepping into/out of a non-text node. (Steps into/out of text nodes happen transparently)
149 *
150 * See https://phabricator.wikimedia.org/T162762 for the algorithm
151 *
152 * @param {number} maxLength Maximum linear model length to step over (integer >= 1)
153 * @return {ve.dm.TreeCursor.Step|null} The type of step taken, or null if there are no more steps
154 */
155 ve.dm.TreeCursor.prototype.stepAtMost = function ( maxLength ) {
156 if ( !this.node ) {
157 this.lastStep = null;
158 return null;
159 }
160 // On very large pages this is a performance bottleneck, so only run in tests
161 if ( ve.test ) {
162 this.checkLinearOffset();
163 }
164 this.normalizeCursor( maxLength );
165 let length, step;
166 if ( this.node.type === 'text' ) {
167 // We cannot be the end, because we just normalized
168 length = Math.min( maxLength, this.node.length - this.offset );
169 step = {
170 type: 'crosstext',
171 length: length,
172 path: this.path.slice(),
173 node: this.node,
174 offset: this.offset,
175 offsetLength: length
176 };
177 this.offset += step.length;
178 this.lastStep = step;
179 this.linearOffset += length;
180 return step;
181 }
182 // Else not a text node
183 const childLength = this.node.hasChildren() ? this.node.children.length : 0;
184 if ( this.offset > childLength ) {
185 throw new Error( 'Offset ' + this.offset + ' > childLength ' + childLength );
186 }
187 if ( this.offset === childLength ) {
188 return this.stepOut();
189 }
190 // Else there are unpassed child nodes
191 const item = this.node.children[ this.offset ];
192 if ( item.getOuterLength() > maxLength ) {
193 return this.stepIn();
194 }
195 // Else step across this item
196 length = item.getOuterLength();
197 step = {
198 type: 'cross',
199 length: length,
200 path: this.path.slice(),
201 node: this.node,
202 offset: this.offset,
203 item: item
204 };
205 this.offset++;
206 this.lastStep = step;
207 this.linearOffset += length;
208 return step;
209 };
210
211 /**
212 * Step into the next node
213 *
214 * @return {Object} The step
215 */
216 ve.dm.TreeCursor.prototype.stepIn = function () {
217 if (
218 this.node.type === 'text' ||
219 !this.node.hasChildren() ||
220 this.offset >= this.node.children.length
221 ) {
222 throw new Error( 'No node to step into' );
223 }
224 const item = this.node.children[ this.offset ];
225 const length = item.type === 'text' ? 0 : 1;
226 const step = {
227 type: 'open',
228 length: length,
229 path: this.path.slice(),
230 node: this.node,
231 offset: this.offset,
232 item: item
233 };
234 this.path.push( this.offset );
235 this.nodes.push( item );
236 this.node = item;
237 this.offset = 0;
238 this.lastStep = step;
239 this.linearOffset += length;
240 return step;
241 };
242
243 /**
244 * Step out of the current node (skipping past any uncrossed children or text within)
245 *
246 * @return {Object|null} The step
247 */
248 ve.dm.TreeCursor.prototype.stepOut = function () {
249 const priorOffset = this.offset;
250 const item = this.nodes.pop();
251 this.node = this.nodes[ this.nodes.length - 1 ];
252 this.offset = this.path.pop();
253 if ( this.node === undefined ) {
254 // Stepped out of the root
255 this.lastStep = null;
256 return null;
257 }
258 if ( item.type === 'text' ) {
259 this.linearOffset += item.getLength() - priorOffset;
260 } else {
261 if ( item.hasChildren() ) {
262 // Increase linearOffset by the length of each child
263 item.children.slice( priorOffset ).forEach( ( child ) => {
264 this.linearOffset += child.getOuterLength();
265 } );
266 }
267 // Increase linearOffset for the close tag
268 this.linearOffset++;
269 }
270 const step = {
271 type: 'close',
272 length: item.type === 'text' ? 0 : 1,
273 path: this.path.slice(),
274 node: this.node,
275 offset: this.offset,
276 item: item
277 };
278 this.offset++;
279 this.lastStep = step;
280 return step;
281 };