瀏覽代碼

Renamed RangeIterator to PositionIterator and changed range (constructor parameter) to boundary, it is optional now.

Piotr Jasiun 10 年之前
父節點
當前提交
1d6b5b0ba2

+ 25 - 12
packages/ckeditor5-utils/src/document/rangeiterator.js

@@ -19,27 +19,35 @@ CKEDITOR.define( [
 	 *
 	 * @class document.Range
 	 */
-	class RangeIterator {
+	class PositionIterator {
 		/**
 		 * Create a range iterator.
 		 *
-		 * @param {document.range} range Range to define boundaries of the iterator.
+		 * @param {document.range} boundaries Range to define boundaries of the iterator.
 		 */
-		constructor( range, iteratorPosition ) {
+		constructor( boundaries, iteratorPosition ) {
 			/**
 			 * Start position.
 			 *
 			 * @type {document.Position}
 			 */
-			this.range = range;
-
-			this.position = iteratorPosition || range.start;
+			if ( boundaries instanceof Position ) {
+				this.position = boundaries;
+			} else {
+				this.boundaries =  boundaries;
+				this.position = iteratorPosition || boundaries.start;
+			}
 		}
 
 		next() {
 			var position = this.position;
 
-			if ( position.equals( this.range.end ) ) {
+			// We are at the end of the root.
+			if ( position.parent.parent === null && position.offset === position.parent.children.length ) {
+				return { done: true };
+			}
+
+			if ( this.boundaries && position.equals( this.boundaries.end ) ) {
 				return { done: true };
 			}
 
@@ -63,7 +71,12 @@ CKEDITOR.define( [
 		previous() {
 			var position = this.position;
 
-			if ( position.equals( this.range.start ) ) {
+			// We are at the begging of the root.
+			if ( position.parent.parent === null && position.offset === 0 ) {
+				return { done: true };
+			}
+
+			if ( this.boundaries && position.equals( this.boundaries.start ) ) {
 				return { done: true };
 			}
 
@@ -100,21 +113,21 @@ CKEDITOR.define( [
 	 *
 	 * @readonly
 	 */
-	RangeIterator.OPENING_TAG = OPENING_TAG;
+	PositionIterator.OPENING_TAG = OPENING_TAG;
 
 	/**
 	 * Flag for linear data closing tag.
 	 *
 	 * @readonly
 	 */
-	RangeIterator.CLOSING_TAG = CLOSING_TAG;
+	PositionIterator.CLOSING_TAG = CLOSING_TAG;
 
 	/**
 	 * Flag for linear data character.
 	 *
 	 * @readonly
 	 */
-	RangeIterator.CHARACTER = CHARACTER;
+	PositionIterator.CHARACTER = CHARACTER;
 
-	return RangeIterator;
+	return PositionIterator;
 } );

+ 2 - 2
packages/ckeditor5-utils/src/document/range.js

@@ -5,7 +5,7 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'document/rangeiterator' ], function( RangeIterator ) {
+CKEDITOR.define( [ 'document/positioniterator' ], function( PositionIterator ) {
 	/**
 	 * Range class.
 	 *
@@ -39,7 +39,7 @@ CKEDITOR.define( [ 'document/rangeiterator' ], function( RangeIterator ) {
 		}
 
 		[ Symbol.iterator ]() {
-			return new RangeIterator( this );
+			return new PositionIterator( this );
 		}
 	}
 

+ 9 - 15
packages/ckeditor5-utils/tests/document/rangeiterator.js

@@ -11,7 +11,7 @@ var modules = bender.amd.require(
 	'document/document',
 	'document/element',
 	'document/character',
-	'document/rangeiterator',
+	'document/positioniterator',
 	'document/position',
 	'document/range' );
 
@@ -28,10 +28,10 @@ describe( 'range iterator', function() {
 		var Element = modules[ 'document/element' ];
 		var Character = modules[ 'document/character' ];
 
-		var RangeIterator = modules[ 'document/rangeiterator' ];
-		OPENING_TAG = RangeIterator.OPENING_TAG;
-		CLOSING_TAG = RangeIterator.CLOSING_TAG;
-		CHARACTER = RangeIterator.CHARACTER;
+		var PositionIterator = modules[ 'document/positioniterator' ];
+		OPENING_TAG = PositionIterator.OPENING_TAG;
+		CLOSING_TAG = PositionIterator.CLOSING_TAG;
+		CHARACTER = PositionIterator.CHARACTER;
 
 		document = new Document();
 		root = document.root;
@@ -82,13 +82,10 @@ describe( 'range iterator', function() {
 	} );
 
 	it( 'should return next position', function() {
-		var RangeIterator = modules[ 'document/rangeiterator' ];
+		var PositionIterator = modules[ 'document/positioniterator' ];
 		var Position = modules[ 'document/position' ];
-		var Range = modules[ 'document/range' ];
 
-		var start = new Position( root, 0 );
-		var end = new Position( root, 2 );
-		var iterator = new RangeIterator( new Range( start, end ) );
+		var iterator = new PositionIterator( new Position( root, 0 ) );
 
 		for ( var i = 0, len = expectedItems.length; i < len; i++ ) {
 			expect( iterator.next() ).to.deep.equal( { done: false, value: expectedItems[ i ] } );
@@ -97,13 +94,10 @@ describe( 'range iterator', function() {
 	} );
 
 	it( 'should return previous position', function() {
-		var RangeIterator = modules[ 'document/rangeiterator' ];
+		var PositionIterator = modules[ 'document/positioniterator' ];
 		var Position = modules[ 'document/position' ];
-		var Range = modules[ 'document/range' ];
 
-		var start = new Position( root, 0 );
-		var end = new Position( root, 2 );
-		var iterator = new RangeIterator( new Range( start, end ), end );
+		var iterator = new PositionIterator( new Position( root, 2 ) );
 
 		for ( var i = expectedItems.length - 1; i >= 0; i-- ) {
 			expect( iterator.previous() ).to.deep.equal( { done: false, value: expectedItems[ i ] } );