r103466 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103465‎ | r103466 | r103467 >
Date:15:01, 17 November 2011
Author:catrope
Status:deferred
Tags:
Comment:
Make selectNodes() also descend (recurse) into child nodes when only the start or only the end is in the middle of a child node. Without this, it was stuff like ranges with only openings and no closings.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js (modified) (history)
  • /trunk/extensions/VisualEditor/tests/es/es.DocumentBranchNode.test.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/es/es.DocumentBranchNode.test.js
@@ -139,7 +139,7 @@
140140 }
141141 } );
142142
143 -test( 'es.DocumentBranchNode.selectNodes', 75, function() {
 143+test( 'es.DocumentBranchNode.selectNodes', 77, function() {
144144
145145 // selectNodes tests
146146
@@ -839,7 +839,7 @@
840840 'input': new es.Range( 2, 10 ),
841841 'output': [
842842 { 'node': big.children[0], 'range': new es.Range( 1, 3 ), 'globalRange': new es.Range( 2, 4 ) },
843 - { 'node': big.children[1], 'range': new es.Range( 0, 4 ), 'globalRange': new es.Range( 6, 10 ) }
 843+ { 'node': big.children[1].children[0].children[0].children[0], 'range': new es.Range( 0, 1 ), 'globalRange': new es.Range( 9, 10 ) }
844844 ],
845845 'desc': 'Select from before the b to after the d'
846846 },
@@ -860,7 +860,8 @@
861861 'input': new es.Range( 9, 20 ),
862862 'output': [
863863 { 'node': big.children[1].children[0].children[0].children[0], 'range': new es.Range( 0, 1 ), 'globalRange': new es.Range( 9, 10 ) },
864 - { 'node': big.children[1].children[0].children[0].children[1], 'range': new es.Range( 0, 8 ), 'globalRange': new es.Range( 12, 20 ) }
 864+ { 'node': big.children[1].children[0].children[0].children[1].children[0], 'globalRange': new es.Range( 12, 17 ) },
 865+ { 'node': big.children[1].children[0].children[0].children[1].children[1].children[0], 'range': new es.Range( 0, 1 ), 'globalRange': new es.Range( 19, 20 ) }
865866 ],
866867 'desc': 'Select from before the d to after the f, with recursion'
867868 },
@@ -873,6 +874,27 @@
874875 { 'node': big.children[1], 'range': new es.Range( 3, 14 ), 'globalRange': new es.Range( 9, 20 ) }
875876 ],
876877 'desc': 'Select from before the d to after the f, without recursion'
 878+ },
 879+ // Test 76
 880+ {
 881+ 'node': big,
 882+ 'input': new es.Range( 3, 9 ),
 883+ 'output': [
 884+ { 'node': big.children[0], 'range': new es.Range( 2, 3 ), 'globalRange': new es.Range( 3, 4 ) },
 885+ { 'node': big.children[1].children[0].children[0].children[0], 'range': new es.Range( 0, 0 ), 'globalRange': new es.Range( 9, 9 ) }
 886+ ],
 887+ 'desc': 'Select from before the c to before the d'
 888+ },
 889+ // Test 77
 890+ {
 891+ 'node': big,
 892+ 'input': new es.Range( 3, 9 ),
 893+ 'shallow': true,
 894+ 'output': [
 895+ { 'node': big.children[0], 'range': new es.Range( 2, 3 ), 'globalRange': new es.Range( 3, 4 ) },
 896+ { 'node': big.children[1], 'range': new es.Range( 0, 3 ), 'globalRange': new es.Range( 6, 9 ) }
 897+ ],
 898+ 'desc': 'Select from before the c to before the d, without recursion'
877899 }
878900 ];
879901
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js
@@ -301,7 +301,8 @@
302302 end = range.end,
303303 startInside,
304304 endInside,
305 - childNode;
 305+ childNode,
 306+ result;
306307
307308 if ( start < 0 ) {
308309 throw 'The start offset of the range is negative';
@@ -350,6 +351,7 @@
351352 // Recurse into childNode
352353 nodes = childNode.selectNodes( new es.Range( start - left, end - left ) );
353354 // Adjust globalRange
 355+ // TODO: do this with an extra parameter
354356 for ( j = 0; j < nodes.length; j++ ) {
355357 if ( nodes[j].globalRange !== undefined ) {
356358 nodes[j].globalRange = es.Range.newFromTranslatedRange( nodes[j].globalRange, left );
@@ -361,20 +363,44 @@
362364 return nodes;
363365 } else if ( startInside ) {
364366 // The start is inside childNode but the end isn't
365 - // Add a range from the start of the range to the end of childNode
366 - nodes.push( {
367 - 'node': childNode,
368 - 'range': new es.Range( start - left, right - left ),
369 - 'globalRange': new es.Range( start, right )
370 - } );
 367+ if ( shallow || !childNode.children ) {
 368+ // Add a range from the start of the range to the end of childNode
 369+ nodes.push( {
 370+ 'node': childNode,
 371+ 'range': new es.Range( start - left, right - left ),
 372+ 'globalRange': new es.Range( start, right )
 373+ } );
 374+ } 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 );
 384+ }
371385 } else if ( endInside ) {
372386 // The end is inside childNode but the start isn't
373 - // Add a range from the start of childNode to the end of the range
374 - nodes.push( {
375 - 'node': childNode,
376 - 'range': new es.Range( 0, end - left ),
377 - 'globalRange': new es.Range( left, end )
378 - } );
 387+ if ( shallow || !childNode.children ) {
 388+ // Add a range from the start of childNode to the end of the range
 389+ nodes.push( {
 390+ 'node': childNode,
 391+ 'range': new es.Range( 0, end - left ),
 392+ 'globalRange': new es.Range( left, end )
 393+ } );
 394+ } 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 );
 404+ }
379405 // We've found the end, so we're done
380406 return nodes;
381407 } else if ( end == right + 1 ) {

Status & tagging log