Skip to content
Merged
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
30 changes: 30 additions & 0 deletions packages/dom/src/dom/get-rectangle-from-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ export default function getRectangleFromRange( range ) {
return null;
}

// A collapsed range at a soft line wrap is equally ambiguous: the same
// position ends one line and starts the next. Some browsers (Gecko)
// return a single rectangle for it, on the upper line, regardless of
// where the caret is. Detect the boundary by measuring the characters
// around the position: when they sit on different lines, there is no
// way to know which line the caret is on, so don't return anything.
if (
rects.length === 1 &&
startContainer.nodeType === startContainer.TEXT_NODE &&
range.startOffset > 0 &&
range.startOffset < /** @type {Text} */ ( startContainer ).length
) {
assertIsDefined( ownerDocument, 'ownerDocument' );
const measure = (
/** @type {number} */ start,
/** @type {number} */ end
) => {
const charRange = ownerDocument.createRange();
charRange.setStart( startContainer, start );
charRange.setEnd( startContainer, end );
return charRange.getBoundingClientRect();
};
const before = measure( range.startOffset - 1, range.startOffset );
const after = measure( range.startOffset, range.startOffset + 1 );

if ( before.bottom <= after.top ) {
return null;
}
}

let rect = rects[ 0 ];

// If the collapsed range starts (and therefore ends) at an element node,
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/specs/editor/various/writing-flow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ test.describe( 'Writing Flow (@firefox, @webkit)', () => {
<!-- /wp:paragraph -->` );
} );

test( 'should move to the start of the first line on ArrowUp (-firefox)', async ( {
test( 'should move to the start of the first line on ArrowUp', async ( {
page,
editor,
} ) => {
Expand Down
Loading