Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent ReactEditor.toDOMRange crash in setDomSelection #5741

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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 @@ -333,7 +333,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 @@ -406,8 +406,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
Loading