Переглянути джерело

Simplfied the Document#getRootNames implementation. Property makes more sense.

Piotrek Koszuliński 9 роки тому
батько
коміт
e09ccd744b

+ 10 - 13
packages/ckeditor5-engine/src/treemodel/document.js

@@ -105,6 +105,16 @@ export default class Document {
 		return this.getRoot( graveyardSymbol );
 	}
 
+	/**
+	 * Gets names of all roots (without the {@link core.treeModel.Document#graveyard}).
+	 *
+	 * @readonly
+	 * @type {Iterable.<String>}
+	 */
+	get rootNames() {
+		return Array.from( this._roots.keys() ).filter( ( name ) => name != graveyardSymbol );
+	}
+
 	/**
 	 * This is the entry point for all document changes. All changes on the document are done using
 	 * {@link core.treeModel.operation.Operation operations}. To create operations in the simple way use the
@@ -227,19 +237,6 @@ export default class Document {
 		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;
-			}
-		}
-	}
-
 	/**
 	 * Custom toJSON method to solve child-parent circular dependencies.
 	 *

+ 13 - 13
packages/ckeditor5-engine/tests/treemodel/document/document.js

@@ -30,6 +30,19 @@ describe( 'Document', () => {
 		} );
 	} );
 
+	describe( 'rootNames', () => {
+		it( 'should return empty iterator if no roots exist', () => {
+			expect( utils.count( doc.rootNames ) ).to.equal( 0 );
+		} );
+
+		it( 'should return an iterator of all roots without the graveyard', () => {
+			doc.createRoot( 'a' );
+			doc.createRoot( 'b' );
+
+			expect( Array.from( doc.rootNames ) ).to.deep.equal( [ 'a', 'b' ] );
+		} );
+	} );
+
 	describe( 'createRoot', () => {
 		it( 'should create a new RootElement, add it to roots map and return it', () => {
 			let root = doc.createRoot( 'root', 'root' );
@@ -67,19 +80,6 @@ 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();