Ver código fonte

Use iterator API.

Piotr Jasiun 9 anos atrás
pai
commit
e9494219f4

+ 10 - 15
packages/ckeditor5-engine/src/treemodel/range.js

@@ -342,28 +342,23 @@ export default class Range {
 		for ( let range of flatRanges ) {
 			// This loop could be much simpler as we could just iterate over siblings of node after the first
 			// position of each range. But then we would have to re-implement character merging strategy here.
-			let it = new TreeWalker( { boundaries: range, singleCharacters: singleCharacters } );
-			let step;
+			let walker = new TreeWalker( { boundaries: range, singleCharacters: singleCharacters } );
 
 			// We will only return nodes that are on same level as node after the range start. To do this,
 			// we keep "depth" counter.
 			let depth = 0;
 
-			do {
-				step = it.next();
-
-				if ( step.value ) {
-					if ( step.value.type == 'ELEMENT_START' ) {
-						depth++;
-					} else if ( step.value.type == 'ELEMENT_END' ) {
-						depth--;
-					}
+			for ( let value of walker ) {
+				if ( value.type == 'ELEMENT_START' ) {
+					depth++;
+				} else if ( value.type == 'ELEMENT_END' ) {
+					depth--;
+				}
 
-					if ( depth === 0 ) {
-						yield step.value.item;
-					}
+				if ( depth === 0 ) {
+					yield value.item;
 				}
-			} while ( !step.done );
+			}
 		}
 	}
 

+ 1 - 11
packages/ckeditor5-engine/tests/treemodel/node.js

@@ -174,17 +174,7 @@ describe( 'Node', () => {
 
 		describe( 'getAttributes', () => {
 			it( 'should return an iterator that iterates over all attributes set on the element', () => {
-				let it = node.getAttributes();
-				let attrs = [];
-
-				let step = it.next();
-
-				while ( !step.done ) {
-					attrs.push( step.value );
-					step = it.next();
-				}
-
-				expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
+				expect( Array.from( node.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
 			} );
 		} );
 	} );

+ 1 - 9
packages/ckeditor5-engine/tests/treemodel/selection.js

@@ -442,15 +442,7 @@ describe( 'Selection', () => {
 				selection.setAttribute( 'foo', 'bar' );
 				selection.setAttribute( 'abc', 'xyz' );
 
-				let it = selection.getAttributes();
-				let attrs = [];
-
-				let step = it.next();
-
-				while ( !step.done ) {
-					attrs.push( step.value );
-					step = it.next();
-				}
+				let attrs = Array.from( selection.getAttributes() );
 
 				expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
 			} );

+ 1 - 9
packages/ckeditor5-engine/tests/treemodel/textfragment.js

@@ -90,15 +90,7 @@ describe( 'TextFragment', () => {
 
 		describe( 'getAttributes', () => {
 			it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
-				let it = textFragment.getAttributes();
-				let attrs = [];
-
-				let step = it.next();
-
-				while ( !step.done ) {
-					attrs.push( step.value );
-					step = it.next();
-				}
+				let attrs = Array.from( textFragment.getAttributes() );
 
 				expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
 			} );