Index: trunk/extensions/VisualEditor/tests/es/es.DocumentBranchNode.test.js |
— | — | @@ -139,7 +139,7 @@ |
140 | 140 | } |
141 | 141 | } ); |
142 | 142 | |
143 | | -test( 'es.DocumentBranchNode.selectNodes', 75, function() { |
| 143 | +test( 'es.DocumentBranchNode.selectNodes', 77, function() { |
144 | 144 | |
145 | 145 | // selectNodes tests |
146 | 146 | |
— | — | @@ -839,7 +839,7 @@ |
840 | 840 | 'input': new es.Range( 2, 10 ), |
841 | 841 | 'output': [ |
842 | 842 | { '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 ) } |
844 | 844 | ], |
845 | 845 | 'desc': 'Select from before the b to after the d' |
846 | 846 | }, |
— | — | @@ -860,7 +860,8 @@ |
861 | 861 | 'input': new es.Range( 9, 20 ), |
862 | 862 | 'output': [ |
863 | 863 | { '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 ) } |
865 | 866 | ], |
866 | 867 | 'desc': 'Select from before the d to after the f, with recursion' |
867 | 868 | }, |
— | — | @@ -873,6 +874,27 @@ |
874 | 875 | { 'node': big.children[1], 'range': new es.Range( 3, 14 ), 'globalRange': new es.Range( 9, 20 ) } |
875 | 876 | ], |
876 | 877 | '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' |
877 | 899 | } |
878 | 900 | ]; |
879 | 901 | |
Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js |
— | — | @@ -301,7 +301,8 @@ |
302 | 302 | end = range.end, |
303 | 303 | startInside, |
304 | 304 | endInside, |
305 | | - childNode; |
| 305 | + childNode, |
| 306 | + result; |
306 | 307 | |
307 | 308 | if ( start < 0 ) { |
308 | 309 | throw 'The start offset of the range is negative'; |
— | — | @@ -350,6 +351,7 @@ |
351 | 352 | // Recurse into childNode |
352 | 353 | nodes = childNode.selectNodes( new es.Range( start - left, end - left ) ); |
353 | 354 | // Adjust globalRange |
| 355 | + // TODO: do this with an extra parameter |
354 | 356 | for ( j = 0; j < nodes.length; j++ ) { |
355 | 357 | if ( nodes[j].globalRange !== undefined ) { |
356 | 358 | nodes[j].globalRange = es.Range.newFromTranslatedRange( nodes[j].globalRange, left ); |
— | — | @@ -361,20 +363,44 @@ |
362 | 364 | return nodes; |
363 | 365 | } else if ( startInside ) { |
364 | 366 | // 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 | + } |
371 | 385 | } else if ( endInside ) { |
372 | 386 | // 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 | + } |
379 | 405 | // We've found the end, so we're done |
380 | 406 | return nodes; |
381 | 407 | } else if ( end == right + 1 ) { |