diff --git a/packages/dom/src/dom.js b/packages/dom/src/dom.js index 8be06daaaa3203..aba561f992d593 100644 --- a/packages/dom/src/dom.js +++ b/packages/dom/src/dom.js @@ -93,7 +93,16 @@ function isEdge( container, isReverse, onlyVertical ) { return false; } - const rangeRect = getRectangleFromRange( selection.getRangeAt( 0 ) ); + const range = selection.getRangeAt( 0 ).cloneRange(); + const isForward = isSelectionForward( selection ); + const isCollapsed = selection.isCollapsed; + + // Collapse in direction of selection. + if ( ! isCollapsed ) { + range.collapse( ! isForward ); + } + + const rangeRect = getRectangleFromRange( range ); if ( ! rangeRect ) { return false; @@ -105,9 +114,9 @@ function isEdge( container, isReverse, onlyVertical ) { // Only consider the multiline selection at the edge if the direction is // towards the edge. if ( - ! selection.isCollapsed && + ! isCollapsed && rangeRect.height > lineHeight && - isSelectionForward( selection ) === isReverse + isForward === isReverse ) { return false; } @@ -213,6 +222,8 @@ export function getRectangleFromRange( range ) { // See: https://stackoverflow.com/a/6847328/995445 if ( ! rect ) { const padNode = document.createTextNode( '\u200b' ); + // Do not modify the live range. + range = range.cloneRange(); range.insertNode( padNode ); rect = range.getClientRects()[ 0 ]; padNode.parentNode.removeChild( padNode ); diff --git a/packages/e2e-tests/specs/__snapshots__/writing-flow.test.js.snap b/packages/e2e-tests/specs/__snapshots__/writing-flow.test.js.snap index 0ea51c111b7981..0e41868f3cffa7 100644 --- a/packages/e2e-tests/specs/__snapshots__/writing-flow.test.js.snap +++ b/packages/e2e-tests/specs/__snapshots__/writing-flow.test.js.snap @@ -187,3 +187,13 @@ exports[`adding blocks should not delete surrounding space when deleting a word
1 2 3
" `; + +exports[`adding blocks should not prematurely multi-select 1`] = ` +" +1
+ + + +>
+" +`; diff --git a/packages/e2e-tests/specs/adding-blocks.test.js b/packages/e2e-tests/specs/adding-blocks.test.js index 7a790862e831c4..aeafc65cd8cc39 100644 --- a/packages/e2e-tests/specs/adding-blocks.test.js +++ b/packages/e2e-tests/specs/adding-blocks.test.js @@ -66,7 +66,6 @@ describe( 'adding blocks', () => { await page.keyboard.type( 'Foo' ); await page.keyboard.press( 'ArrowUp' ); await page.keyboard.press( 'ArrowUp' ); - await pressKeyTimes( 'ArrowRight', 3 ); await pressKeyTimes( 'Delete', 6 ); await page.keyboard.type( ' text' ); diff --git a/packages/e2e-tests/specs/writing-flow.test.js b/packages/e2e-tests/specs/writing-flow.test.js index c3c4d477e13780..c9ca7bf2021df7 100644 --- a/packages/e2e-tests/specs/writing-flow.test.js +++ b/packages/e2e-tests/specs/writing-flow.test.js @@ -327,4 +327,19 @@ describe( 'adding blocks', () => { expect( await getEditedPostContent() ).toMatchSnapshot(); } ); + + it( 'should not prematurely multi-select', async () => { + await clickBlockAppender(); + await page.keyboard.type( '1' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( '><<' ); + await pressKeyWithModifier( 'shift', 'Enter' ); + await page.keyboard.type( '<<<' ); + await page.keyboard.down( 'Shift' ); + await pressKeyTimes( 'ArrowLeft', '<<\n<<<'.length ); + await page.keyboard.up( 'Shift' ); + await page.keyboard.press( 'Backspace' ); + + expect( await getEditedPostContent() ).toMatchSnapshot(); + } ); } );