Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/dom/src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ function isSelectionForward( selection ) {
return true;
}

function normaliseRange( range ) {
range = range.cloneRange();

const { startContainer, startOffset } = range;

if (
startContainer.nodeType === startContainer.TEXT_NODE &&
startContainer.nodeValue.length === startOffset
) {
let element = startContainer;

while ( ! element.nextSibling ) {
element = element.parentNode;
}

range.setStart( element.nextSibling, 0 );
}

return range;
}

/**
* Check whether the selection is at the edge of the container. Checks for
* horizontal position by default. Set `onlyVertical` to true to check only
Expand Down Expand Up @@ -93,7 +114,7 @@ function isEdge( container, isReverse, onlyVertical ) {
return false;
}

const range = selection.getRangeAt( 0 ).cloneRange();
const range = normaliseRange( selection.getRangeAt( 0 ) );
const isForward = isSelectionForward( selection );
const isCollapsed = selection.isCollapsed;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ exports[`Writing Flow Should navigate inner blocks with arrow keys 1`] = `
<!-- /wp:paragraph -->"
`;

exports[`Writing Flow should allow selecting entire list with longer last item 1`] = `
"<!-- wp:paragraph -->
<p>a</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li></li></ul>
<!-- /wp:list -->"
`;

exports[`Writing Flow should create valid paragraph blocks when rapidly pressing Enter 1`] = `
"<!-- wp:paragraph -->
<p></p>
Expand Down
17 changes: 17 additions & 0 deletions packages/e2e-tests/specs/editor/various/writing-flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,21 @@ describe( 'Writing Flow', () => {

expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should allow selecting entire list with longer last item', async () => {
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'a' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '* b' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'cd' );
await pressKeyWithModifier( 'shift', 'ArrowUp' );
await pressKeyWithModifier( 'shift', 'ArrowUp' );

// Ensure multi selection is not triggered and selection stays within
// the list.
await page.keyboard.press( 'Backspace' );

expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );