8
0
فهرست منبع

Merge pull request #32 from ckeditor/t/ckeditor5/1591

Feature: `DecoupledEditor.create()` will throw an error, when textarea element is used.
Maciej 6 سال پیش
والد
کامیت
0e8e55d4d8

+ 8 - 1
packages/ckeditor5-editor-decoupled/src/decouplededitor.js

@@ -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(
@@ -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: ' +

+ 28 - 3
packages/ckeditor5-editor-decoupled/tests/decouplededitor.js

@@ -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 ) {