Skip to content

Commit

Permalink
fix: Prevent ReactEditor.toDOMRange crash in setDomSelection (#5741)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienPoupa authored Nov 19, 2024
1 parent 644ebdc commit 90fbcde
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-eels-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Fix ReactEditor.toDOMRange crash in setDomSelection
11 changes: 8 additions & 3 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export const Editable = forwardRef(
const focusNode = domSelection.focusNode
let anchorNode

// COMPAT: In firefox the normal seletion way does not work
// COMPAT: In firefox the normal selection way does not work
// (https://github.com/ianstormtaylor/slate/pull/5486#issue-1820720223)
if (IS_FIREFOX && domSelection.rangeCount > 1) {
const firstRange = domSelection.getRangeAt(0)
Expand Down Expand Up @@ -412,8 +412,13 @@ export const Editable = forwardRef(
// Otherwise the DOM selection is out of sync, so update it.
state.isUpdatingSelection = true

const newDomRange: DOMRange | null =
selection && ReactEditor.toDOMRange(editor, selection)
let newDomRange: DOMRange | null = null

try {
newDomRange = selection && ReactEditor.toDOMRange(editor, selection)
} catch (e) {
// Ignore, dom and state might be out of sync
}

if (newDomRange) {
if (ReactEditor.isComposing(editor) && !IS_ANDROID) {
Expand Down

0 comments on commit 90fbcde

Please sign in to comment.