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

ElementApi#updateSourceElement will throw an error if ElementApi#sourceElement is undefined.

Kamil Piechaczek 7 лет назад
Родитель
Сommit
d4ffa94257

+ 10 - 0
packages/ckeditor5-core/src/editor/utils/elementapimixin.js

@@ -3,6 +3,7 @@
  * For licensing, see LICENSE.md.
  */
 
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement';
 
 /**
@@ -20,6 +21,15 @@ const ElementApiMixin = {
 	 * @inheritDoc
 	 */
 	updateSourceElement() {
+		if ( !this.sourceElement ) {
+			/**
+			 * The `sourceElement` is required by the `ElementApi` interface.
+			 *
+			 * @error elementapimixin-missing-sourceelement
+			 */
+			throw new CKEditorError( 'elementapi-missing-sourceelement: The "sourceElement" is required by the "ElementApi" interface.' );
+		}
+
 		setDataInElement( this.sourceElement, this.data.get() );
 	}
 };

+ 6 - 1
packages/ckeditor5-core/tests/editor/utils/elementapimixin.js

@@ -9,6 +9,7 @@ import ElementApiMixin from '../../../src/editor/utils/elementapimixin';
 import Editor from '../../../src/editor/editor';
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 describe( 'ElementApiMixin', () => {
 	let editor;
@@ -27,7 +28,7 @@ describe( 'ElementApiMixin', () => {
 		editor.destroy();
 	} );
 
-	describe( 'updateEditorElement()', () => {
+	describe( 'updateSourceElement()', () => {
 		it( 'should be added to editor interface', () => {
 			expect( editor ).have.property( 'updateSourceElement' ).to.be.a( 'function' );
 		} );
@@ -43,5 +44,9 @@ describe( 'ElementApiMixin', () => {
 
 			expect( editorElement.innerHTML ).to.equal( 'foo bar' );
 		} );
+
+		it( 'throws an error if "sourceElement" has not set', () => {
+			expect( () => editor.updateSourceElement() ).to.throw( CKEditorError, /elementapi-missing-sourceelement/ );
+		} );
 	} );
 } );