Browse Source

Merge pull request #283 from ckeditor/t/282

Implemented Document#getRootNames.
Piotr Jasiun 9 năm trước cách đây
mục cha
commit
d0631e3291

+ 34 - 21
packages/ckeditor5-engine/src/treemodel/document.js

@@ -147,27 +147,27 @@ export default class Document {
 	/**
 	 * Creates a new top-level root.
 	 *
-	 * @param {String|Symbol} id Unique root id.
-	 * @param {String} name Element name.
+	 * @param {String|Symbol} rootName Unique root name.
+	 * @param {String} elementName Element name.
 	 * @returns {core.treeModel.RootElement} Created root.
 	 */
-	createRoot( id, name ) {
-		if ( this._roots.has( id ) ) {
+	createRoot( rootName, elementName ) {
+		if ( this._roots.has( rootName ) ) {
 			/**
-			 * Root with specified id already exists.
+			 * Root with specified name already exists.
 			 *
-			 * @error document-createRoot-id-exists
+			 * @error document-createRoot-name-exists
 			 * @param {core.treeModel.Document} doc
-			 * @param {String} id
+			 * @param {String} name
 			 */
 			throw new CKEditorError(
-				'document-createRoot-id-exists: Root with specified id already exists.',
-				{ id: id }
+				'document-createRoot-name-exists: Root with specified name already exists.',
+				{ name: rootName }
 			);
 		}
 
-		const root = new RootElement( this, name );
-		this._roots.set( id, root );
+		const root = new RootElement( this, elementName );
+		this._roots.set( rootName, root );
 
 		return root;
 	}
@@ -205,26 +205,39 @@ export default class Document {
 	}
 
 	/**
-	 * Returns top-level root by it's id.
+	 * Returns top-level root by its name.
 	 *
-	 * @param {String|Symbol} id Unique root id.
-	 * @returns {core.treeModel.RootElement} Root registered under given id.
+	 * @param {String|Symbol} name Unique root name.
+	 * @returns {core.treeModel.RootElement} Root registered under given name.
 	 */
-	getRoot( id ) {
-		if ( !this._roots.has( id ) ) {
+	getRoot( name ) {
+		if ( !this._roots.has( name ) ) {
 			/**
-			 * Root with specified id does not exist.
+			 * Root with specified name does not exist.
 			 *
 			 * @error document-getRoot-root-not-exist
-			 * @param {String} id
+			 * @param {String} name
 			 */
 			throw new CKEditorError(
-				'document-getRoot-root-not-exist: Root with specified id does not exist.',
-				{ id: id }
+				'document-getRoot-root-not-exist: Root with specified name does not exist.',
+				{ name: name }
 			);
 		}
 
-		return this._roots.get( id );
+		return this._roots.get( name );
+	}
+
+	/**
+	 * Gets iterator of names of all roots (without the {@link core.treeModel.Document#graveyard}).
+	 *
+	 * @returns {Iterator.<String>}
+	 */
+	*getRootNames() {
+		for ( let rootName of this._roots.keys() ) {
+			if ( rootName != graveyardSymbol ) {
+				yield rootName;
+			}
+		}
 	}
 
 	/**

+ 17 - 3
packages/ckeditor5-engine/tests/treemodel/document/document.js

@@ -11,6 +11,7 @@ import Document from '/ckeditor5/core/treemodel/document.js';
 import RootElement from '/ckeditor5/core/treemodel/rootelement.js';
 import Batch from '/ckeditor5/core/treemodel/batch.js';
 import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
+import utils from '/ckeditor5/utils/utils.js';
 
 describe( 'Document', () => {
 	let doc;
@@ -38,19 +39,19 @@ describe( 'Document', () => {
 			expect( root.getChildCount() ).to.equal( 0 );
 		} );
 
-		it( 'should throw an error when trying to create a second root with the same id', () => {
+		it( 'should throw an error when trying to create a second root with the same name', () => {
 			doc.createRoot( 'root', 'root' );
 
 			expect(
 				() => {
 					doc.createRoot( 'root', 'root' );
 				}
-			).to.throw( CKEditorError, /document-createRoot-id-exists/ );
+			).to.throw( CKEditorError, /document-createRoot-name-exists/ );
 		} );
 	} );
 
 	describe( 'getRoot', () => {
-		it( 'should return a RootElement previously created with given id', () => {
+		it( 'should return a RootElement previously created with given name', () => {
 			let newRoot = doc.createRoot( 'root' );
 			let getRoot = doc.getRoot( 'root' );
 
@@ -66,6 +67,19 @@ describe( 'Document', () => {
 		} );
 	} );
 
+	describe( 'getRootNames', () => {
+		it( 'should return empty iterator if no roots exist', () => {
+			expect( utils.count( doc.getRootNames() ) ).to.equal( 0 );
+		} );
+
+		it( 'should return an iterator of all roots without the graveyard', () => {
+			doc.createRoot( 'a' );
+			doc.createRoot( 'b' );
+
+			expect( Array.from( doc.getRootNames() ) ).to.deep.equal( [ 'a', 'b' ] );
+		} );
+	} );
+
 	describe( 'applyOperation', () => {
 		it( 'should increase document version, execute operation and fire event with proper data', () => {
 			const changeCallback = sinon.spy();