Browse Source

Other: Remove non semantic methods from Position.

Maciej Gołaszewski 8 years ago
parent
commit
b938ae39df

+ 17 - 5
packages/ckeditor5-engine/src/model/delta/basic-transformations.js

@@ -73,7 +73,10 @@ addTransformationCase( AttributeDelta, SplitDelta, ( a, b, context ) => {
 			const additionalAttributeDelta = new AttributeDelta();
 
 			const rangeStart = splitPosition.getShiftedBy( 1 );
-			const rangeEnd = rangeStart.getMovedToChild();
+
+			const rangeEndPath = rangeStart.path.slice();
+			rangeEndPath.push( 0 );
+			const rangeEnd = new Position( rangeStart.root, rangeEndPath );
 
 			const oldValue = b._cloneOperation.nodes.getNode( 0 ).getAttribute( operation.key );
 
@@ -319,7 +322,10 @@ addTransformationCase( SplitDelta, WrapDelta, ( a, b, context ) => {
 
 		// Now, `splitNodePos` points before wrapping element.
 		// To get a position before last children of that element, we expand position's `path` member by proper offset.
-		const splitNodePos = b.range.start.getMovedToChild( b.howMany - 1 );
+		const splitPath = b.range.start.path.slice();
+		splitPath.push( b.howMany - 1 );
+
+		const splitNodePos = new Position( b.range.start.root, splitPath );
 
 		// SplitDelta insert operation position should be right after the node we split.
 		delta._cloneOperation.position = splitNodePos.getShiftedBy( 1 );
@@ -328,12 +334,18 @@ addTransformationCase( SplitDelta, WrapDelta, ( a, b, context ) => {
 		// Nodes moved by SplitDelta will be moved from new position, modified by WrapDelta.
 		// To obtain that new position, `splitNodePos` will be used, as this is the node we are extracting children from.
 		// Nothing changed inside split node so it is correct to use the original split position offset.
-		delta._moveOperation.sourcePosition = splitNodePos.getMovedToChild( a.position.offset );
+		const sourcePath = splitNodePos.path.slice();
+		sourcePath.push( a.position.offset );
+
+		delta._moveOperation.sourcePosition = new Position( splitNodePos.root, sourcePath );
 
 		// 3. Fix move operation target position.
 		// SplitDelta move operation target position should be inside the node inserted by operation above.
 		// Since the node is empty, we will insert at offset 0.
-		delta._moveOperation.targetPosition = splitNodePos.getShiftedBy( 1 ).getMovedToChild();
+		const targetPath = splitNodePos.getShiftedBy( 1 ).path.slice();
+		targetPath.push( 0 );
+
+		delta._moveOperation.targetPosition = new Position( splitNodePos.root, targetPath );
 
 		return [ delta ];
 	}
@@ -438,7 +450,7 @@ addTransformationCase( WrapDelta, SplitDelta, ( a, b, context ) => {
 
 		const path = delta._moveOperation.targetPosition.path.slice();
 		path[ index ] += 1;
-		delta._moveOperation.targetPosition = delta._moveOperation.targetPosition.getMovedToPath( path );
+		delta._moveOperation.targetPosition = new Position( delta._moveOperation.targetPosition.root, path );
 
 		return [ delta ];
 	}

+ 12 - 37
packages/ckeditor5-engine/src/model/position.js

@@ -346,16 +346,6 @@ export default class Position {
 	}
 
 	/**
-	 * Returns a new instance of `Position`, that has same {@link #root root} but it's path is set to `path` value.
-	 *
-	 * @param {Array.<Number>} path Position path. See {@link module:engine/model/position~Position#path}.
-	 * @returns {module:engine/model/position~Position} Moved position.
-	 */
-	getMovedToPath( path ) {
-		return new Position( this.root, path );
-	}
-
-	/**
 	 * Returns a new instance of `Position`, that has same {@link #parent parent} but it's offset
 	 * is set to `offset` value.
 	 *
@@ -367,7 +357,7 @@ export default class Position {
 
 		setOffset( path, offset );
 
-		return this.getMovedToPath( path );
+		return new Position( this.root, path );
 	}
 
 	/**
@@ -384,28 +374,6 @@ export default class Position {
 	}
 
 	/**
-	 * Returns a new instance of `Position`, that has same {@link #root root} but it's moved in path by one level up.
-	 *
-	 * @returns {module:engine/model/position~Position} Moved position.
-	 */
-	getMovedToParent() {
-		return this.getMovedToPath( this.getParentPath() );
-	}
-
-	/**
-	 * Returns a new instance of `Position`, that has same {@link #root root} but it's moved in path by one level down.
-	 *
-	 * @returns {module:engine/model/position~Position} Moved position.
-	 */
-	getMovedToChild( offsetInChild = 0 ) {
-		const path = this.path.slice();
-
-		path.push( offsetInChild );
-
-		return this.getMovedToPath( path );
-	}
-
-	/**
 	 * Checks whether this position is after given position.
 	 *
 	 * @see module:engine/model/position~Position#isBefore
@@ -508,14 +476,17 @@ export default class Position {
 					return false;
 				}
 
-				left = left.getMovedToParent().getShiftedBy( 1 );
+				const path = left.getParentPath();
+				path[ path.length - 1 ]++;
+				left = new Position( left.root, path );
+
 				leftParent = leftParent.parent;
 			} else {
 				if ( right.offset !== 0 ) {
 					return false;
 				}
 
-				right = right.getMovedToParent();
+				right = new Position( right.root, right.getParentPath() );
 			}
 		}
 	}
@@ -573,8 +544,10 @@ export default class Position {
 				} else {
 					// Otherwise, decrement index on that path.
 					const path = this.path.slice();
+
 					path[ i ] -= howMany;
-					return this.getMovedToPath( path );
+
+					return new Position( this.root, path );
 				}
 			}
 		}
@@ -614,8 +587,10 @@ export default class Position {
 				// And are inserted before next node of that path...
 				// "Push" the index on that path.
 				const path = this.path.slice();
+
 				path[ i ] += howMany;
-				return this.getMovedToPath( path );
+
+				return new Position( this.root, path );
 			}
 		}
 

+ 9 - 2
packages/ckeditor5-engine/src/model/range.js

@@ -301,7 +301,10 @@ export default class Range {
 				ranges.push( new Range( pos, pos.getShiftedBy( howMany ) ) );
 			}
 
-			pos = pos.getMovedToParent().getShiftedBy( 1 );
+			const path = pos.getParentPath();
+			path[ path.length - 1 ]++;
+
+			pos = new Position( pos.root, path );
 
 			posParent = posParent.parent;
 		}
@@ -315,7 +318,11 @@ export default class Range {
 				ranges.push( new Range( pos, pos.getShiftedBy( howMany ) ) );
 			}
 
-			pos = pos.getShiftedTo( offset ).getMovedToChild();
+			const path = pos.getParentPath();
+			path.push( offset );
+			path.push( 0 );
+
+			pos = new Position( pos.root, path );
 		}
 
 		return ranges;

+ 11 - 4
packages/ckeditor5-engine/src/model/treewalker.js

@@ -223,7 +223,9 @@ export default class TreeWalker {
 		if ( node instanceof Element ) {
 			if ( !this.shallow ) {
 				// Manual operations on path internals for optimization purposes. Here and in the rest of the method.
-				position = position.getMovedToChild();
+				const path = position.path.slice();
+				path.push( 0 );
+				position = new Position( position.root, path );
 
 				this._visitedParent = node;
 			} else {
@@ -258,7 +260,9 @@ export default class TreeWalker {
 			return formatReturnValue( 'text', item, previousPosition, this.position, charactersCount );
 		} else {
 			// `node` is not set, we reached the end of current `parent`.
-			position = position.getMovedToParent().getShiftedBy( 1 );
+			const path = position.getParentPath();
+			path[ path.length - 1 ]++;
+			position = new Position( position.root, path );
 
 			this.position = position;
 			this._visitedParent = parent.parent;
@@ -301,7 +305,10 @@ export default class TreeWalker {
 			position = position.getShiftedBy( -1 );
 
 			if ( !this.shallow ) {
-				position = position.getMovedToChild( node.maxOffset );
+				const path = position.path.slice();
+				path.push( node.maxOffset );
+
+				position = new Position( position.root, path );
 
 				this.position = position;
 				this._visitedParent = node;
@@ -341,7 +348,7 @@ export default class TreeWalker {
 			return formatReturnValue( 'text', item, previousPosition, position, charactersCount );
 		} else {
 			// `node` is not set, we reached the beginning of current `parent`.
-			position = position.getMovedToParent();
+			position = new Position( position.root, position.getParentPath() );
 
 			this.position = position;
 			this._visitedParent = parent.parent;

+ 21 - 6
packages/ckeditor5-engine/tests/model/delta/transform/_utils/utils.js

@@ -68,9 +68,14 @@ export function getMarkerDelta( name, oldRange, newRange, version ) {
 export function getMergeDelta( position, howManyInPrev, howManyInNext, version ) {
 	const delta = new MergeDelta();
 
-	const sourcePosition = position.getMovedToChild();
+	const sourcePath = position.path.slice();
+	sourcePath.push( 0 );
+	const sourcePosition = new Position( position.root, sourcePath );
 
-	const targetPosition = position.getShiftedBy( -1 ).getMovedToChild( howManyInPrev );
+	const targetPath = position.getShiftedBy( -1 ).path.slice();
+	targetPath.push( howManyInPrev );
+
+	const targetPosition = new Position( position.root, targetPath );
 
 	const move = new MoveOperation( sourcePosition, howManyInNext, targetPosition, version );
 	move.isSticky = true;
@@ -126,9 +131,15 @@ export function getRenameDelta( position, oldName, newName, baseVersion ) {
 export function getSplitDelta( position, nodeCopy, howManyMove, version ) {
 	const delta = new SplitDelta();
 
-	const insertPosition = position.getMovedToParent().getShiftedBy( 1 );
+	const insertPath = position.getParentPath();
+	insertPath[ insertPath.length - 1 ]++;
+
+	const insertPosition = new Position( position.root, insertPath );
+
+	const targetPath = insertPosition.path.slice();
+	targetPath.push( 0 );
 
-	const targetPosition = insertPosition.getMovedToChild();
+	const targetPosition = new Position( insertPosition.root, targetPath );
 
 	delta.addOperation( new InsertOperation( insertPosition, [ nodeCopy ], version ) );
 
@@ -147,7 +158,9 @@ export function getWrapDelta( range, element, version ) {
 
 	const insert = new InsertOperation( range.end, element, version );
 
-	const targetPosition = range.end.getMovedToChild();
+	const targetPath = range.end.path.slice();
+	targetPath.push( 0 );
+	const targetPosition = new Position( range.end.root, targetPath );
 
 	const move = new MoveOperation( range.start, range.end.offset - range.start.offset, targetPosition, version + 1 );
 
@@ -162,7 +175,9 @@ export function getWrapDelta( range, element, version ) {
 export function getUnwrapDelta( positionBefore, howManyChildren, version ) {
 	const delta = new UnwrapDelta();
 
-	const sourcePosition = positionBefore.getMovedToChild();
+	const sourcePath = positionBefore.path.slice();
+	sourcePath.push( 0 );
+	const sourcePosition = new Position( positionBefore.root, sourcePath );
 
 	const move = new MoveOperation( sourcePosition, howManyChildren, positionBefore, version );
 	move.isSticky = true;

+ 16 - 4
packages/ckeditor5-engine/tests/model/delta/transform/splitdelta.js

@@ -968,8 +968,15 @@ describe( 'transform', () => {
 				baseVersion = removeDelta.operations.length;
 
 				const newInsertPosition = removeOperation.targetPosition.getShiftedBy( 2 );
-				const newMoveSourcePosition = removeOperation.targetPosition.getShiftedBy( 1 ).getMovedToChild( 2 );
-				const newMoveTargetPosition = newInsertPosition.getMovedToChild( );
+
+				const newSourcePath = removeOperation.targetPosition.getShiftedBy( 1 ).path.slice();
+				newSourcePath.push( 2 );
+				const newMoveSourcePosition = new Position( removeOperation.targetPosition.root, newSourcePath );
+
+				const newMoveTargetPath = newInsertPosition.path.slice();
+				newMoveTargetPath.push( 0 );
+
+				const newMoveTargetPosition = new Position( newInsertPosition.root, newMoveTargetPath );
 
 				expectDelta( transformed[ 0 ], {
 					type: SplitDelta,
@@ -1002,8 +1009,13 @@ describe( 'transform', () => {
 				baseVersion = removeDelta.operations.length;
 
 				const newInsertPosition = removeOperation.targetPosition.getShiftedBy( 2 );
-				const newMoveSourcePosition = removeOperation.targetPosition.getShiftedBy( 1 ).getMovedToChild( 3 );
-				const newMoveTargetPosition = newInsertPosition.getMovedToChild( );
+				const newMoveSourcePath = removeOperation.targetPosition.getShiftedBy( 1 ).path.slice();
+				newMoveSourcePath.push( 3 );
+				const newMoveSourcePosition = new Position( removeOperation.targetPosition.root, newMoveSourcePath );
+
+				const newMoveTargetPath = newInsertPosition.path.slice();
+				newMoveTargetPath.push( 0 );
+				const newMoveTargetPosition = new Position( newInsertPosition.root, newMoveTargetPath );
 
 				expectDelta( transformed[ 0 ], {
 					type: SplitDelta,

+ 1 - 1
packages/ckeditor5-engine/tests/model/operation/transform.js

@@ -58,7 +58,7 @@ describe( 'transform', () => {
 
 		path[ index ] += howMany;
 
-		return position.getMovedToPath( path );
+		return new Position( position.root, path );
 	}
 
 	describe( 'InsertOperation', () => {