Răsfoiți Sursa

Change check function from promise to synchronous.

Mateusz Samsel 6 ani în urmă
părinte
comite
54f0bf7bb8

+ 19 - 24
packages/ckeditor5-core/src/editor/editor.js

@@ -271,36 +271,31 @@ export default class Editor {
 	}
 
 	/**
-	 * Methods creates rejected promise when provided parameter is a HTML Textarea element.
-	 * It resolves in any other case.
+	 * Throws `editor-wrong-element` when checked element is `<textarea>`.
 	 *
+	 * @protected
 	 * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
 	 * or the editor's initial data.
-	 * @returns {Promise}
 	 */
-	static allowedElement( sourceElementOrData ) {
+	static _assertAllowedSourceElement( sourceElementOrData ) {
 		if ( isElement( sourceElementOrData ) && sourceElementOrData.tagName.toLowerCase() === 'textarea' ) {
-			return Promise.reject(
-
-				/**
-				 * This error is thrown when a user tries to use a `<textarea>` element to create a non-classic editor in it.
-				 *
-				 * Textarea element represents a plain-text and cannot be used as a editable root element with included CKEditor5.
-				 * Content of an editor should be nicely present to the user and show him how it's going to looks like. Textarea element
-				 * doesn't support such behavior.
-				 *
-				 * Only {@glink builds/guides/overview#classic-editor Classic Editor} has implemented a special system, which
-				 * **replace** DOM element and load data from it
-				 * ({@link module:editor-classic/classiceditor~ClassicEditor.create more information}). All other editors
-				 * use an existing element, load data from it and make this element editable. Details about behaviour of each editor
-				 * might be found in an associated description of a `create` method of each editor.
-				 *
-				 * @error editor-wrong-element
-				 */
-				new CKEditorError( 'editor-wrong-element: This type of editor cannot be initialized inside <textarea> element.' )
-			);
+			/**
+			 * This error is thrown when a user tries to use a `<textarea>` element to create a non-classic editor in it.
+			 *
+			 * Textarea element represents a plain-text and cannot be used as a editable root element with included CKEditor5.
+			 * Content of an editor should be nicely present to the user and show him how it's going to looks like. Textarea element
+			 * doesn't support such behavior.
+			 *
+			 * Only {@glink builds/guides/overview#classic-editor Classic Editor} has implemented a special system, which
+			 * **replace** DOM element and load data from it
+			 * ({@link module:editor-classic/classiceditor~ClassicEditor.create more information}). All other editors
+			 * use an existing element, load data from it and make this element editable. Details about behaviour of each editor
+			 * might be found in an associated description of a `create` method of each editor.
+			 *
+			 * @error editor-wrong-element
+			 */
+			throw new CKEditorError( 'editor-wrong-element: This type of editor cannot be initialized inside <textarea> element.' );
 		}
-		return Promise.resolve();
 	}
 
 	/**

+ 18 - 32
packages/ckeditor5-core/tests/editor/editor.js

@@ -774,46 +774,32 @@ describe( 'Editor', () => {
 		} );
 	} );
 
-	describe( 'allowedElement()', () => {
-		it( 'should return resolved promise for non-html data', done => {
-			const sourceElementOrData = Editor.allowedElement( 'some initial data' );
-
-			expect( sourceElementOrData ).to.be.a( 'promise' );
+	describe( '_assertAllowedSourceElement()', () => {
+		it( 'should pass for non-html data', () => {
+			const notThrowingFn = () => {
+				Editor._assertAllowedSourceElement( 'some initial data' );
+			};
 
-			sourceElementOrData
-				.then( done )
-				.catch( done );
+			expect( notThrowingFn ).to.not.throw();
 		} );
 
-		it( 'should return resolved promise for non-textarea html element', done => {
+		it( 'should pass non-textarea element', () => {
 			const element = document.createElement( 'div' );
-			const sourceElementOrData = Editor.allowedElement( element );
-
-			expect( sourceElementOrData ).to.be.a( 'promise' );
+			const notThrowingFn = () => {
+				Editor._assertAllowedSourceElement( element );
+			};
 
-			sourceElementOrData
-				.then( done )
-				.catch( done );
+			expect( notThrowingFn ).to.not.throw();
 		} );
 
-		it( 'should return rejected promise for textarea html element', done => {
+		it( 'should throw error for textarea element', () => {
 			const element = document.createElement( 'textarea' );
-			const sourceElementOrData = Editor.allowedElement( element );
-
-			expect( sourceElementOrData ).to.be.a( 'promise' );
-
-			sourceElementOrData
-				.then(
-					() => {
-						expect.fail( 'This promise should be rejected.' );
-					},
-					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 );
+			const throwingFn = () => {
+				Editor._assertAllowedSourceElement( element );
+			};
+
+			expect( throwingFn ).to
+				.throw( CKEditorError, /^editor-wrong-element: This type of editor cannot be initialized inside <textarea> element\./ );
 		} );
 	} );
 } );