Преглед изворни кода

Implemented Document#getRootNames.

Piotrek Koszuliński пре 9 година
родитељ
комит
0b2960806c

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

@@ -227,6 +227,19 @@ 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.
 	 *

+ 14 - 0
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;
@@ -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();