From 0e619820992a7e366c34cd5df20ef4ca011bdf04 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Tue, 7 May 2019 15:53:19 +0200 Subject: [PATCH 1/7] Prevent of creating inline editor inside textarea. --- src/inlineeditor.js | 49 ++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/inlineeditor.js b/src/inlineeditor.js index 8d7f2a7..9902d3d 100644 --- a/src/inlineeditor.js +++ b/src/inlineeditor.js @@ -188,31 +188,30 @@ export default class InlineEditor extends Editor { * @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance. */ static create( sourceElementOrData, config = {} ) { - return new Promise( resolve => { - const editor = new this( sourceElementOrData, config ); - - resolve( - editor.initPlugins() - .then( () => { - editor.ui.init(); - } ) - .then( () => { - if ( !isElement( sourceElementOrData ) && config.initialData ) { - // Documented in core/editor/editorconfig.jdoc. - throw new CKEditorError( - 'editor-create-initial-data: ' + - 'The config.initialData option cannot be used together with initial data passed in Editor.create().' - ); - } - - const initialData = config.initialData || getInitialData( sourceElementOrData ); - - return editor.data.init( initialData ); - } ) - .then( () => editor.fire( 'ready' ) ) - .then( () => editor ) - ); - } ); + let editor; + return Editor.allowedElement( sourceElementOrData ) + .then( () => { + editor = new this( sourceElementOrData, config ); + } ) + .then( () => editor.initPlugins() ) + .then( () => { + editor.ui.init(); + } ) + .then( () => { + if ( !isElement( sourceElementOrData ) && config.initialData ) { + // Documented in core/editor/editorconfig.jdoc. + throw new CKEditorError( + 'editor-create-initial-data: ' + + 'The config.initialData option cannot be used together with initial data passed in Editor.create().' + ); + } + + const initialData = config.initialData || getInitialData( sourceElementOrData ); + + return editor.data.init( initialData ); + } ) + .then( () => editor.fire( 'ready' ) ) + .then( () => editor ); } } From cfa518895a24b2ec48e8e187ba841906c38c8c1c Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Tue, 7 May 2019 15:54:04 +0200 Subject: [PATCH 2/7] Remove incorrect unit test. --- tests/inlineeditor.js | 33 +-------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/tests/inlineeditor.js b/tests/inlineeditor.js index 76a2e49..f3ff387 100644 --- a/tests/inlineeditor.js +++ b/tests/inlineeditor.js @@ -3,7 +3,7 @@ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ -/* globals document, Event */ +/* globals document */ import InlineEditorUI from '../src/inlineeditorui'; import InlineEditorUIView from '../src/inlineeditoruiview'; @@ -68,37 +68,6 @@ describe( 'InlineEditor', () => { expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement ); } ); - it( 'handles form element', () => { - const form = document.createElement( 'form' ); - const textarea = document.createElement( 'textarea' ); - form.appendChild( textarea ); - document.body.appendChild( form ); - - // Prevents page realods in Firefox ;| - form.addEventListener( 'submit', evt => { - evt.preventDefault(); - } ); - - return InlineEditor.create( textarea, { - plugins: [ Paragraph ] - } ).then( editor => { - expect( textarea.value ).to.equal( '' ); - - editor.setData( '

Foo

' ); - - form.dispatchEvent( new Event( 'submit', { - // We need to be able to do preventDefault() to prevent page reloads in Firefox. - cancelable: true - } ) ); - - expect( textarea.value ).to.equal( '

Foo

' ); - - return editor.destroy().then( () => { - form.remove(); - } ); - } ); - } ); - it( 'should have undefined the #sourceElement if editor was initialized with data', () => { return InlineEditor.create( '

Hello world!

', { plugins: [ Paragraph ] From 9b34b1106cc935394a67b4a07bf5a03ca00af5f1 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Tue, 7 May 2019 15:56:20 +0200 Subject: [PATCH 3/7] Add unit test Check if inline editor throws an error when is created inside textarea. --- tests/inlineeditor.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/inlineeditor.js b/tests/inlineeditor.js index f3ff387..00569f3 100644 --- a/tests/inlineeditor.js +++ b/tests/inlineeditor.js @@ -185,6 +185,21 @@ describe( 'InlineEditor', () => { return newEditor.destroy(); } ); } ); + + it( 'throws an error when is initialized in textarea', done => { + InlineEditor.create( document.createElement( 'textarea' ) ) + .then( + () => { + expect.fail( 'Inline editor should throw an error when is initialized in textarea.' ); + }, + err => { + expect( err ).to.be.an( 'error' ).with.property( 'message' ).and + .match( /^editor-wrong-element: This type of editor cannot be initialized inside