Repository navigation
[lexical] Bug Fix: Create selection when clicking between block decorators on Firefox - #8862
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
left a comment
There was a problem hiding this comment.
Is it possible to add a browser unit or e2e that covers this?
…ators on Firefox Firefox does not produce a DOM selection when clicking in the gap between block-level decorator elements (rangeCount === 0) and does not fire a selectionchange event. This leaves Lexical without a selection, preventing the block cursor from appearing. Add two coordinating branches in LexicalEvents.ts: 1. onDocumentSelectionChange (mousedown path): clear Lexical's selection when Firefox reports domAnchorNode === null. 2. onClick: compute the correct rootElement child offset from event.clientY via midpoint comparison, then setBaseAndExtent to establish the DOM selection that $internalCreateRangeSelection picks up. Closes meta#6775
74f61f8 to
8ebabde
Compare
|
Sure, pushed one — two adjacent HRs (paragraphs removed), click the gap, verify a RangeSelection lands on root. All three browsers pass. |
|
Something in here seems to be causing a lot of firefox e2e tests to fail |
The previous approach cleared Lexical's selection in onDocumentSelectionChange whenever Firefox reported an empty DOM selection (domAnchorNode === null). This was too aggressive — Firefox can temporarily empty the DOM selection during table operations (drag-select, merge/unmerge, copy/paste), causing 52 Firefox e2e test failures. Instead, the onClick handler now directly checks domSelection.rangeCount === 0 without depending on selection being pre-cleared. The selectionchange guard is removed entirely.
|
(Updated) Yeah, the Pushed another fix on top: removed the |
The coordinate-based selection was firing for any Firefox click with rangeCount === 0, including clicks inside table cells during drag-select and merge/unmerge operations. Restrict to event.target === rootElement so only clicks directly on the editor root (between block decorators) trigger the fallback.
Description
Firefox does not produce a DOM selection when clicking in the gap between block-level decorator elements (e.g. consecutive
<hr>nodes). Chrome and Safari handle this by placing the selection onrootElementat the appropriate child offset, but Firefox leavesrangeCount === 0and does not fire aselectionchangeevent (since from its perspective nothing changed). This means Lexical never creates a selection, and the block cursor that normally appears between decorators is never shown.The fix adds two coordinating branches in
LexicalEvents.ts:In
onDocumentSelectionChange(mousedown path): when Firefox reports an empty DOM selection (domAnchorNode === null), clear Lexical's selection so the committed state reflects the empty DOM.In
onClick: whenselection === nullanddomSelection.rangeCount === 0, iteraterootElement.childNodescomparingevent.clientYagainst each child's vertical midpoint to compute the correct offset, then callsetBaseAndExtentto establish the DOM selection.$internalCreateRangeSelectionpicks it up from there, and the existing block cursor mechanism ($updateDOMBlockCursorElement) renders the cursor at the gap.Closes #6775
Design notes
Why two sites instead of onClick alone? — For click events with
detail !== 3,$internalCreateRangeSelectionclones the committed RangeSelection without re-reading the DOM. If a selection already exists when the user clicks between decorators, the clone makes$getSelection()return non-null, preventing the onClick branch from entering. The selectionchange branch nulls the committed selection first, so the subsequent onClick seesnulland fires.caretPositionFromPoint— Tested on Firefox; it returns the decorator element itself ({offsetNode: hr, offset: 0}) rather than a root-level child offset. It can't distinguish "between child 1 and child 2 of rootElement," so the manual midpoint loop is necessary.Why core, not an extension? — The block cursor mechanism itself lives in
LexicalUtils.ts(core). This fix establishes the selection that the block cursor relies on — it's event-layer plumbing, not a feature-specific behavior.Test plan
IS_FIREFOXguard prevents the new code from running)pnpm tsc --noEmit— clean