From 90fbcdeff58d9b1f5de13102a1198f1c7244ae0c Mon Sep 17 00:00:00 2001 From: Adrien Poupa Date: Tue, 19 Nov 2024 13:19:24 -0500 Subject: [PATCH] fix: Prevent ReactEditor.toDOMRange crash in setDomSelection (#5741) --- .changeset/serious-eels-pay.md | 5 +++++ packages/slate-react/src/components/editable.tsx | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 .changeset/serious-eels-pay.md diff --git a/.changeset/serious-eels-pay.md b/.changeset/serious-eels-pay.md new file mode 100644 index 000000000..17cc7795c --- /dev/null +++ b/.changeset/serious-eels-pay.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +Fix ReactEditor.toDOMRange crash in setDomSelection diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index 1c3252538..701b7c7b8 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -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) @@ -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) {