8
0
Просмотр исходного кода

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

Feature: `BalloonEditor.create()` will throw an error, when textarea element is used.
Maciej 6 лет назад
Родитель
Сommit
45adb77ab4

+ 8 - 1
packages/ckeditor5-editor-balloon/src/ballooneditor.js

@@ -196,6 +196,13 @@ export default class BalloonEditor 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(
@@ -204,7 +211,7 @@ export default class BalloonEditor 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: ' +

+ 29 - 35
packages/ckeditor5-editor-balloon/tests/ballooneditor.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  */
 
-/* globals document, Event */
+/* globals document */
 
 import BalloonEditorUI from '../src/ballooneditorui';
 import BalloonEditorUIView from '../src/ballooneditoruiview';
@@ -75,37 +75,6 @@ describe( 'BalloonEditor', () => {
 			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 BalloonEditor.create( textarea, {
-				plugins: [ Paragraph ]
-			} ).then( editor => {
-				expect( textarea.value ).to.equal( '' );
-
-				editor.setData( '<p>Foo</p>' );
-
-				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( '<p>Foo</p>' );
-
-				return editor.destroy().then( () => {
-					form.remove();
-				} );
-			} );
-		} );
-
 		it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
 			return BalloonEditor
 				.create( '<p>Foo.</p>', {
@@ -195,9 +164,19 @@ describe( 'BalloonEditor', () => {
 			BalloonEditor.create( '<p>Hello world!</p>', {
 				initialData: '<p>I am evil!</p>',
 				plugins: [ Paragraph ]
-			} ).catch( () => {
-				done();
-			} );
+			} )
+				.then(
+					() => {
+						expect.fail( 'Balloon 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 );
 		} );
 
 		// ckeditor/ckeditor5-editor-classic#53
@@ -226,6 +205,21 @@ describe( 'BalloonEditor', () => {
 					return newEditor.destroy();
 				} );
 		} );
+
+		it( 'throws an error when is initialized in textarea', done => {
+			BalloonEditor.create( document.createElement( 'textarea' ) )
+				.then(
+					() => {
+						expect.fail( 'Balloon 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 );
+		} );
 	} );
 
 	describe( 'create - events', () => {