r103478 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103477‎ | r103478 | r103479 >
Date:18:00, 17 November 2011
Author:catrope
Status:deferred
Tags:
Comment:
Add an extra offset parameter to selectNodes() and use it in the recursion, so the results of recursive calls don't need to have their globalRanges translated
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js
@@ -282,11 +282,12 @@
283283 * @method
284284 * @param {es.Range} range Range to select nodes within
285285 * @param {Boolean} [shallow] Do not recurse into child nodes of child nodes
 286+ * @param {Number} [offset] Used for recursive invocations. Callers should not pass this parameter
286287 * @returns {Array} List of objects with 'node', 'range' and 'globalRange' properties describing nodes which are
287288 * covered by the range and the range within the node that is covered. If an entire node is covered, 'range' is
288289 * absent but 'globalRange' is still set
289290 */
290 -es.DocumentBranchNode.prototype.selectNodes = function( range, shallow ) {
 291+es.DocumentBranchNode.prototype.selectNodes = function( range, shallow, offset ) {
291292 if ( typeof range === 'undefined' ) {
292293 range = new es.Range( 0, this.model.getContentLength() );
293294 } else {
@@ -301,8 +302,8 @@
302303 end = range.end,
303304 startInside,
304305 endInside,
305 - childNode,
306 - result;
 306+ childNode;
 307+ offset = offset || 0;
307308
308309 if ( start < 0 ) {
309310 throw 'The start offset of the range is negative';
@@ -314,7 +315,7 @@
315316 if ( end > this.getContentLength() ) {
316317 throw 'The end offset of the range is past the end of the node';
317318 }
318 - return [{ 'node': this, 'range': new es.Range( start, end ), 'globalRange': new es.Range( start, end ) }];
 319+ return [{ 'node': this, 'range': new es.Range( start, end ), 'globalRange': new es.Range( start + offset, end + offset ) }];
319320 }
320321
321322 // This node has children, loop over them
@@ -326,7 +327,7 @@
327328
328329 if ( start == end && ( start == left - 1 || start == right + 1 ) ) {
329330 // Empty range outside of any node
330 - return [{ 'node': this, 'range': new es.Range( start, end ), 'globalRange': new es.Range( start, end ) }];
 331+ return [{ 'node': this, 'range': new es.Range( start, end ), 'globalRange': new es.Range( start + offset, end + offset ) }];
331332 }
332333
333334 startInside = start >= left && start <= right; // is the start inside childNode?
@@ -344,19 +345,12 @@
345346 {
346347 'node': childNode,
347348 'range': new es.Range( start - left, end - left ),
348 - 'globalRange': new es.Range( start, end )
 349+ 'globalRange': new es.Range( start + offset, end + offset )
349350 }
350351 ];
351352 } else {
352353 // Recurse into childNode
353 - nodes = childNode.selectNodes( new es.Range( start - left, end - left ) );
354 - // Adjust globalRange
355 - // TODO: do this with an extra parameter
356 - for ( j = 0; j < nodes.length; j++ ) {
357 - if ( nodes[j].globalRange !== undefined ) {
358 - nodes[j].globalRange = es.Range.newFromTranslatedRange( nodes[j].globalRange, left );
359 - }
360 - }
 354+ nodes = childNode.selectNodes( new es.Range( start - left, end - left ), false, left + offset );
361355 }
362356 // Since the start and end are both inside childNode, we know for sure that we're
363357 // done, so return
@@ -368,18 +362,10 @@
369363 nodes.push( {
370364 'node': childNode,
371365 'range': new es.Range( start - left, right - left ),
372 - 'globalRange': new es.Range( start, right )
 366+ 'globalRange': new es.Range( start + offset, right + offset )
373367 } );
374368 } else {
375 - result = childNode.selectNodes( new es.Range( start - left, right - left ) );
376 - // Adjust globalRange
377 - // TODO: do this with an extra parameter
378 - for ( j = 0; j < result.length; j++ ) {
379 - if ( result[j].globalRange !== undefined ) {
380 - result[j].globalRange = es.Range.newFromTranslatedRange( result[j].globalRange, left );
381 - }
382 - }
383 - nodes = nodes.concat( result );
 369+ nodes = nodes.concat( childNode.selectNodes( new es.Range( start - left, right - left ), false, left + offset ) );
384370 }
385371 } else if ( endInside ) {
386372 // The end is inside childNode but the start isn't
@@ -388,18 +374,10 @@
389375 nodes.push( {
390376 'node': childNode,
391377 'range': new es.Range( 0, end - left ),
392 - 'globalRange': new es.Range( left, end )
 378+ 'globalRange': new es.Range( left + offset, end + offset )
393379 } );
394380 } else {
395 - result = childNode.selectNodes( new es.Range( 0, end - left ) );
396 - // Adjust globalRange
397 - // TODO: do this with an extra parameter
398 - for ( j = 0; j < result.length; j++ ) {
399 - if ( result[j].globalRange !== undefined ) {
400 - result[j].globalRange = es.Range.newFromTranslatedRange( result[j].globalRange, left );
401 - }
402 - }
403 - nodes = nodes.concat( result );
 381+ nodes = nodes.concat( childNode.selectNodes( new es.Range( 0, end - left ), false, left + offset ) );
404382 }
405383 // We've found the end, so we're done
406384 return nodes;
@@ -407,19 +385,19 @@
408386 // end is between childNode and this.children[i+1]
409387 // start is not inside childNode, so the selection covers
410388 // all of childNode, then ends
411 - nodes.push( { 'node': childNode, 'globalRange': new es.Range( left - 1, right + 1 ) } );
 389+ nodes.push( { 'node': childNode, 'globalRange': new es.Range( left - 1 + offset, right + 1 + offset ) } );
412390 // We've reached the end so we're done
413391 return nodes;
414392 } else if ( start == left - 1 ) {
415393 // start is between this.children[i-1] and childNode
416394 // end is not inside childNode, so the selection covers
417395 // all of childNode and more
418 - nodes.push( { 'node': childNode, 'globalRange': new es.Range( left - 1, right + 1 ) } );
 396+ nodes.push( { 'node': childNode, 'globalRange': new es.Range( left - 1 + offset, right + 1 + offset ) } );
419397 } else if ( nodes.length > 0 ) {
420398 // Neither the start nor the end is inside childNode, but nodes is non-empty,
421399 // so childNode must be between the start and the end
422400 // Add the entire node, so no range property
423 - nodes.push( { 'node': childNode, 'globalRange': new es.Range( left - 1, right + 1 ) } );
 401+ nodes.push( { 'node': childNode, 'globalRange': new es.Range( left - 1 + offset, right + 1 + offset ) } );
424402 }
425403
426404 // Move left to the start of this.children[i+1] for the next iteration

Status & tagging log