浏览代码

Dropped Document#hasRoot method in favor of Document#getRoot.

Oskar Wróbel 8 年之前
父节点
当前提交
1122f7b1df

+ 3 - 17
packages/ckeditor5-engine/src/model/document.js

@@ -188,25 +188,11 @@ export default class Document {
 	 * Returns top-level root by its name.
 	 *
 	 * @param {String} [name='main'] Unique root name.
-	 * @returns {module:engine/model/rootelement~RootElement} Root registered under given name.
+	 * @returns {module:engine/model/rootelement~RootElement|null} Root registered under given name or null when
+	 * there is no root of given name.
 	 */
 	getRoot( name = 'main' ) {
-		const root = this.roots.get( name );
-
-		if ( !root ) {
-			/**
-			 * Root with specified name does not exist.
-			 *
-			 * @error model-document-getRoot-root-not-exist
-			 * @param {String} name
-			 */
-			throw new CKEditorError(
-				'model-document-getRoot-root-not-exist: Root with specified name does not exist.',
-				{ name }
-			);
-		}
-
-		return root;
+		return this.roots.get( name );
 	}
 
 	/**

+ 2 - 1
packages/ckeditor5-engine/src/view/document.js

@@ -217,7 +217,8 @@ export default class Document {
 	 * specific "main" root is returned.
 	 *
 	 * @param {String} [name='main'] Name of the root.
-	 * @returns {module:engine/view/rooteditableelement~RootEditableElement} The view root element with the specified name.
+	 * @returns {module:engine/view/rooteditableelement~RootEditableElement|null} The view root element with the specified name
+	 * or null when there is no root of given name.
 	 */
 	getRoot( name = 'main' ) {
 		return this.roots.get( name );

+ 11 - 10
packages/ckeditor5-engine/tests/model/document/document.js

@@ -163,19 +163,20 @@ describe( 'Document', () => {
 	} );
 
 	describe( 'getRoot()', () => {
-		it( 'should return a RootElement previously created with given name', () => {
-			const newRoot = doc.createRoot();
-			const getRoot = doc.getRoot();
+		it( 'should return a RootElement with default "main" name', () => {
+			const newRoot = doc.createRoot( 'main' );
 
-			expect( getRoot ).to.equal( newRoot );
+			expect( doc.getRoot() ).to.equal( newRoot );
 		} );
 
-		it( 'should throw an error when trying to get non-existent root', () => {
-			expect(
-				() => {
-					doc.getRoot( 'root' );
-				}
-			).to.throw( CKEditorError, /model-document-getRoot-root-not-exist/ );
+		it( 'should return a RootElement with custom name', () => {
+			const newRoot = doc.createRoot( 'custom', 'custom' );
+
+			expect( doc.getRoot( 'custom' ) ).to.equal( newRoot );
+		} );
+
+		it( 'should return null when trying to get non-existent root', () => {
+			expect( doc.getRoot( 'not-existing' ) ).to.null;
 		} );
 	} );
 

+ 4 - 0
packages/ckeditor5-engine/tests/view/document/document.js

@@ -160,6 +160,10 @@ describe( 'Document', () => {
 
 			expect( viewDocument.getRoot( 'header' ) ).to.equal( viewDocument.roots.get( 'header' ) );
 		} );
+
+		it( 'should return null when trying to get non-existent root', () => {
+			expect( viewDocument.getRoot( 'not-existing' ) ).to.null;
+		} );
 	} );
 
 	describe( 'addObserver()', () => {