From 7a4e4a1c37a56440736b61a4c16328e7bbf6b771 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Tue, 7 May 2019 15:34:45 +0200 Subject: [PATCH 1/6] Prevent of creation decoupled editor in textarea. --- src/decouplededitor.js | 45 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/src/decouplededitor.js b/src/decouplededitor.js index 0f6f9b4..adacae7 100644 --- a/src/decouplededitor.js +++ b/src/decouplededitor.js @@ -213,31 +213,30 @@ export default class DecoupledEditor 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().' - ); - } + 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 ); + const initialData = config.initialData || getInitialData( sourceElementOrData ); - return editor.data.init( initialData ); - } ) - .then( () => editor.fire( 'ready' ) ) - .then( () => editor ) - ); - } ); + return editor.data.init( initialData ); + } ) + .then( () => editor.fire( 'ready' ) ) + .then( () => editor ); } } From cade493e25ed6c8e9eab974733d03b737cb423ef Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Tue, 7 May 2019 15:44:54 +0200 Subject: [PATCH 2/6] Add unit test Check if decoupled editor throws an error when is created in textarea --- tests/decouplededitor.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/decouplededitor.js b/tests/decouplededitor.js index 8e40316..0776ffa 100644 --- a/tests/decouplededitor.js +++ b/tests/decouplededitor.js @@ -134,6 +134,21 @@ describe( 'DecoupledEditor', () => { } ); } ); + it( 'throws error if it is initialized in textarea', done => { + DecoupledEditor.create( document.createElement( 'textarea' ) ) + .then( + () => { + expect.fail( 'Decoupled 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