瀏覽代碼

Added: engine.treeModel.DocumentFragment iterator, #root, #getPath. DocumentFragment can be used in engine.treeModel.Range.createFromElement.

Szymon Cofalik 9 年之前
父節點
當前提交
b118d7b3e6

+ 27 - 0
packages/ckeditor5-engine/src/treemodel/documentfragment.js

@@ -33,6 +33,33 @@ export default class DocumentFragment {
 		}
 	}
 
+	/**
+	 * `DocumentFragment` iterator. Returns {@link engine.treeModel.Node nodes} that are added to the `DocumentFragment`.
+	 */
+	[ Symbol.iterator ]() {
+		return this._children[ Symbol.iterator ]();
+	}
+
+	/**
+	 * The root of `DocumentFragment`. Returns itself. Added for compatibility reasons with {@link engine.treeModel.Element}.
+	 *
+	 * @readonly
+	 * @type {engine.treeModel.DocumentFragment}
+	 */
+	get root() {
+		return this;
+	}
+
+	/**
+	 * Returns path to the `DocumentFragment` This is always equal to empty array and is added for compatibility reasons
+	 * with {@link engine.treeModel.Element}.
+	 *
+	 * @returns {Array} The path.
+	 */
+	getPath() {
+		return [];
+	}
+
 	/**
 	 * Gets child at the given index.
 	 *

+ 26 - 0
packages/ckeditor5-engine/tests/treemodel/documentfragment.js

@@ -27,6 +27,32 @@ describe( 'DocumentFragment', () => {
 			expect( frag.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'p' );
 			expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'y' );
 		} );
+
+		it( 'should have root property, equal to itself', () => {
+			let frag = new DocumentFragment();
+
+			expect( frag ).to.have.property( 'root' ).that.equals( frag );
+		} );
+	} );
+
+	describe( 'iterator', () => {
+		it( 'should iterate over all nodes added to document fragment', () => {
+			let frag = new DocumentFragment( [ 'x', new Element( 'p' ), 'y' ] );
+			let arr = Array.from( frag );
+
+			expect( arr.length ).to.equal( 3 );
+			expect( arr[ 0 ] ).to.have.property( 'character' ).that.equals( 'x' );
+			expect( arr[ 1 ] ).to.have.property( 'name' ).that.equals( 'p' );
+			expect( arr[ 2 ] ).to.have.property( 'character' ).that.equals( 'y' );
+		} );
+	} );
+
+	describe( 'getPath', () => {
+		it( 'should return empty array', () => {
+			let frag = new DocumentFragment( [ 'x', new Element( 'p' ), 'y' ] );
+
+			expect( frag.getPath() ).to.deep.equal( [] );
+		} );
 	} );
 
 	describe( 'insertChildren', () => {