Forráskód Böngészése

Changed Range.clone and LiveRange.clone to Range.createFromPosition and LiveRange.createFromPosition. Docs LiveRange update.

Szymon Cofalik 10 éve
szülő
commit
cc21916670

+ 36 - 15
packages/ckeditor5-engine/src/treemodel/liverange.js

@@ -34,21 +34,6 @@ CKEDITOR.define( [
 			bindWithDocument.call( this );
 		}
 
-		/**
-		 * Creates and returns a new range which is equal to this LiveRange. Returned object may be
-		 * an instance of Range or LiveRange, depending on passed argument.
-		 *
-		 * @param {Boolean} [makeLiveRange] Flag representing whether new object should be "live". Defaults to false.
-		 * @returns {treeModel.Range|treeModel.LiveRange}
-		 */
-		clone( makeLiveRange ) {
-			if ( makeLiveRange ) {
-				return new LiveRange( this.start, this.end );
-			} else {
-				return super.clone();
-			}
-		}
-
 		/**
 		 * Unbinds all events previously bound by LiveRange. Use it whenever you don't need LiveRange instance
 		 * anymore (i.e. when leaving scope in which it was declared or before re-assigning variable that was
@@ -59,6 +44,42 @@ CKEDITOR.define( [
 			this.end.detach();
 			this.stopListening();
 		}
+
+		/**
+		 * @see {@link treeModel.Range#createFromElement}
+		 * @static
+		 * @method createFromElement
+		 * @param {treeModel.Element} element
+		 * @returns {treeModel.LiveRange}
+		 */
+
+		/**
+		 * @see {@link treeModel.Range#createFromPositionAndShift}
+		 * @static
+		 * @method createFromPositionAndShift
+		 * @param {treeModel.Position} position
+		 * @param {Number} shift
+		 * @returns {treeModel.LiveRange}
+		 */
+
+		/**
+		 * @see {@link treeModel.Range#createFromParentsAndOffsets}
+		 * @static
+		 * @method createFromParentsAndOffsets
+		 * @param {treeModel.Element} startElement
+		 * @param {Number} startOffset
+		 * @param {treeModel.Element} endElement
+		 * @param {Number} endOffset
+		 * @returns {treeModel.LiveRange}
+		 */
+
+		/**
+		 * @see {@link treeModel.Range#createFromRange}
+		 * @static
+		 * @method createFromRange
+		 * @param {treeModel.Range} range
+		 * @returns {treeModel.LiveRange}
+		 */
 	}
 
 	/**

+ 3 - 2
packages/ckeditor5-engine/src/treemodel/operation/attributeoperation.js

@@ -7,8 +7,9 @@
 
 CKEDITOR.define( [
 	'treemodel/operation/operation',
+	'treemodel/range',
 	'ckeditorerror'
-], ( Operation, CKEditorError ) => {
+], ( Operation, Range, CKEditorError ) => {
 	/**
 	 * Operation to change nodes' attribute. Using this class you can add, remove or change value of the attribute.
 	 *
@@ -42,7 +43,7 @@ CKEDITOR.define( [
 			 * @readonly
 			 * @type {treeModel.Range}
 			 */
-			this.range = range.clone();
+			this.range = Range.createFromRange( range );
 
 			/**
 			 * Old attribute to change. Set to `null` if operation inserts a new attribute.

+ 12 - 12
packages/ckeditor5-engine/src/treemodel/range.js

@@ -53,16 +53,6 @@ CKEDITOR.define( [ 'treemodel/position', 'treemodel/positioniterator', 'utils' ]
 			return new PositionIterator( this );
 		}
 
-		/**
-		 * Creates and returns a new instance of {@link treeModel.Range}
-		 * which is equal to this {@link treeModel.Range range}.
-		 *
-		 * @returns {treeModel.Position} Cloned {@link treeModel.Range range}.
-		 */
-		clone() {
-			return new Range( this.start, this.end );
-		}
-
 		/**
 		 * Checks whether this contains given {@link treeModel.Position position}.
 		 *
@@ -124,7 +114,7 @@ CKEDITOR.define( [ 'treemodel/position', 'treemodel/positioniterator', 'utils' ]
 				}
 			} else {
 				// Ranges do not intersect, return the original range.
-				ranges.push( this.clone() );
+				ranges.push( Range.createFromRange( this ) );
 			}
 
 			return ranges;
@@ -238,7 +228,7 @@ CKEDITOR.define( [ 'treemodel/position', 'treemodel/positioniterator', 'utils' ]
 				// If insertion is not inside the range, simply transform range boundaries (positions) by the insertion.
 				// Both, one or none of them might be affected by the insertion.
 
-				const range = this.clone();
+				const range = Range.createFromRange( this );
 
 				range.start = range.start.getTransformedByInsertion( insertPosition, howMany, true );
 				range.end = range.end.getTransformedByInsertion( insertPosition, howMany, false );
@@ -296,6 +286,16 @@ CKEDITOR.define( [ 'treemodel/position', 'treemodel/positioniterator', 'utils' ]
 				Position.createFromParentAndOffset( endElement, endOffset )
 			);
 		}
+
+		/**
+		 * Creates and returns a new instance of Range which is equal to passed range.
+		 *
+		 * @param {treeModel.Range} range Range to clone.
+		 * @returns {treeModel.Range}
+		 */
+		static createFromRange( range ) {
+			return new this( range.start, range.end );
+		}
 	}
 
 	return Range;

+ 8 - 21
packages/ckeditor5-engine/tests/treemodel/liverange.js

@@ -55,25 +55,6 @@ describe( 'LiveRange', () => {
 		expect( live ).to.be.instanceof( Range );
 	} );
 
-	it( 'should return instance of Range when cloned', () => {
-		let live = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
-		let clone = live.clone();
-
-		expect( clone ).not.to.be.instanceof( LiveRange );
-
-		live.detach();
-	} );
-
-	it( 'should return instance of LiveRange when cloned with flag set to true', () => {
-		let live = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
-		let clone = live.clone( true );
-
-		expect( clone ).to.be.instanceof( LiveRange );
-
-		live.detach();
-		clone.detach();
-	} );
-
 	it( 'should listen to a change event of the document that owns this range', () => {
 		sinon.spy( LiveRange.prototype, 'listenTo' );
 
@@ -114,7 +95,13 @@ describe( 'LiveRange', () => {
 		range.detach();
 	} );
 
-	// Examples may be weird when you compare them with the tree structure generated at the beginning of tests.
+	it( 'createFromRange should return LiveRange', () => {
+		let range = LiveRange.createFromRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ) );
+		expect( range ).to.be.instanceof( LiveRange );
+		range.detach();
+	} );
+
+	// Examples may seem weird when you compare them with the tree structure generated at the beginning of tests.
 	// Since change event is fired _after_ operation is executed on tree model, you have to imagine that generated
 	// structure is representing what is _after_ operation is executed. So live LiveRange properties are describing
 	// virtual tree that is not existing anymore and event ranges are operating on the tree generated above.
@@ -369,7 +356,7 @@ describe( 'LiveRange', () => {
 
 		beforeEach( () => {
 			live = new LiveRange( new Position( root, [ 0, 1, 4 ] ), new Position( root, [ 0, 2, 2 ] ) );
-			clone = live.clone();
+			clone = Range.createFromRange( live );
 		} );
 
 		afterEach( () => {

+ 2 - 2
packages/ckeditor5-engine/tests/treemodel/operation/transform.js

@@ -548,7 +548,7 @@ describe( 'transform', () => {
 			describe( 'by AttributeOperation', () => {
 				it( 'attributes have different key: no operation update', () => {
 					let transformBy = new AttributeOperation(
-						range.clone(),
+						Range.createFromRange( range ),
 						new Attribute( 'abc', true ),
 						new Attribute( 'abc', false ),
 						baseVersion
@@ -562,7 +562,7 @@ describe( 'transform', () => {
 
 				it( 'attributes set same value: no operation update', () => {
 					let transformBy = new AttributeOperation(
-						range.clone(),
+						Range.createFromRange( range ),
 						oldAttr,
 						newAttr,
 						baseVersion

+ 9 - 9
packages/ckeditor5-engine/tests/treemodel/range.js

@@ -129,6 +129,15 @@ describe( 'Range', () => {
 				expect( range.end.path ).to.deep.equal( [ 1, 2, 7 ] );
 			} );
 		} );
+
+		describe( 'createFromRange', () => {
+			it( 'should create a new instance of Range that is equal to passed range', () => {
+				const clone = Range.createFromRange( range );
+
+				expect( clone ).not.to.be.equal( range ); // clone is not pointing to the same object as position
+				expect( clone.isEqual( range ) ).to.be.true; // but they are equal in the position-sense
+			} );
+		} );
 	} );
 
 	describe( 'getNodes', () => {
@@ -160,15 +169,6 @@ describe( 'Range', () => {
 		} );
 	} );
 
-	describe( 'clone', () => {
-		it( 'should return a new, equal position', () => {
-			const clone = range.clone();
-
-			expect( clone ).not.to.be.equal( range ); // clone is not pointing to the same object as position
-			expect( clone.isEqual( range ) ).to.be.true; // but they are equal in the position-sense
-		} );
-	} );
-
 	describe( 'containsPosition', () => {
 		beforeEach( () => {
 			range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) );