Procházet zdrojové kódy

Position.isBefore() and Position.isAfter() introduced.

Szymon Cofalik před 10 roky
rodič
revize
26d1df9abc

+ 48 - 3
packages/ckeditor5-utils/src/document/position.js

@@ -119,15 +119,60 @@ CKEDITOR.define( [ 'document/rootelement', 'utils', 'ckeditorerror' ], ( RootEle
 		}
 
 		/**
-		 * Two positions equal if paths are equal and roots are the same.
+		 * Checks whether this position equals given position.
 		 *
-		 * @param {document.Position} otherPosition Position to compare.
-		 * @returns {Boolean} True if positions equal.
+		 * @param {document.Position} otherPosition Position to compare with.
+		 * @returns {Boolean} True if positions are same.
 		 */
 		isEqual( otherPosition ) {
 			return this.compareWith( otherPosition ) == SAME;
 		}
 
+		/**
+		 * Checks whether this position is before given position.
+		 * Attention: watch out when using negation of the value returned by this method, because the negation will also
+		 * be true if positions are in different roots and you might not expect this. You should probably use
+		 * `a.isAfter( b ) || a.isEqual( b )` or `!a.isBefore( p ) && a.root == b.root` in most scenarios. If your
+		 * condition uses multiple `isAfter` and `isBefore` checks, build them so they do not use negated values, i.e.:
+		 *
+		 *  if ( a.isBefore( b ) && c.isAfter( d ) ) {
+		 *    // do A.
+		 *  } else {
+		 *    // do B.
+		 *  }
+		 *
+		 * or, if you have only one if-branch:
+		 *
+		 *  if ( !( a.isBefore( b ) && c.isAfter( d ) ) {
+		 *    // do B.
+		 *  }
+		 *
+		 * rather than:
+		 *
+		 *  if ( !a.isBefore( b ) || && !c.isAfter( d ) ) {
+		 *    // do B.
+		 *  } else {
+		 *    // do A.
+		 *  }
+		 *
+		 * @param {document.Position} otherPosition Position to compare with.
+		 * @returns {Boolean} True if this position is before given position.
+		 */
+		isBefore( otherPosition ) {
+			return this.compareWith( otherPosition ) == BEFORE;
+		}
+
+		/**
+		 * Checks whether this position is after given position.
+		 * Attention: see {document.Position#isBefore}.
+		 *
+		 * @param {document.Position} otherPosition Position to compare with.
+		 * @returns {Boolean} True if this position is after given position.
+		 */
+		isAfter( otherPosition ) {
+			return this.compareWith( otherPosition ) == AFTER;
+		}
+
 		/**
 		 * Returns the path to the parent, which is the {@link document.Position#path} without the last element.
 		 *

+ 1 - 1
packages/ckeditor5-utils/src/document/range.js

@@ -106,7 +106,7 @@ CKEDITOR.define( [ 'document/position', 'document/positioniterator', 'utils' ],
 		 * @returns {boolean} True if given {document.Position position} is contained.
 		 */
 		containsPosition( position ) {
-			return position.compareWith( this.start ) == Position.AFTER && position.compareWith( this.end ) == Position.BEFORE;
+			return position.isAfter( this.start ) && position.isBefore( this.end );
 		}
 
 		/**

+ 1 - 1
packages/ckeditor5-utils/src/document/transformoperation.js

@@ -386,7 +386,7 @@ CKEDITOR.define( [
 				// To keep them in the right order, we need to move them starting from the last one.
 				// [|ABC||DEF||GHI|] ==> [^], [|ABC||DEF|] ==> [^GHI], [|ABC|] ==> [^DEFGHI], [] ==> [ABCDEFGHI]
 				// To achieve this, we sort ranges by their starting position in descending order.
-				ranges.sort( ( a, b ) => b.start.compareWith( a.start ) );
+				ranges.sort( ( a, b ) => a.start.isBefore( b.start ) ? 1 : -1 );
 
 				// Map transformed range(s) to operations and return them.
 				return ranges.map( ( range ) => {