8
0
Просмотр исходного кода

Fixed: view.Selection#isEqual better algorithm.

Szymon Cofalik 9 лет назад
Родитель
Сommit
8f422cf4e2
1 измененных файлов с 16 добавлено и 10 удалено
  1. 16 10
      packages/ckeditor5-engine/src/view/selection.js

+ 16 - 10
packages/ckeditor5-engine/src/view/selection.js

@@ -283,15 +283,20 @@ export default class Selection {
 	}
 
 	/**
-	 * Checks whether, this selection is equal to given selection. Selections equal if they have the same ranges and directions.
+	 * Checks whether, this selection is equal to given selection. Selections are equal if they have same directions,
+	 * same number of ranges and all ranges from one selection equal to a range from other selection.
 	 *
 	 * @param {engine.view.Selection} otherSelection Selection to compare with.
 	 * @returns {Boolean} `true` if selections are equal, `false` otherwise.
 	 */
 	isEqual( otherSelection ) {
-		const rangeCount = this.rangeCount;
+		if ( this.rangeCount != otherSelection.rangeCount ) {
+			return false;
+		} else if ( this.rangeCount === 0 ) {
+			return true;
+		}
 
-		if ( rangeCount != otherSelection.rangeCount ) {
+		if ( !this.anchor.isEqual( otherSelection.anchor ) || !this.focus.isEqual( otherSelection.focus ) ) {
 			return false;
 		}
 
@@ -303,13 +308,14 @@ export default class Selection {
 			return false;
 		}
 
-		for ( let i = 0; i < this.rangeCount; i++ ) {
-			if ( !this._ranges[ i ].isEqual( otherSelection._ranges[ i ] ) ) {
-				return false;
-			}
-		}
-
-		return this._lastRangeBackward === otherSelection._lastRangeBackward;
+		// Every range from this selection...
+		return Array.from( this.getRanges() ).every( ( rangeA ) => {
+			// ...Has a range in other selection...
+			return Array.from( otherSelection.getRanges() ).some( ( rangeB ) => {
+				// That it is equal to.
+				return rangeA.isEqual( rangeB );
+			} );
+		} );
 	}
 
 	/**