8
0
فهرست منبع

Tests and fixes for getCommonPath.

Piotrek Koszuliński 9 سال پیش
والد
کامیت
00e6897cc2
2فایلهای تغییر یافته به همراه46 افزوده شده و 0 حذف شده
  1. 11 0
      packages/ckeditor5-engine/src/model/position.js
  2. 35 0
      packages/ckeditor5-engine/tests/model/position.js

+ 11 - 0
packages/ckeditor5-engine/src/model/position.js

@@ -259,7 +259,18 @@ export default class Position {
 		return this.parent.getAncestors( { includeNode: true, parentFirst: true } );
 	}
 
+	/**
+	 * Returns the slice of two position {@link #path paths} which is identical. The {@link #root roots}
+	 * of these two paths must be identical.
+	 *
+	 * @param {engine.model.Position} position The second position.
+	 * @returns {Array.<Number>} The common path.
+	 */
 	getCommonPath( position ) {
+		if ( this.root != position.root ) {
+			return [];
+		}
+
 		// We find on which tree-level start and end have the lowest common ancestor
 		let cmp = compareArrays( this.path, position.path );
 		// If comparison returned string it means that arrays are same.

+ 35 - 0
packages/ckeditor5-engine/tests/model/position.js

@@ -509,6 +509,41 @@ describe( 'position', () => {
 		} );
 	} );
 
+	describe( 'getCommonPath', () => {
+		it( 'returns the common part', () => {
+			const pos1 = new Position( root, [ 1, 0, 0 ] );
+			const pos2 = new Position( root, [ 1, 0, 1 ] );
+
+			expect( pos1.getCommonPath( pos2 ) ).to.deep.equal( [ 1, 0 ] );
+		} );
+
+		it( 'returns the common part when paths are equal', () => {
+			const pos1 = new Position( root, [ 1, 0, 1 ] );
+			const pos2 = new Position( root, [ 1, 0, 1 ] );
+			const commonPath = pos1.getCommonPath( pos2 );
+
+			// Ensure that we have a clone
+			expect( commonPath ).to.not.equal( pos1.path );
+			expect( commonPath ).to.not.equal( pos2.path );
+
+			expect( commonPath ).to.deep.equal( [ 1, 0, 1 ] );
+		} );
+
+		it( 'returns empty array when paths totally differ', () => {
+			const pos1 = new Position( root, [ 1, 1 ] );
+			const pos2 = new Position( root, [ 0 ] );
+
+			expect( pos1.getCommonPath( pos2 ) ).to.deep.equal( [] );
+		} );
+
+		it( 'returns empty array when roots differ, but paths are the same', () => {
+			const pos1 = new Position( root, [ 1, 1 ] );
+			const pos2 = new Position( otherRoot, [ 1, 1 ] );
+
+			expect( pos1.getCommonPath( pos2 ) ).to.deep.equal( [] );
+		} );
+	} );
+
 	describe( 'compareWith', () => {
 		it( 'should return same if positions are same', () => {
 			const position = new Position( root, [ 1, 2, 3 ] );