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 iframe crash #4219

Merged
merged 5 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thick-apricots-fly.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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