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

Merge branch 't/743' into t/764

Szymon Kupś 9 лет назад
Родитель
Сommit
c3f9b0b977

+ 1 - 1
packages/ckeditor5-engine/package.json

@@ -10,7 +10,7 @@
   },
   "devDependencies": {
     "@ckeditor/ckeditor5-dev-lint": "^2.0.0",
-    "ckeditor5-core": "ckeditor/ckeditor5-core#engine/t/743",
+    "ckeditor5-core": "ckeditor/ckeditor5-core",
     "gulp": "^3.9.0",
     "guppy-pre-commit": "^0.4.0"
   },

+ 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(

+ 1 - 1
packages/ckeditor5-engine/src/view/element.js

@@ -73,7 +73,7 @@ export default class Element extends Node {
 		 */
 		this._children = [];
 
-		if ( children && ( children instanceof Node || Array.from( children ).length > 0 ) ) {
+		if ( children ) {
 			this.insertChildren( 0, children );
 		}
 

+ 10 - 20
packages/ckeditor5-engine/src/view/emptyelement.js

@@ -9,6 +9,7 @@
 
 import Element from './element';
 import CKEditorError from 'ckeditor5-utils/src/ckeditorerror';
+import Node from './node';
 
 /**
  * EmptyElement class. It is used to represent elements that cannot contain any child nodes.
@@ -35,34 +36,23 @@ export default class EmptyElement extends Element {
 		this.getFillerOffset = getFillerOffset;
 	}
 
-	/**
-	 * Overrides {@link module:engine/view/element~Element#appendChildren} method.
-	 * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-emptyelement-cannot-add` to prevent adding any child nodes
-	 * to EmptyElement.
-	 */
-	appendChildren() {
-		throwCannotAdd();
-	}
-
 	/**
 	 * Overrides {@link module:engine/view/element~Element#insertChildren} method.
 	 * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-emptyelement-cannot-add` to prevent adding any child nodes
 	 * to EmptyElement.
 	 */
-	insertChildren() {
-		throwCannotAdd();
+	insertChildren( index, nodes ) {
+		if ( nodes && ( nodes instanceof Node || Array.from( nodes ).length > 0 ) ) {
+			/**
+			 * Cannot add children to {@link module:engine/view/emptyelement~EmptyElement}.
+			 *
+			 * @error view-emptyelement-cannot-add
+			 */
+			throw new CKEditorError( 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.' );
+		}
 	}
 }
 
-function throwCannotAdd() {
-	/**
-	 * Cannot add children to {@link module:engine/view/emptyelement~EmptyElement}.
-	 *
-	 * @error view-emptyelement-cannot-add
-	 */
-	throw new CKEditorError( 'view-emptyelement-cannot-add: Cannot add child nodes to EmptyElement instance.' );
-}
-
 // Returns `null` because block filler is not needed for EmptyElements.
 //
 // @returns {null}

+ 2 - 12
packages/ckeditor5-engine/src/view/rooteditableelement.js

@@ -30,28 +30,18 @@ export default class RootEditableElement extends EditableElement {
 		super( name );
 
 		/**
-		 * Name of this root inside {@link module:engine/view/document~Document} that is an owner of this root.
+		 * Name of this root inside {@link module:engine/view/document~Document} that is an owner of this root. If no
+		 * other name is set, `main` name is used.
 		 *
-		 * @readonly
 		 * @member {String}
 		 */
 		this.rootName = 'main';
 	}
 
-	/**
-	 * Gets root name inside parent {@link module:engine/view/document~Document}.
-	 *
-	 * @type {String}
-	 */
 	get rootName() {
 		return this.getCustomProperty( rootNameSymbol );
 	}
 
-	/**
-	 * Sets root name inside parent {@link module:engine/view/document~Document}.
-	 *
-	 * @type {String}
-	 */
 	set rootName( rootName ) {
 		this.setCustomProperty( rootNameSymbol, rootName );
 	}

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

@@ -4,10 +4,46 @@
  */
 
 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.' );
+		} );
+
+		it( 'should be cloned properly', () => {
+			element.document = docMock;
+			const newElement = element.clone();
+
+			expect( newElement.document ).to.equal( docMock );
+		} );
+	} );
+
 	describe( 'isFocused', () => {
 		let docMock, viewMain, viewHeader;
 

+ 11 - 0
packages/ckeditor5-engine/tests/view/rooteditableelement.js

@@ -37,4 +37,15 @@ describe( 'RootEditableElement', () => {
 			expect( root.isReadOnly ).to.be.false;
 		} );
 	} );
+
+	it( 'should be cloned properly', () => {
+		const root = new RootEditableElement( 'h1' );
+		root.document = createDocumentMock();
+		root.rootName = 'header';
+
+		const newRoot = root.clone();
+
+		expect( newRoot.document ).to.equal( root.document );
+		expect( newRoot.rootName ).to.equal( root.rootName );
+	} );
 } );