Преглед изворни кода

Removed removeAllRanges method.

Maciej Bukowski пре 8 година
родитељ
комит
c2cbb0891b

+ 2 - 2
packages/ckeditor5-engine/src/dev-utils/model.js

@@ -185,10 +185,10 @@ export function stringify( node, selectionOrPositionOrRange = null ) {
 		selection = selectionOrPositionOrRange;
 	} else if ( selectionOrPositionOrRange instanceof ModelRange ) {
 		selection = new ModelSelection();
-		selection.addRange( selectionOrPositionOrRange );
+		selection._addRange( selectionOrPositionOrRange );
 	} else if ( selectionOrPositionOrRange instanceof ModelPosition ) {
 		selection = new ModelSelection();
-		selection.addRange( new ModelRange( selectionOrPositionOrRange, selectionOrPositionOrRange ) );
+		selection._addRange( new ModelRange( selectionOrPositionOrRange, selectionOrPositionOrRange ) );
 	}
 
 	// Setup model to view converter.

+ 1 - 1
packages/ckeditor5-engine/src/model/documentselection.js

@@ -122,7 +122,7 @@ export default class DocumentSelection {
 	}
 
 	_removeAllRanges() {
-		this._selection.removeAllRanges();
+		this._selection.setTo( null );
 	}
 
 	_getStoredAttributes() {

+ 2 - 10
packages/ckeditor5-engine/src/model/liveselection.js

@@ -158,16 +158,8 @@ export default class LiveSelection extends Selection {
 	/**
 	 * @inheritDoc
 	 */
-	addRange( range, isBackward = false ) {
-		super.addRange( range, isBackward );
-		this.refreshAttributes();
-	}
-
-	/**
-	 * @inheritDoc
-	 */
-	removeAllRanges() {
-		super.removeAllRanges();
+	_addRange( range, isBackward = false ) {
+		super._addRange( range, isBackward );
 		this.refreshAttributes();
 	}
 

+ 9 - 80
packages/ckeditor5-engine/src/model/selection.js

@@ -282,18 +282,6 @@ export default class Selection {
 		this.fire( 'change:range', { directChange: true } );
 	}
 
-	/**
-	 * Removes all ranges that were added to the selection.
-	 *
-	 * @fires change:range
-	 */
-	removeAllRanges() {
-		if ( this._ranges.length > 0 ) {
-			this._removeAllRanges();
-			this.fire( 'change:range', { directChange: true } );
-		}
-	}
-
 	/**
 	 * Sets this selection's ranges and direction to the specified location based on the given
 	 * {@link module:engine/model/selection~Selection selection}, {@link module:engine/model/position~Position position},
@@ -302,10 +290,11 @@ export default class Selection {
 	 * @protected
 	 * @param {module:engine/model/selection~Selection|module:engine/model/position~Position|
 	 * Iterable.<module:engine/model/range~Range>|module:engine/model/range~Range|null} selectable
+	 * @param {string|boolean|number} [backwardSelectionOrOffset]
 	 */
 	setTo( selectable, backwardSelectionOrOffset ) {
 		if ( !selectable ) {
-			this.removeAllRanges();
+			this._setRanges( [] );
 		} else if ( selectable instanceof Selection ) {
 			this._setRanges( selectable.getRanges(), selectable.isBackward );
 		} else if ( selectable instanceof Range ) {
@@ -313,7 +302,7 @@ export default class Selection {
 		} else if ( selectable instanceof Position ) {
 			this._setRanges( [ new Range( selectable ) ] );
 		} else if ( selectable instanceof Element ) {
-			this._setRanges( Position.createAt( selectable, backwardSelectionOrOffset ) );
+			this._setRanges( [ Range.createCollapsedAt( selectable, backwardSelectionOrOffset ) ] );
 		} else if ( isIterable( selectable ) ) {
 			// We assume that the selectable is an iterable of ranges.
 			this._setRanges( selectable, backwardSelectionOrOffset );
@@ -363,66 +352,18 @@ export default class Selection {
 		this.fire( 'change:range', { directChange: true } );
 	}
 
-<<<<<<< HEAD
 	/**
-	 * Sets this selection's ranges and direction to the specified location based on the given
-	 * {@link module:engine/model/selection~Selection selection}, {@link module:engine/model/position~Position position},
-	 * {@link module:engine/model/range~Range range} or an iterable of {@link module:engine/model/range~Range ranges}.
+	 * Deletes ranges from internal range array. Uses {@link #_popRange _popRange} to
+	 * ensure proper ranges removal.
 	 *
-	 * @param {module:engine/model/selection~Selection|module:engine/model/position~Position|
-	 * Iterable.<module:engine/model/range~Range>|module:engine/model/range~Range} selectable
+	 * @private
 	 */
-	setTo( selectable ) {
-		if ( selectable instanceof Selection ) {
-			this.setRanges( selectable.getRanges(), selectable.isBackward );
-		} else if ( selectable instanceof Range ) {
-			this.setRanges( [ selectable ] );
-		} else if ( isIterable( selectable ) ) {
-			// We assume that the selectable is an iterable of ranges.
-			this.setRanges( selectable );
-		} else {
-			// We assume that the selectable is a position.
-			this.setRanges( [ new Range( selectable ) ] );
+	_removeAllRanges() {
+		while ( this._ranges.length > 0 ) {
+			this._popRange();
 		}
 	}
 
-	/**
-	 * Sets this selection in the provided element.
-	 *
-	 * @param {module:engine/model/element~Element} element
-	 */
-	setIn( element ) {
-		this.setRanges( [ Range.createIn( element ) ] );
-	}
-
-	/**
-	 * Sets this selection on the provided item.
-	 *
-	 * @param {module:engine/model/item~Item} item
-	 */
-	setOn( item ) {
-		this.setRanges( [ Range.createOn( item ) ] );
-	}
-=======
-	// /**
-	//  * Sets this selection in the provided element.
-	//  *
-	//  * @param {module:engine/model/element~Element} element
-	//  */
-	// setIn( element ) {
-	// 	this.setRanges( [ Range.createIn( element ) ] );
-	// }
-
-	// /**
-	//  * Sets this selection on the provided item.
-	//  *
-	//  * @param {module:engine/model/item~Item} item
-	//  */
-	// setOn( item ) {
-	// 	this.setRanges( [ Range.createOn( item ) ] );
-	// }
->>>>>>> WIP - DocumentSelection. part 2.
-
 	/**
 	 * Sets collapsed selection at the specified location.
 	 *
@@ -792,18 +733,6 @@ export default class Selection {
 		this._ranges.pop();
 	}
 
-	/**
-	 * Deletes ranges from internal range array. Uses {@link #_popRange _popRange} to
-	 * ensure proper ranges removal.
-	 *
-	 * @private
-	 */
-	_removeAllRanges() {
-		while ( this._ranges.length > 0 ) {
-			this._popRange();
-		}
-	}
-
 	/**
 	 * @event change
 	 */

+ 16 - 16
packages/ckeditor5-engine/tests/model/selection.js

@@ -272,19 +272,19 @@ describe( 'Selection', () => {
 		} );
 	} );
 
-	describe( 'setCollapsedAt()', () => {
+	describe( 'setTo - set to position or item', () => {
 		it( 'fires change:range', () => {
 			const spy = sinon.spy();
 
 			selection.on( 'change:range', spy );
 
-			selection.setCollapsedAt( root );
+			selection.setTo( root );
 
 			expect( spy.calledOnce ).to.be.true;
 		} );
 
 		it( 'sets selection at the 0 offset if second parameter not passed', () => {
-			selection.setCollapsedAt( root );
+			selection.setTo( root );
 
 			expect( selection ).to.have.property( 'isCollapsed', true );
 
@@ -294,7 +294,7 @@ describe( 'Selection', () => {
 		} );
 
 		it( 'sets selection at given offset in given parent', () => {
-			selection.setCollapsedAt( root, 3 );
+			selection.setTo( root, 3 );
 
 			expect( selection ).to.have.property( 'isCollapsed', true );
 
@@ -304,7 +304,7 @@ describe( 'Selection', () => {
 		} );
 
 		it( 'sets selection at the end of the given parent', () => {
-			selection.setCollapsedAt( root, 'end' );
+			selection.setTo( root, 'end' );
 
 			expect( selection ).to.have.property( 'isCollapsed', true );
 
@@ -314,7 +314,7 @@ describe( 'Selection', () => {
 		} );
 
 		it( 'sets selection before the specified element', () => {
-			selection.setCollapsedAt( root.getChild( 1 ), 'before' );
+			selection.setTo( root.getChild( 1 ), 'before' );
 
 			expect( selection ).to.have.property( 'isCollapsed', true );
 
@@ -324,7 +324,7 @@ describe( 'Selection', () => {
 		} );
 
 		it( 'sets selection after the specified element', () => {
-			selection.setCollapsedAt( root.getChild( 1 ), 'after' );
+			selection.setTo( root.getChild( 1 ), 'after' );
 
 			expect( selection ).to.have.property( 'isCollapsed', true );
 
@@ -336,7 +336,7 @@ describe( 'Selection', () => {
 		it( 'sets selection at the specified position', () => {
 			const pos = Position.createFromParentAndOffset( root, 3 );
 
-			selection.setCollapsedAt( pos );
+			selection.setTo( pos );
 
 			expect( selection ).to.have.property( 'isCollapsed', true );
 
@@ -518,7 +518,7 @@ describe( 'Selection', () => {
 		} );
 	} );
 
-	describe( 'removeAllRanges()', () => {
+	describe( 'setTo - selection set to null', () => {
 		let spy;
 
 		it( 'should remove all stored ranges', () => {
@@ -1117,7 +1117,7 @@ describe( 'Selection', () => {
 
 		describe( 'setAttribute()', () => {
 			it( 'should set given attribute on the selection', () => {
-				selection._setRanges( [ rangeInFullP ] );
+				selection.setTo( [ rangeInFullP ] );
 				selection.setAttribute( 'foo', 'bar' );
 
 				expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
@@ -1152,7 +1152,7 @@ describe( 'Selection', () => {
 
 		describe( 'getAttributes()', () => {
 			it( 'should return an iterator that iterates over all attributes set on selection', () => {
-				selection._setRanges( [ rangeInFullP ] );
+				selection.setTo( [ rangeInFullP ] );
 				selection.setAttribute( 'foo', 'bar' );
 				selection.setAttribute( 'abc', 'xyz' );
 
@@ -1164,7 +1164,7 @@ describe( 'Selection', () => {
 
 		describe( 'getAttributeKeys()', () => {
 			it( 'should return iterator that iterates over all attribute keys set on selection', () => {
-				selection._setRanges( [ rangeInFullP ] );
+				selection.setTo( [ rangeInFullP ] );
 				selection.setAttribute( 'foo', 'bar' );
 				selection.setAttribute( 'abc', 'xyz' );
 
@@ -1176,7 +1176,7 @@ describe( 'Selection', () => {
 
 		describe( 'hasAttribute()', () => {
 			it( 'should return true if element contains attribute with given key', () => {
-				selection._setRanges( [ rangeInFullP ] );
+				selection.setTo( [ rangeInFullP ] );
 				selection.setAttribute( 'foo', 'bar' );
 
 				expect( selection.hasAttribute( 'foo' ) ).to.be.true;
@@ -1189,7 +1189,7 @@ describe( 'Selection', () => {
 
 		describe( 'clearAttributes()', () => {
 			it( 'should remove all attributes from the element', () => {
-				selection._setRanges( [ rangeInFullP ] );
+				selection.setTo( [ rangeInFullP ] );
 				selection.setAttribute( 'foo', 'bar' );
 				selection.setAttribute( 'abc', 'xyz' );
 
@@ -1222,7 +1222,7 @@ describe( 'Selection', () => {
 
 		describe( 'removeAttribute()', () => {
 			it( 'should remove attribute', () => {
-				selection._setRanges( [ rangeInFullP ] );
+				selection.setTo( [ rangeInFullP ] );
 				selection.setAttribute( 'foo', 'bar' );
 				selection.removeAttribute( 'foo' );
 
@@ -1282,7 +1282,7 @@ describe( 'Selection', () => {
 			} );
 
 			it( 'should not fire change:attribute event if attributes had not changed', () => {
-				selection._setRanges( [ rangeInFullP ] );
+				selection.setTo( [ rangeInFullP ] );
 				selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
 
 				const spy = sinon.spy();