mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-13 10:44:02 +02:00
Update findDocumentOrShadowRoot
to return the root node instead of throwing unnecessarily (#4427)
* Update `findDocumentOrShadowRoot` to return undefined instead of throwing unnecessarily * Small refactoring to improve the diff for reviewers * Add changeset for patch * Update new-trainers-peel.md * Resolve PR comments * Revert undefined checks, return window.document and update changeset * Simplify findDocumentOrShadowRoot based on PR feedback * Re-run CI Thanks everyone for your review and thanks @ben10code for your first contribution!
This commit is contained in:
5
.changeset/new-trainers-peel.md
Normal file
5
.changeset/new-trainers-peel.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate-react': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix crash when unmounting an editor rendered within a React portal. The issue was arising at unmount time, because `getRootNode` returned the dettached portal node and it is not an instance of `Document` or `ShadowRoot`. As a fix, `getDocumentOrShadowRoot` has been refactored to return a root node instead of throwing. In sum, this patch fixes a regression bug introduced by https://github.com/ianstormtaylor/slate/pull/3749/
|
@@ -106,23 +106,14 @@ export const ReactEditor = {
|
|||||||
const el = ReactEditor.toDOMNode(editor, editor)
|
const el = ReactEditor.toDOMNode(editor, editor)
|
||||||
const root = el.getRootNode()
|
const root = el.getRootNode()
|
||||||
|
|
||||||
// The below exception will always be thrown for iframes because the document inside an iframe
|
if (
|
||||||
// does not inherit it's prototype from the parent document, therefore we return early
|
(root instanceof Document || root instanceof ShadowRoot) &&
|
||||||
if (el.ownerDocument !== document) return el.ownerDocument
|
root.getSelection != null
|
||||||
|
) {
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
if (!(root instanceof Document || root instanceof ShadowRoot))
|
return el.ownerDocument
|
||||||
throw new Error(
|
|
||||||
`Unable to find DocumentOrShadowRoot for editor element: ${el}`
|
|
||||||
)
|
|
||||||
|
|
||||||
// COMPAT: Only Chrome implements the DocumentOrShadowRoot mixin for
|
|
||||||
// ShadowRoot; other browsers still implement it on the Document
|
|
||||||
// interface. (2020/08/08)
|
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot#Properties
|
|
||||||
if (root.getSelection === undefined && el.ownerDocument !== null)
|
|
||||||
return el.ownerDocument
|
|
||||||
|
|
||||||
return root
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user