8
0
Pārlūkot izejas kodu

Simplified the code and docs. Added more tests.

Kamil Piechaczek 7 gadi atpakaļ
vecāks
revīzija
6751aedfc1

+ 9 - 11
packages/ckeditor5-editor-classic/src/classiceditor.js

@@ -84,9 +84,7 @@ export default class ClassicEditor extends Editor {
 	}
 
 	/**
-	 * An HTML element that represents the whole editor (with UI). See the {@link module:core/editor/editorwithui~EditorWithUI} interface.
-	 *
-	 * @returns {HTMLElement|null}
+	 * @inheritDoc
 	 */
 	get element() {
 		return this.ui.view.element;
@@ -100,7 +98,10 @@ export default class ClassicEditor extends Editor {
 	 * @returns {Promise}
 	 */
 	destroy() {
-		this.updateSourceElement();
+		if ( this.sourceElement ) {
+			this.updateSourceElement();
+		}
+
 		this._elementReplacer.restore();
 		this.ui.destroy();
 
@@ -193,19 +194,16 @@ export default class ClassicEditor extends Editor {
 					.then( () => editor.ui.init() )
 					.then( () => {
 						if ( isElement( sourceElementOrData ) ) {
-							editor._elementReplacer.replace( sourceElementOrData, editor.ui.view.element );
+							editor._elementReplacer.replace( sourceElementOrData, editor.element );
 						}
 
 						editor.fire( 'uiReady' );
 					} )
 					.then( () => editor.editing.view.attachDomRoot( editor.ui.view.editableElement ) )
 					.then( () => {
-						if ( editor.sourceElement ) {
-							editor.data.init( getDataFromElement( editor.sourceElement ) );
-						} else {
-							editor.data.init( sourceElementOrData );
-							editor.sourceElement = editor.ui.view.element;
-						}
+						editor.data.init(
+							isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData
+						);
 					} )
 					.then( () => {
 						editor.fire( 'dataReady' );

+ 43 - 8
packages/ckeditor5-editor-classic/tests/classiceditor.js

@@ -100,14 +100,6 @@ describe( 'ClassicEditor', () => {
 			} );
 		} );
 
-		it( 'editor.element should be equal to editor.ui.view.element when data is passed', () => {
-			return ClassicEditor.create( '<p>Hello world!</p>', {
-				plugins: [ Paragraph ]
-			} ).then( editor => {
-				expect( editor.element ).to.equal( editor.ui.view.element );
-			} );
-		} );
-
 		describe( 'ui', () => {
 			it( 'creates the UI using BoxedEditorUI classes', () => {
 				expect( editor.ui ).to.be.instanceof( ClassicEditorUI );
@@ -157,6 +149,18 @@ describe( 'ClassicEditor', () => {
 				} );
 		} );
 
+		it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
+			return ClassicEditor
+				.create( '<p>Foo.</p>', {
+					plugins: [ Paragraph, Bold ]
+				} )
+				.then( newEditor => {
+					expect( newEditor.sourceElement ).to.be.undefined;
+
+					return newEditor.destroy();
+				} );
+		} );
+
 		describe( 'ui', () => {
 			it( 'inserts editor UI next to editor element', () => {
 				expect( editor.ui.view.element.previousSibling ).to.equal( editorElement );
@@ -165,6 +169,20 @@ describe( 'ClassicEditor', () => {
 			it( 'attaches editable UI as view\'s DOM root', () => {
 				expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.view.editable.element );
 			} );
+
+			it( 'editor.element points to the editor\'s UI when editor was initialized on the DOM element', () => {
+				expect( editor.element ).to.equal( editor.ui.view.element );
+			} );
+
+			it( 'editor.element points to the editor\'s UI when editor was initialized with data', () => {
+				return ClassicEditor.create( '<p>Hello world!</p>', {
+					plugins: [ Paragraph ]
+				} ).then( editor => {
+					expect( editor.element ).to.equal( editor.ui.view.element );
+
+					return editor.destroy();
+				} );
+			} );
 		} );
 	} );
 
@@ -263,6 +281,23 @@ describe( 'ClassicEditor', () => {
 				} );
 		} );
 
+		it( 'does not update the source element if editor was initialized with data', () => {
+			return ClassicEditor
+				.create( '<p>Foo.</p>', {
+					plugins: [ Paragraph, Bold ]
+				} )
+				.then( newEditor => {
+					const spy = sinon.stub( newEditor, 'updateSourceElement' );
+
+					return newEditor.destroy()
+						.then( () => {
+							expect( spy.called ).to.be.false;
+
+							spy.restore();
+						} );
+				} );
+		} );
+
 		it( 'restores the editor element', () => {
 			expect( editor.sourceElement.style.display ).to.equal( 'none' );