1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-04-19 21:01:57 +02:00

Fix iframe crash (#4219)

* fixes #4170

* add smoke test for iframe example

* add changeset

* update changeset wording
This commit is contained in:
Julian Krispel-Samsel 2021-04-26 14:47:12 +01:00 committed by GitHub
parent c70e30f83d
commit 737aaa9cde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
'slate-react': patch
---
Fixes error that occurs when Editor is rendered inside iframe

View File

@ -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')
})
})

View File

@ -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}`