|
399 | 399 | }
|
400 | 400 | }
|
401 | 401 |
|
402 |
| - // Fix ranges which ends after hidden selection container. |
403 |
| - function fixRangesAfterHiddenSelectionContainer( ranges, root, hiddenSelectionContainerIndex ) { |
| 402 | + // Fix ranges which may end after hidden selection container. |
| 403 | + // Note: this function may only be used if hidden selection container |
| 404 | + // is not in DOM any more. |
| 405 | + function fixRangesAfterHiddenSelectionContainer( ranges, root ) { |
404 | 406 | for ( var i = 0; i < ranges.length; ++i ) {
|
405 | 407 | range = ranges[ i ];
|
406 |
| - if ( range.endContainer.equals( root ) ) |
407 |
| - range.endOffset = Math.min( range.endOffset, hiddenSelectionContainerIndex ); |
| 408 | + if ( range.endContainer.equals( root ) ) { |
| 409 | + // We can use getChildCount() because hidden selection container is not in DOM. |
| 410 | + range.endOffset = Math.min( range.endOffset, root.getChildCount() ); |
| 411 | + } |
408 | 412 | }
|
409 | 413 | }
|
410 | 414 |
|
|
1725 | 1729 | */
|
1726 | 1730 | selectRanges: function( ranges ) {
|
1727 | 1731 | var editor = this.root.editor,
|
1728 |
| - hiddenSelectionContainer; |
| 1732 | + hadHiddenSelectionContainer = editor && editor._.hiddenSelectionContainer; |
| 1733 | + |
| 1734 | + this.reset(); |
1729 | 1735 |
|
1730 | 1736 | // Check if there's a hiddenSelectionContainer in editable at some index.
|
1731 | 1737 | // Some ranges may be anchored after the hiddenSelectionContainer and,
|
1732 | 1738 | // once the container is removed while resetting the selection, they
|
1733 |
| - // may need new endOffset (one element less within the range) (#11021). |
1734 |
| - if ( editor && ( hiddenSelectionContainer = editor._.hiddenSelectionContainer ) ) |
1735 |
| - fixRangesAfterHiddenSelectionContainer( ranges, this.root, hiddenSelectionContainer.getIndex() ); |
1736 |
| - |
1737 |
| - this.reset(); |
| 1739 | + // may need new endOffset (one element less within the range) (#11021 #11393). |
| 1740 | + if ( hadHiddenSelectionContainer ) |
| 1741 | + fixRangesAfterHiddenSelectionContainer( ranges, this.root ); |
1738 | 1742 |
|
1739 | 1743 | if ( !ranges.length )
|
1740 | 1744 | return;
|
|
0 commit comments