Browse Source

View's EditableElement throws when trying to set document more than once.

Szymon Kupś 9 years ago
parent
commit
cd1a7c7504

+ 18 - 11
packages/ckeditor5-engine/src/view/editableelement.js

@@ -8,7 +8,7 @@
  */
 
 import ContainerElement from './containerelement';
-
+import CKEditorError from 'ckeditor5-utils/src/ckeditorerror';
 import mix from 'ckeditor5-utils/src/mix';
 import ObservableMixin from 'ckeditor5-utils/src/observablemixin';
 
@@ -47,23 +47,30 @@ export default class EditableElement extends ContainerElement {
 		 * @member {Boolean} module:engine/view/editableelement~EditableElement#isFocused
 		 */
 		this.set( 'isFocused', false );
+
+		/**
+		 * {@link module:engine/view/document~Document} that is an owner of this root.
+		 * Can only by set once, throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-editableelement-document-already-set`
+		 * when document is already set.
+		 *
+		 * @member {module:engine/view/document~Document} module:engine/view/rooteditableelement~RootEditableElement#document
+		 */
 	}
 
-	/**
-	 * Gets {@link module:engine/view/document~Document View document} reference that owns this editable element.
-	 *
-	 * @type {module:engine/view/document~Document}
-	 */
 	get document() {
 		return this.getCustomProperty( documentSymbol );
 	}
 
-	/**
-	 * Sets {@link module:engine/view/document~Document} that is an owner of this root.
-	 *
-	 * @type {module:engine/view/document~Document}
-	 */
 	set document( document ) {
+		if ( this.getCustomProperty( documentSymbol ) ) {
+			/**
+			 * Cannot set {@link module:engine/view/document~Document} if one is already present.
+			 *
+			 * @error view-editableelement-document-already-set
+			 */
+			throw new CKEditorError( 'view-editableelement-document-already-set: View document is already set.' );
+		}
+
 		this.setCustomProperty( documentSymbol, document );
 
 		this.bind( 'isFocused' ).to(

+ 29 - 0
packages/ckeditor5-engine/tests/view/editableelement.js

@@ -4,10 +4,39 @@
  */
 
 import createDocumentMock from 'ckeditor5-engine/tests/view/_utils/createdocumentmock';
+import CKEditorError from 'ckeditor5-utils/src/ckeditorerror';
 import RootEditableElement from 'ckeditor5-engine/src/view/rooteditableelement';
 import Range from 'ckeditor5-engine/src/view/range';
 
 describe( 'EditableElement', () => {
+	describe( 'document', () => {
+		let element, docMock;
+
+		beforeEach( () => {
+			element = new RootEditableElement( 'div' );
+			docMock = createDocumentMock();
+		} );
+
+		it( 'should allow to set document', () => {
+			element.document = docMock;
+
+			expect( element.document ).to.equal( docMock );
+		} );
+
+		it( 'should return undefined if document is not set', () => {
+			expect( element.document ).to.be.undefined;
+		} );
+
+		it( 'should throw if trying to set document again', () => {
+			element.document = docMock;
+			const newDoc = createDocumentMock();
+
+			expect( () => {
+				element.document = newDoc;
+			} ).to.throw( CKEditorError, 'view-editableelement-document-already-set: View document is already set.' );
+		} );
+	} );
+
 	describe( 'isFocused', () => {
 		let docMock, viewMain, viewHeader;