Przeglądaj źródła

Other: Remove excessive view Position.createFromPosition calls.

Maciej Gołaszewski 8 lat temu
rodzic
commit
13efc02c86

+ 3 - 4
packages/ckeditor5-engine/src/view/range.js

@@ -28,14 +28,14 @@ export default class Range {
 		 *
 		 *
 		 * @member {module:engine/view/position~Position}
 		 * @member {module:engine/view/position~Position}
 		 */
 		 */
-		this.start = Position.createFromPosition( start );
+		this.start = start;
 
 
 		/**
 		/**
 		 * End position.
 		 * End position.
 		 *
 		 *
 		 * @member {module:engine/view/position~Position}
 		 * @member {module:engine/view/position~Position}
 		 */
 		 */
-		this.end = end ? Position.createFromPosition( end ) : Position.createFromPosition( start );
+		this.end = end ? end : start;
 	}
 	}
 
 
 	/**
 	/**
@@ -451,9 +451,8 @@ export default class Range {
 	 */
 	 */
 	static createCollapsedAt( itemOrPosition, offset ) {
 	static createCollapsedAt( itemOrPosition, offset ) {
 		const start = Position.createAt( itemOrPosition, offset );
 		const start = Position.createAt( itemOrPosition, offset );
-		const end = Position.createFromPosition( start );
 
 
-		return new Range( start, end );
+		return new Range( start, start );
 	}
 	}
 }
 }
 
 

+ 4 - 6
packages/ckeditor5-engine/src/view/selection.js

@@ -132,9 +132,8 @@ export default class Selection {
 			return null;
 			return null;
 		}
 		}
 		const range = this._ranges[ this._ranges.length - 1 ];
 		const range = this._ranges[ this._ranges.length - 1 ];
-		const anchor = this._lastRangeBackward ? range.end : range.start;
 
 
-		return Position.createFromPosition( anchor );
+		return this._lastRangeBackward ? range.end : range.start;
 	}
 	}
 
 
 	/**
 	/**
@@ -148,9 +147,8 @@ export default class Selection {
 			return null;
 			return null;
 		}
 		}
 		const range = this._ranges[ this._ranges.length - 1 ];
 		const range = this._ranges[ this._ranges.length - 1 ];
-		const focus = this._lastRangeBackward ? range.start : range.end;
 
 
-		return Position.createFromPosition( focus );
+		return this._lastRangeBackward ? range.start : range.end;
 	}
 	}
 
 
 	/**
 	/**
@@ -281,7 +279,7 @@ export default class Selection {
 	getFirstPosition() {
 	getFirstPosition() {
 		const firstRange = this.getFirstRange();
 		const firstRange = this.getFirstRange();
 
 
-		return firstRange ? Position.createFromPosition( firstRange.start ) : null;
+		return firstRange ? firstRange.start : null;
 	}
 	}
 
 
 	/**
 	/**
@@ -294,7 +292,7 @@ export default class Selection {
 	getLastPosition() {
 	getLastPosition() {
 		const lastRange = this.getLastRange();
 		const lastRange = this.getLastRange();
 
 
-		return lastRange ? Position.createFromPosition( lastRange.end ) : null;
+		return lastRange ? lastRange.end : null;
 	}
 	}
 
 
 	/**
 	/**

+ 2 - 2
packages/ckeditor5-engine/src/view/treewalker.js

@@ -187,7 +187,7 @@ 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 = Position.createFromPosition( this.position );
+		let position = this.position;
 		const previousPosition = this.position;
 		const previousPosition = this.position;
 		const parent = position.parent;
 		const parent = position.parent;
 
 
@@ -293,7 +293,7 @@ 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 = Position.createFromPosition( this.position );
+		let position = this.position;
 		const previousPosition = this.position;
 		const previousPosition = this.position;
 		const parent = position.parent;
 		const parent = position.parent;
 
 

+ 4 - 4
packages/ckeditor5-engine/src/view/writer.js

@@ -355,7 +355,7 @@ export function remove( range ) {
 	// Merge after removing.
 	// Merge after removing.
 	const mergePosition = mergeAttributes( breakStart );
 	const mergePosition = mergeAttributes( breakStart );
 	range.start = mergePosition;
 	range.start = mergePosition;
-	range.end = Position.createFromPosition( mergePosition );
+	range.end = mergePosition;
 
 
 	// Return removed nodes.
 	// Return removed nodes.
 	return new DocumentFragment( removed );
 	return new DocumentFragment( removed );
@@ -537,7 +537,7 @@ export function wrapPosition( position, attribute ) {
 
 
 	// Return same position when trying to wrap with attribute similar to position parent.
 	// Return same position when trying to wrap with attribute similar to position parent.
 	if ( attribute.isSimilar( position.parent ) ) {
 	if ( attribute.isSimilar( position.parent ) ) {
-		return movePositionToTextNode( Position.createFromPosition( position ) );
+		return movePositionToTextNode( position );
 	}
 	}
 
 
 	// When position is inside text node - break it and place new position between two text nodes.
 	// When position is inside text node - break it and place new position between two text nodes.
@@ -752,12 +752,12 @@ function _breakAttributes( position, forceSplitText = false ) {
 
 
 	// There are no attributes to break and text nodes breaking is not forced.
 	// There are no attributes to break and text nodes breaking is not forced.
 	if ( !forceSplitText && positionParent.is( 'text' ) && isContainerOrFragment( positionParent.parent ) ) {
 	if ( !forceSplitText && positionParent.is( 'text' ) && isContainerOrFragment( positionParent.parent ) ) {
-		return Position.createFromPosition( position );
+		return position;
 	}
 	}
 
 
 	// Position's parent is container, so no attributes to break.
 	// Position's parent is container, so no attributes to break.
 	if ( isContainerOrFragment( positionParent ) ) {
 	if ( isContainerOrFragment( positionParent ) ) {
-		return Position.createFromPosition( position );
+		return position;
 	}
 	}
 
 
 	// Break text and start again in new position.
 	// Break text and start again in new position.

+ 2 - 2
packages/ckeditor5-engine/tests/view/range.js

@@ -25,8 +25,8 @@ describe( 'Range', () => {
 			const range = new Range( start, end );
 			const range = new Range( start, end );
 
 
 			expect( range ).to.be.an.instanceof( Range );
 			expect( range ).to.be.an.instanceof( Range );
-			expect( range ).to.have.property( 'start' ).that.not.equals( start );
-			expect( range ).to.have.property( 'end' ).that.not.equals( end );
+			expect( range ).to.have.property( 'start' ).that.equals( start );
+			expect( range ).to.have.property( 'end' ).that.equals( end );
 			expect( range.start.parent ).to.equal( start.parent );
 			expect( range.start.parent ).to.equal( start.parent );
 			expect( range.end.parent ).to.equal( end.parent );
 			expect( range.end.parent ).to.equal( end.parent );
 			expect( range.start.offset ).to.equal( start.offset );
 			expect( range.start.offset ).to.equal( start.offset );

+ 5 - 5
packages/ckeditor5-engine/tests/view/selection.js

@@ -59,7 +59,7 @@ describe( 'Selection', () => {
 			const anchor = selection.anchor;
 			const anchor = selection.anchor;
 
 
 			expect( anchor.isEqual( range1.start ) ).to.be.true;
 			expect( anchor.isEqual( range1.start ) ).to.be.true;
-			expect( anchor ).to.not.equal( range1.start );
+			expect( anchor ).to.equal( range1.start );
 		} );
 		} );
 
 
 		it( 'should return end of single range in selection when added as backward', () => {
 		it( 'should return end of single range in selection when added as backward', () => {
@@ -67,7 +67,7 @@ describe( 'Selection', () => {
 			const anchor = selection.anchor;
 			const anchor = selection.anchor;
 
 
 			expect( anchor.isEqual( range1.end ) ).to.be.true;
 			expect( anchor.isEqual( range1.end ) ).to.be.true;
-			expect( anchor ).to.not.equal( range1.end );
+			expect( anchor ).to.equal( range1.end );
 		} );
 		} );
 
 
 		it( 'should get anchor from last inserted range', () => {
 		it( 'should get anchor from last inserted range', () => {
@@ -95,7 +95,7 @@ describe( 'Selection', () => {
 			const focus = selection.focus;
 			const focus = selection.focus;
 
 
 			expect( focus.isEqual( range1.start ) ).to.be.true;
 			expect( focus.isEqual( range1.start ) ).to.be.true;
-			expect( focus ).to.not.equal( range1.start );
+			expect( focus ).to.equal( range1.start );
 		} );
 		} );
 
 
 		it( 'should get focus from last inserted range', () => {
 		it( 'should get focus from last inserted range', () => {
@@ -410,7 +410,7 @@ describe( 'Selection', () => {
 			const position = selection.getFirstPosition();
 			const position = selection.getFirstPosition();
 
 
 			expect( position.isEqual( range2.start ) ).to.be.true;
 			expect( position.isEqual( range2.start ) ).to.be.true;
-			expect( position ).to.not.equal( range2.start );
+			expect( position ).to.equal( range2.start );
 		} );
 		} );
 
 
 		it( 'should return null if no ranges are present', () => {
 		it( 'should return null if no ranges are present', () => {
@@ -427,7 +427,7 @@ describe( 'Selection', () => {
 			const position = selection.getLastPosition();
 			const position = selection.getLastPosition();
 
 
 			expect( position.isEqual( range3.end ) ).to.be.true;
 			expect( position.isEqual( range3.end ) ).to.be.true;
-			expect( position ).to.not.equal( range3.end );
+			expect( position ).to.equal( range3.end );
 		} );
 		} );
 
 
 		it( 'should return null if no ranges are present', () => {
 		it( 'should return null if no ranges are present', () => {