From 71234284cd454993b139ce065a9ab2db431abce8 Mon Sep 17 00:00:00 2001 From: Jim Fisher Date: Sat, 9 Oct 2021 13:06:06 +0100 Subject: [PATCH] Fix bug: setting selection from contentEditable:false element causes crash Fixes https://github.com/ianstormtaylor/slate/issues/4583 When clicking some text in a `contentEditable:false` element, if the handler for this sets the selection, Slate crashes. Slate tries to find a Slate range for the text that was clicked on, but there is no such range, because the text is inside a `contentEditable:false` element. Slate seems to be making a bad assumption that the current DOM selection necessarily corresponds to a Slate range, but this is not the case if the user just clicked into an element with `contentEditable: false`. To fix this, I changed `exactMatch: false` to `exactMatch: true`, which seems to mean "fail gracefully if there is no exact match". --- packages/slate-react/src/components/editable.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index 872bec892..23ca847e3 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -182,7 +182,9 @@ export const Editable = (props: EditableProps) => { // If the DOM selection is in the editor and the editor selection is already correct, we're done. if (hasDomSelection && hasDomSelectionInEditor && selection) { const slateRange = ReactEditor.toSlateRange(editor, domSelection, { - exactMatch: false, + // domSelection is not necessarily a valid Slate range + // (e.g. when clicking on contentEditable:false element) + exactMatch: true }) if (slateRange && Range.equals(slateRange, selection)) { return