Skip to content

Commit

Permalink
Fix iframe crash (#4219)
Browse files Browse the repository at this point in the history
* fixes #4170

* add smoke test for iframe example

* add changeset

* update changeset wording
  • Loading branch information
juliankrispel committed Apr 26, 2021
1 parent c70e30f commit 737aaa9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-apricots-fly.md
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Fixes error that occurs when Editor is rendered inside iframe
31 changes: 31 additions & 0 deletions cypress/integration/iframe.ts
@@ -0,0 +1,31 @@
// Taken from https://www.cypress.io/blog/2020/02/12/working-with-iframes-in-cypress/
const getIframeDocument = () => {
return cy
.get('iframe')
.its('0.contentDocument')
.should('exist')
}

const getIframeBody = () => {
return (
getIframeDocument()
.its('body')
// automatically retries until body is loaded
.should('not.be.undefined')
.then(cy.wrap)
)
}

describe('iframe editor', () => {
beforeEach(() => {
cy.visit('examples/iframe')
})

it('should be editable', () => {
getIframeBody()
.findByRole('textbox')
.type('{movetostart}')
.type('Hello World')
.should('contain.text', 'Hello World')
})
})
4 changes: 4 additions & 0 deletions packages/slate-react/src/plugin/react-editor.ts
Expand Up @@ -106,6 +106,10 @@ export const ReactEditor = {
const el = ReactEditor.toDOMNode(editor, editor)
const root = el.getRootNode()

// The below exception will always be thrown for iframes because the document inside an iframe
// does not inherit it's prototype from the parent document, therefore we return early
if (el.ownerDocument !== document) return el.ownerDocument

if (!(root instanceof Document || root instanceof ShadowRoot))
throw new Error(
`Unable to find DocumentOrShadowRoot for editor element: ${el}`
Expand Down

0 comments on commit 737aaa9

Please sign in to comment.