Browse Source

Added isEqual method to treeView.Position.

Szymon Kupś 9 năm trước cách đây
mục cha
commit
8d1ae47055

+ 14 - 3
packages/ckeditor5-engine/src/treeview/position.js

@@ -5,7 +5,8 @@
 
 'use strict';
 
-/**Position in the tree. Position is always located before or after a node.
+/**
+ * Position in the tree. Position is always located before or after a node.
  *
  * @memberOf core.treeView
  */
@@ -49,11 +50,21 @@ export default class Position {
 		return shifted;
 	}
 
+	/**
+	 * Checks whether this position equals given position.
+	 *
+	 * @param {core.treeView.Position} otherPosition Position to compare with.
+	 * @returns {Boolean} True if positions are same.
+	 */
+	isEqual( otherPosition ) {
+		return this == otherPosition || ( this.parent == otherPosition.parent && this.offset == otherPosition.offset );
+	}
+
 	/**
 	 * Creates and returns a new instance of Position, which is equal to passed position.
 	 *
-	 * @param {treeModel.Position} position Position to be cloned.
-	 * @returns {treeModel.Position}
+	 * @param {core.treeView.Position} position Position to be cloned.
+	 * @returns {core.treeView.Position}
 	 */
 	static createFromPosition( position ) {
 		return new this( position.parent, position.offset );

+ 27 - 0
packages/ckeditor5-engine/tests/treeview/position.js

@@ -53,4 +53,31 @@ describe( 'Position', () => {
 			expect( position.parent ).to.equal( parentMock );
 		} );
 	} );
+
+	describe( 'isEqual', () => {
+		it( 'should return true for same object', () => {
+			const position = new Position( {}, 12 );
+			expect( position.isEqual( position ) ).to.be.true;
+		} );
+
+		it( 'should return true for positions with same parent and offset', () => {
+			const parentMock = {};
+			const position1 = new Position( parentMock, 12 );
+			const position2 = new Position( parentMock, 12 );
+			expect( position1.isEqual( position2 ) ).to.be.true;
+		} );
+
+		it( 'should return false for positions with different parents', () => {
+			const position1 = new Position( {}, 12 );
+			const position2 = new Position( {}, 12 );
+			expect( position1.isEqual( position2 ) ).to.be.false;
+		} );
+
+		it( 'should return false for positions with different positions', () => {
+			const parentMock = {};
+			const position1 = new Position( parentMock, 12 );
+			const position2 = new Position( parentMock, 2 );
+			expect( position1.isEqual( position2 ) ).to.be.false;
+		} );
+	} );
 } );