Sfoglia il codice sorgente

Added: engine.treeView.DocumentFragment iterator.

Szymon Cofalik 9 anni fa
parent
commit
4a1cc024db

+ 8 - 1
packages/ckeditor5-engine/src/treeview/documentfragment.js

@@ -34,6 +34,13 @@ export default class DocumentFragment {
 	}
 
 	/**
+	 * Iterates over nodes added to this DocumentFragment.
+	 */
+	[ Symbol.iterator ]() {
+		return this._children[ Symbol.iterator ]();
+	}
+
+	/**
 	 * {@link engine.treeView.DocumentFragment#insertChildren Insert} a child node or a list of child nodes at the end
 	 * and sets the parent of these nodes to this fragment.
 	 *
@@ -122,4 +129,4 @@ export default class DocumentFragment {
 
 		return this._children.splice( index, howMany );
 	}
-}
+}

+ 14 - 1
packages/ckeditor5-engine/tests/treeview/documentfragment.js

@@ -38,6 +38,19 @@ describe( 'DocumentFragment', () => {
 		} );
 	} );
 
+	describe( 'iterator', () => {
+		it( 'should iterate over all nodes added to document fragment', () => {
+			const children = [ new Element( 'p' ), new Element( 'div' ) ];
+			const fragment = new DocumentFragment( children );
+
+			const arr = Array.from( fragment );
+
+			expect( arr.length ).to.equal( 2 );
+			expect( arr[ 0 ] ).to.have.property( 'name' ).that.equals( 'p' );
+			expect( arr[ 1 ] ).to.have.property( 'name' ).that.equals( 'div' );
+		} );
+	} );
+
 	describe( 'children manipulation methods', () => {
 		let fragment, el1, el2, el3, el4;
 
@@ -196,4 +209,4 @@ describe( 'DocumentFragment', () => {
 			expect( fragment.getChild( 0 ) ).to.equal( node2 );
 		} );
 	} );
-} );
+} );