Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #32 from ckeditor/t/ckeditor5/1591
Browse files Browse the repository at this point in the history
Feature: `DecoupledEditor.create()` will throw an error, when textarea element is used.
  • Loading branch information
jodator authored Jun 12, 2019
2 parents 5d0c0d7 + e49f6cf commit af4daea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/decouplededitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ export default class DecoupledEditor extends Editor {
*/
static create( sourceElementOrData, config = {} ) {
return new Promise( resolve => {
const isHTMLElement = isElement( sourceElementOrData );

if ( isHTMLElement && sourceElementOrData.tagName === 'TEXTAREA' ) {
// Documented in core/editor/editor.js
throw new CKEditorError( 'editor-wrong-element: This type of editor cannot be initialized inside <textarea> element.' );
}

const editor = new this( sourceElementOrData, config );

resolve(
Expand All @@ -222,7 +229,7 @@ export default class DecoupledEditor extends Editor {
editor.ui.init();
} )
.then( () => {
if ( !isElement( sourceElementOrData ) && config.initialData ) {
if ( !isHTMLElement && config.initialData ) {
// Documented in core/editor/editorconfig.jdoc.
throw new CKEditorError(
'editor-create-initial-data: ' +
Expand Down
31 changes: 28 additions & 3 deletions tests/decouplededitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,34 @@ describe( 'DecoupledEditor', () => {
DecoupledEditor.create( '<p>Hello world!</p>', {
initialData: '<p>I am evil!</p>',
plugins: [ Paragraph ]
} ).catch( () => {
done();
} );
} )
.then(
() => {
expect.fail( 'Decoupled editor should throw an error when both initial data are passed' );
},
err => {
expect( err ).to.be.an( 'error' ).with.property( 'message' ).and
// eslint-disable-next-line max-len
.match( /^editor-create-initial-data: The config\.initialData option cannot be used together with initial data passed in Editor\.create\(\)\./ );
}
)
.then( done )
.catch( done );
} );

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 <textarea> element\./ );
}
)
.then( done )
.catch( done );
} );

function test( getElementOrData ) {
Expand Down

0 comments on commit af4daea

Please sign in to comment.