Explorar el Código

Range.intersectsWith() removed, Range.getCommon() renamed to Range.getIntersection().

Szymon Cofalik hace 10 años
padre
commit
e37b35de2e

+ 55 - 74
packages/ckeditor5-utils/src/document/range.js

@@ -71,24 +71,6 @@ CKEDITOR.define( [ 'document/position', 'document/positioniterator', 'utils' ],
 			return this.start.isEqual( otherRange.start ) && this.end.isEqual( otherRange.end );
 		}
 
-		/**
-		 * Checks whether this {document.Range range} intersects with the given {document.Range range}.
-		 *
-		 * @param {document.Range} otherRange Range to check.
-		 * @returns {boolean} True if ranges intersect.
-		 */
-		intersectsWith( otherRange ) {
-			const isBefore = this.start.compareWith( otherRange.start ) == Position.BEFORE &&
-				this.end.compareWith( otherRange.start ) == Position.BEFORE;
-
-			const isAfter = this.start.compareWith( otherRange.end ) == Position.AFTER &&
-				this.end.compareWith( otherRange.end ) == Position.AFTER;
-
-			const touches = this.start.isEqual( otherRange.end ) || this.end.isEqual( otherRange.start );
-
-			return !isBefore && !isAfter && !touches;
-		}
-
 		/**
 		 * Creates and returns a new instance of {@link document.Range}
 		 * that is equal to this {@link document.Range range}.
@@ -186,80 +168,79 @@ CKEDITOR.define( [ 'document/position', 'document/positioniterator', 'utils' ],
 		 * @returns {Array.<document.Range>} The difference between ranges.
 		 */
 		getDifference( otherRange ) {
-			// If ranges do not intersect, return the original range.
-			if ( !otherRange.intersectsWith( this ) ) {
-				return [
-					this.clone()
-				];
-			}
-
-			// At this point we know that ranges intersect but given range does not contain this range.
 			const ranges = [];
 
-			if ( this.containsPosition( otherRange.start ) ) {
-				// Given range start is inside this range. This means that we have to
-				// add shrunken range - from the start to the middle of this range.
-				ranges.push(
-					new Range(
-						this.start.clone(),
-						otherRange.start.clone()
-					)
-				);
-			}
-
-			if ( this.containsPosition( otherRange.end ) ) {
-				// Given range end is inside this range. This means that we have to
-				// add shrunken range - from the middle of this range to the end.
-				ranges.push(
-					new Range(
-						otherRange.end.clone(),
-						this.end.clone()
-					)
-				);
+			if ( this.start.isBefore( otherRange.end ) && this.end.isAfter( otherRange.start ) ) {
+				// Ranges intersect.
+
+				if ( this.containsPosition( otherRange.start ) ) {
+					// Given range start is inside this range. This means that we have to
+					// add shrunken range - from the start to the middle of this range.
+					ranges.push(
+						new Range(
+							this.start.clone(),
+							otherRange.start.clone()
+						)
+					);
+				}
+
+				if ( this.containsPosition( otherRange.end ) ) {
+					// Given range end is inside this range. This means that we have to
+					// add shrunken range - from the middle of this range to the end.
+					ranges.push(
+						new Range(
+							otherRange.end.clone(),
+							this.end.clone()
+						)
+					);
+				}
+			} else {
+				// Ranges do not intersect, return the original range.
+				ranges.push( this.clone() );
 			}
 
 			return ranges;
 		}
 
 		/**
-		 * Returns a part of this {document.Range range} that is also a part of given {document.Range range}. If
-		 * ranges has no common part, returns null.
+		 * Returns an intersection of this {document.Range range} and given {document.Range range}. Intersection
+		 * is a common part of both of those ranges. If ranges has no common part, returns null.
 		 *
 		 * Examples:
 		 * 	let range = new Range( new Position( [ 2, 7 ], root ), new Position( [ 4, 0, 1 ], root ) );
 		 * 	let otherRange = new Range( new Position( [ 1 ], root ), new Position( [ 2 ], root ) );
-		 * 	let transformed = range.getCommon( otherRange ); // null - ranges have no common part
+		 * 	let transformed = range.getIntersection( otherRange ); // null - ranges have no common part
 		 *
 		 * 	otherRange = new Range( new Position( [ 3 ], root ), new Position( [ 5 ], root ) );
-		 * 	transformed = range.getCommon( otherRange ); // range from [ 3 ] to [ 4, 0, 1 ]
+		 * 	transformed = range.getIntersection( otherRange ); // range from [ 3 ] to [ 4, 0, 1 ]
 		 *
-		 * @param {document.Range} otherRange Range to compare with.
-		 * @returns {document.Range} Range that is common part of given ranges.
+		 * @param {document.Range} otherRange Range to check for intersection.
+		 * @returns {document.Range|null} A common part of given ranges or null if ranges have no common part.
 		 */
-		getCommon( otherRange ) {
-			// If ranges do not intersect, they do not have common part.
-			if ( !otherRange.intersectsWith( this ) ) {
-				return null;
-			}
-
-			// At this point we know that ranges intersect, so a common range will be returned.
-			// At most, it will be same as this range.
-			let commonRangeStart = this.start;
-			let commonRangeEnd = this.end;
-
-			if ( this.containsPosition( otherRange.start ) ) {
-				// Given range start is inside this range. This means that we have to
-				// shrink common range to the given range start.
-				commonRangeStart = otherRange.start;
-			}
-
-			if ( this.containsPosition( otherRange.end ) ) {
-				// Given range end is inside this range. This means that we have to
-				// shrink common range to the given range end.
-				commonRangeEnd = otherRange.end;
+		getIntersection( otherRange ) {
+			if ( this.start.isBefore( otherRange.end ) && this.end.isAfter( otherRange.start ) ) {
+				// Ranges intersect, so a common range will be returned.
+				// At most, it will be same as this range.
+				let commonRangeStart = this.start;
+				let commonRangeEnd = this.end;
+
+				if ( this.containsPosition( otherRange.start ) ) {
+					// Given range start is inside this range. This means thaNt we have to
+					// shrink common range to the given range start.
+					commonRangeStart = otherRange.start;
+				}
+
+				if ( this.containsPosition( otherRange.end ) ) {
+					// Given range end is inside this range. This means that we have to
+					// shrink common range to the given range end.
+					commonRangeEnd = otherRange.end;
+				}
+
+				return new Range( commonRangeStart.clone(), commonRangeEnd.clone() );
 			}
 
-			return new Range( commonRangeStart.clone(), commonRangeEnd.clone() );
+			// Ranges do not intersect, so they do not have common part.
+			return null;
 		}
 
 		/**

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

@@ -204,7 +204,7 @@ CKEDITOR.define( [
 				let ranges = [];
 
 				const differenceSet = a.range.getDifference( rangeB );
-				const common = a.range.getCommon( rangeB );
+				const common = a.range.getIntersection( rangeB );
 
 				// Difference is a part of changed range that is modified by ChangeOperation but are not affected
 				// by MoveOperation. This can be zero, one or two ranges (if moved range is inside changed range).

+ 5 - 51
packages/ckeditor5-utils/tests/document/range.js

@@ -139,52 +139,6 @@ describe( 'Range', () => {
 		} );
 	} );
 
-	describe( 'intersectsWith', () => {
-		beforeEach( () => {
-			range = new Range( new Position( [ 3, 7 ], root ), new Position( [ 4, 2, 2 ], root ) );
-		} );
-
-		// [  --  range A  --  ]  <  --  range B --  >
-		it( 'should return false if ranges are next to each other', () => {
-			const otherRange = new Range( new Position( [ 1, 3 ], root ), new Position( [ 3, 6 ], root ) );
-
-			expect( range.intersectsWith( otherRange ) ).to.be.false;
-			expect( otherRange.intersectsWith( range ) ).to.be.false;
-		} );
-
-		// [  --  range A  --  ]<  --  range B --  >
-		it( 'should return false if ranges are touching', () => {
-			const otherRange = new Range( new Position( [ 1, 3 ], root ), new Position( [ 3, 7 ], root ) );
-
-			expect( range.intersectsWith( otherRange ) ).to.be.false;
-			expect( otherRange.intersectsWith( range ) ).to.be.false;
-		} );
-
-		// [  --  range A  <  --  ]  range B --  >
-		it( 'should return true if ranges partially intersects on one of sides', () => {
-			const otherRange = new Range( new Position( [ 1, 3 ], root ), new Position( [ 3, 7, 2 ], root ) );
-
-			expect( range.intersectsWith( otherRange ) ).to.be.true;
-			expect( otherRange.intersectsWith( range ) ).to.be.true;
-		} );
-
-		// [<  --  range A  range B  --  >]
-		it( 'should return true if ranges are same', () => {
-			const otherRange = new Range( new Position( [ 3, 7 ], root ), new Position( [ 4, 2, 2 ], root ) );
-
-			expect( range.intersectsWith( otherRange ) ).to.be.true;
-			expect( otherRange.intersectsWith( range ) ).to.be.true;
-		} );
-
-		// [  --  range A  --  <  --  range B  --  >  ]
-		it( 'should return true if one range contains or is contained by the other', () => {
-			const otherRange = new Range( new Position( [ 1 ], root ), new Position( [ 4, 3 ], root ) );
-
-			expect( range.intersectsWith( otherRange ) ).to.be.true;
-			expect( otherRange.intersectsWith( range ) ).to.be.true;
-		} );
-	} );
-
 	describe( 'containsPosition', () => {
 		beforeEach( () => {
 			range = new Range( new Position( [ 1 ], root ), new Position( [ 3 ], root ) );
@@ -308,7 +262,7 @@ describe( 'Range', () => {
 		} );
 	} );
 
-	describe( 'getCommon', () => {
+	describe( 'getIntersection', () => {
 		let range;
 
 		beforeEach( () => {
@@ -317,28 +271,28 @@ describe( 'Range', () => {
 
 		it( 'should return null if ranges do not intersect', () => {
 			const otherRange = new Range( new Position( [ 5, 4 ], root ), new Position( [ 7 ], root ) );
-			const common = range.getCommon( otherRange );
+			const common = range.getIntersection( otherRange );
 
 			expect( common ).to.be.null;
 		} );
 
 		it( 'should return a range equal to the common part of ranges - original range contains the other range', () => {
 			const otherRange = new Range( new Position( [ 4 ], root ), new Position( [ 5 ], root ) );
-			const common = range.getCommon( otherRange );
+			const common = range.getIntersection( otherRange );
 
 			expect( common.isEqual( otherRange ) ).to.be.true;
 		} );
 
 		it( 'should return a range equal to the common part of ranges - original range is contained by the other range', () => {
 			const otherRange = new Range( new Position( [ 3 ], root ), new Position( [ 6 ], root ) );
-			const common = range.getCommon( otherRange );
+			const common = range.getIntersection( otherRange );
 
 			expect( common.isEqual( range ) ).to.be.true;
 		} );
 
 		it( 'should return a range equal to the common part of ranges - original range intersects with the other range', () => {
 			const otherRange = new Range( new Position( [ 3 ], root ), new Position( [ 4, 7 ], root ) );
-			const common = range.getCommon( otherRange );
+			const common = range.getIntersection( otherRange );
 
 			expect( common.start.path ).to.deep.equal( [ 3, 2 ] );
 			expect( common.end.path ).to.deep.equal( [ 4, 7 ] );