Selaa lähdekoodia

Code style: Remove confusing position variable from TreeWalker methods.

Maciej Gołaszewski 8 vuotta sitten
vanhempi
sitoutus
21c025786e
1 muutettua tiedostoa jossa 39 lisäystä ja 57 poistoa
  1. 39 57
      packages/ckeditor5-engine/src/view/treewalker.js

+ 39 - 57
packages/ckeditor5-engine/src/view/treewalker.js

@@ -187,17 +187,16 @@ export default class TreeWalker {
 	 * @returns {module:engine/view/treewalker~TreeWalkerValue} return.value Information about taken step.
 	 * @returns {module:engine/view/treewalker~TreeWalkerValue} return.value Information about taken step.
 	 */
 	 */
 	_next() {
 	_next() {
-		let position = this.position;
 		const previousPosition = this.position;
 		const previousPosition = this.position;
-		const parent = position.parent;
+		const parent = this.position.parent;
 
 
 		// We are at the end of the root.
 		// We are at the end of the root.
-		if ( parent.parent === null && position.offset === parent.childCount ) {
+		if ( parent.parent === null && this.position.offset === parent.childCount ) {
 			return { done: true };
 			return { done: true };
 		}
 		}
 
 
 		// We reached the walker boundary.
 		// We reached the walker boundary.
-		if ( parent === this._boundaryEndParent && position.offset == this.boundaries.end.offset ) {
+		if ( parent === this._boundaryEndParent && this.position.offset == this.boundaries.end.offset ) {
 			return { done: true };
 			return { done: true };
 		}
 		}
 
 
@@ -206,32 +205,29 @@ export default class TreeWalker {
 
 
 		// Text is a specific parent because it contains string instead of child nodes.
 		// Text is a specific parent because it contains string instead of child nodes.
 		if ( parent instanceof Text ) {
 		if ( parent instanceof Text ) {
-			if ( position.isAtEnd ) {
+			if ( this.position.isAtEnd ) {
 				// Prevent returning "elementEnd" for Text node. Skip that value and return the next walker step.
 				// Prevent returning "elementEnd" for Text node. Skip that value and return the next walker step.
 				this.position = Position.createAfter( parent );
 				this.position = Position.createAfter( parent );
 
 
 				return this._next();
 				return this._next();
 			}
 			}
 
 
-			node = parent.data[ position.offset ];
+			node = parent.data[ this.position.offset ];
 		} else {
 		} else {
-			node = parent.getChild( position.offset );
+			node = parent.getChild( this.position.offset );
 		}
 		}
 
 
 		if ( node instanceof Element ) {
 		if ( node instanceof Element ) {
 			if ( !this.shallow ) {
 			if ( !this.shallow ) {
-				position = new Position( node, 0 );
+				this.position = new Position( node, 0 );
 			} else {
 			} else {
-				position = position.getShiftedBy( 1 );
+				this.position = this.position.getShiftedBy( 1 );
 			}
 			}
 
 
-			this.position = position;
-
-			return this._formatReturnValue( 'elementStart', node, previousPosition, position, 1 );
+			return this._formatReturnValue( 'elementStart', node, previousPosition, this.position, 1 );
 		} else if ( node instanceof Text ) {
 		} else if ( node instanceof Text ) {
 			if ( this.singleCharacters ) {
 			if ( this.singleCharacters ) {
-				position = new Position( node, 0 );
-				this.position = position;
+				this.position = new Position( node, 0 );
 
 
 				return this._next();
 				return this._next();
 			} else {
 			} else {
@@ -242,15 +238,13 @@ export default class TreeWalker {
 				if ( node == this._boundaryEndParent ) {
 				if ( node == this._boundaryEndParent ) {
 					charactersCount = this.boundaries.end.offset;
 					charactersCount = this.boundaries.end.offset;
 					item = new TextProxy( node, 0, charactersCount );
 					item = new TextProxy( node, 0, charactersCount );
-					position = Position.createAfter( item );
+					this.position = Position.createAfter( item );
 				} else {
 				} else {
 					// If not just keep moving forward.
 					// If not just keep moving forward.
-					position = position.getShiftedBy( 1 );
+					this.position = this.position.getShiftedBy( 1 );
 				}
 				}
 
 
-				this.position = position;
-
-				return this._formatReturnValue( 'text', item, previousPosition, position, charactersCount );
+				return this._formatReturnValue( 'text', item, previousPosition, this.position, charactersCount );
 			}
 			}
 		} else if ( typeof node == 'string' ) {
 		} else if ( typeof node == 'string' ) {
 			let textLength;
 			let textLength;
@@ -261,25 +255,22 @@ export default class TreeWalker {
 				// Check if text stick out of walker range.
 				// Check if text stick out of walker range.
 				const endOffset = parent === this._boundaryEndParent ? this.boundaries.end.offset : parent.data.length;
 				const endOffset = parent === this._boundaryEndParent ? this.boundaries.end.offset : parent.data.length;
 
 
-				textLength = endOffset - position.offset;
+				textLength = endOffset - this.position.offset;
 			}
 			}
 
 
-			const textProxy = new TextProxy( parent, position.offset, textLength );
-
-			position = position.getShiftedBy( textLength );
+			const textProxy = new TextProxy( parent, this.position.offset, textLength );
 
 
-			this.position = position;
+			this.position = this.position.getShiftedBy( textLength );
 
 
-			return this._formatReturnValue( 'text', textProxy, previousPosition, position, textLength );
+			return this._formatReturnValue( 'text', textProxy, previousPosition, this.position, textLength );
 		} else {
 		} else {
 			// `node` is not set, we reached the end of current `parent`.
 			// `node` is not set, we reached the end of current `parent`.
-			position = Position.createAfter( parent );
-			this.position = position;
+			this.position = Position.createAfter( parent );
 
 
 			if ( this.ignoreElementEnd ) {
 			if ( this.ignoreElementEnd ) {
 				return this._next();
 				return this._next();
 			} else {
 			} else {
-				return this._formatReturnValue( 'elementEnd', parent, previousPosition, position );
+				return this._formatReturnValue( 'elementEnd', parent, previousPosition, this.position );
 			}
 			}
 		}
 		}
 	}
 	}
@@ -293,17 +284,16 @@ export default class TreeWalker {
 	 * @returns {module:engine/view/treewalker~TreeWalkerValue} return.value Information about taken step.
 	 * @returns {module:engine/view/treewalker~TreeWalkerValue} return.value Information about taken step.
 	 */
 	 */
 	_previous() {
 	_previous() {
-		let position = this.position;
 		const previousPosition = this.position;
 		const previousPosition = this.position;
-		const parent = position.parent;
+		const parent = this.position.parent;
 
 
 		// We are at the beginning of the root.
 		// We are at the beginning of the root.
-		if ( parent.parent === null && position.offset === 0 ) {
+		if ( parent.parent === null && this.position.offset === 0 ) {
 			return { done: true };
 			return { done: true };
 		}
 		}
 
 
 		// We reached the walker boundary.
 		// We reached the walker boundary.
-		if ( parent == this._boundaryStartParent && position.offset == this.boundaries.start.offset ) {
+		if ( parent == this._boundaryStartParent && this.position.offset == this.boundaries.start.offset ) {
 			return { done: true };
 			return { done: true };
 		}
 		}
 
 
@@ -312,38 +302,35 @@ export default class TreeWalker {
 
 
 		// Text {@link module:engine/view/text~Text} element is a specific parent because contains string instead of child nodes.
 		// Text {@link module:engine/view/text~Text} element is a specific parent because contains string instead of child nodes.
 		if ( parent instanceof Text ) {
 		if ( parent instanceof Text ) {
-			if ( position.isAtStart ) {
+			if ( this.position.isAtStart ) {
 				// Prevent returning "elementStart" for Text node. Skip that value and return the next walker step.
 				// Prevent returning "elementStart" for Text node. Skip that value and return the next walker step.
 				this.position = Position.createBefore( parent );
 				this.position = Position.createBefore( parent );
 
 
 				return this._previous();
 				return this._previous();
 			}
 			}
 
 
-			node = parent.data[ position.offset - 1 ];
+			node = parent.data[ this.position.offset - 1 ];
 		} else {
 		} else {
-			node = parent.getChild( position.offset - 1 );
+			node = parent.getChild( this.position.offset - 1 );
 		}
 		}
 
 
 		if ( node instanceof Element ) {
 		if ( node instanceof Element ) {
 			if ( !this.shallow ) {
 			if ( !this.shallow ) {
-				position = new Position( node, node.childCount );
-				this.position = position;
+				this.position = new Position( node, node.childCount );
 
 
 				if ( this.ignoreElementEnd ) {
 				if ( this.ignoreElementEnd ) {
 					return this._previous();
 					return this._previous();
 				} else {
 				} else {
-					return this._formatReturnValue( 'elementEnd', node, previousPosition, position );
+					return this._formatReturnValue( 'elementEnd', node, previousPosition, this.position );
 				}
 				}
 			} else {
 			} else {
-				position = position.getShiftedBy( -1 );
-				this.position = position;
+				this.position = this.position.getShiftedBy( -1 );
 
 
-				return this._formatReturnValue( 'elementStart', node, previousPosition, position, 1 );
+				return this._formatReturnValue( 'elementStart', node, previousPosition, this.position, 1 );
 			}
 			}
 		} else if ( node instanceof Text ) {
 		} else if ( node instanceof Text ) {
 			if ( this.singleCharacters ) {
 			if ( this.singleCharacters ) {
-				position = new Position( node, node.data.length );
-				this.position = position;
+				this.position = new Position( node, node.data.length );
 
 
 				return this._previous();
 				return this._previous();
 			} else {
 			} else {
@@ -356,15 +343,13 @@ export default class TreeWalker {
 
 
 					item = new TextProxy( node, offset, node.data.length - offset );
 					item = new TextProxy( node, offset, node.data.length - offset );
 					charactersCount = item.data.length;
 					charactersCount = item.data.length;
-					position = Position.createBefore( item );
+					this.position = Position.createBefore( item );
 				} else {
 				} else {
 					// If not just keep moving backward.
 					// If not just keep moving backward.
-					position = position.getShiftedBy( -1 );
+					this.position = this.position.getShiftedBy( -1 );
 				}
 				}
 
 
-				this.position = position;
-
-				return this._formatReturnValue( 'text', item, previousPosition, position, charactersCount );
+				return this._formatReturnValue( 'text', item, previousPosition, this.position, charactersCount );
 			}
 			}
 		} else if ( typeof node == 'string' ) {
 		} else if ( typeof node == 'string' ) {
 			let textLength;
 			let textLength;
@@ -373,24 +358,21 @@ export default class TreeWalker {
 				// Check if text stick out of walker range.
 				// Check if text stick out of walker range.
 				const startOffset = parent === this._boundaryStartParent ? this.boundaries.start.offset : 0;
 				const startOffset = parent === this._boundaryStartParent ? this.boundaries.start.offset : 0;
 
 
-				textLength = position.offset - startOffset;
+				textLength = this.position.offset - startOffset;
 			} else {
 			} else {
 				textLength = 1;
 				textLength = 1;
 			}
 			}
 
 
-			position = position.getShiftedBy( -textLength );
-
-			const textProxy = new TextProxy( parent, position.offset, textLength );
+			this.position = this.position.getShiftedBy( -textLength );
 
 
-			this.position = position;
+			const textProxy = new TextProxy( parent, this.position.offset, textLength );
 
 
-			return this._formatReturnValue( 'text', textProxy, previousPosition, position, textLength );
+			return this._formatReturnValue( 'text', textProxy, previousPosition, this.position, textLength );
 		} else {
 		} else {
 			// `node` is not set, we reached the beginning of current `parent`.
 			// `node` is not set, we reached the beginning of current `parent`.
-			position = Position.createBefore( parent );
-			this.position = position;
+			this.position = Position.createBefore( parent );
 
 
-			return this._formatReturnValue( 'elementStart', parent, previousPosition, position, 1 );
+			return this._formatReturnValue( 'elementStart', parent, previousPosition, this.position, 1 );
 		}
 		}
 	}
 	}