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

Made sure now one will ever use DocumentSelection.createFromSelection().

Piotrek Koszuliński 8 лет назад
Родитель
Сommit
8e8f57c151

+ 14 - 11
packages/ckeditor5-engine/src/model/documentselection.js

@@ -241,17 +241,19 @@ export default class DocumentSelection extends Selection {
 	}
 
 	/**
-	 * Creates and returns an instance of `DocumentSelection` that is a clone of given selection, meaning that it has same
-	 * ranges and same direction as this selection.
-	 *
-	 * @params {module:engine/model/selection~Selection} otherSelection Selection to be cloned.
-	 * @returns {module:engine/model/documentselection~DocumentSelection} `Selection` instance that is a clone of given selection.
+	 * This method is not available in `DocumentSelection`. There can be only one
+	 * `DocumentSelection` per document instance, so creating new `DocumentSelection`s this way
+	 * would be unsafe.
 	 */
-	static createFromSelection( otherSelection ) {
-		const selection = new this( otherSelection._document );
-		selection.setTo( otherSelection );
-
-		return selection;
+	static createFromSelection() {
+		/**
+		 * `DocumentSelection#createFromSelection()` is not available. There can be only one
+		 * `DocumentSelection` per document instance, so creating new `DocumentSelection`s this way
+		 * would be unsafe.
+		 *
+		 * @error documentselection-cannot-create
+		 */
+		throw new CKEditorError( 'documentselection-cannot-create: Cannot create new DocumentSelection instance.' );
 	}
 
 	/**
@@ -671,7 +673,8 @@ export default class DocumentSelection extends Selection {
  * @event change:attribute
  */
 
-// Helper function for {@link module:engine/model/documentselection~DocumentSelection#_updateAttributes}. It takes model item, checks whether
+// Helper function for {@link module:engine/model/documentselection~DocumentSelection#_updateAttributes}.
+// It takes model item, checks whether
 // it is a text node (or text proxy) and if so, returns it's attributes. If not, returns `null`.
 //
 // @param {module:engine/model/item~Item|null}  node

+ 10 - 20
packages/ckeditor5-engine/tests/model/documentselection.js

@@ -374,10 +374,12 @@ describe( 'DocumentSelection', () => {
 	} );
 
 	describe( 'createFromSelection()', () => {
-		it( 'should return a DocumentSelection instance', () => {
+		it( 'should throw', () => {
 			selection.addRange( range, true );
 
-			expect( DocumentSelection.createFromSelection( selection ) ).to.be.instanceof( DocumentSelection );
+			expect( () => {
+				DocumentSelection.createFromSelection( selection );
+			} ).to.throw( CKEditorError, /^documentselection-cannot-create:/ );
 		} );
 	} );
 
@@ -893,49 +895,37 @@ describe( 'DocumentSelection', () => {
 			it( 'ignores attributes inside an object if selection contains that object', () => {
 				setData( doc, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
 
-				const liveSelection = DocumentSelection.createFromSelection( selection );
-
-				expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
+				expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
 			} );
 
 			it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
 				setData( doc, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
 
-				const liveSelection = DocumentSelection.createFromSelection( selection );
-
-				expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
+				expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
 			} );
 
 			it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
 				setData( doc, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
 
-				const liveSelection = DocumentSelection.createFromSelection( selection );
-
-				expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
+				expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
 			} );
 
 			it( 'reads attributes from text even if the selection contains an object', () => {
 				setData( doc, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
 
-				const liveSelection = DocumentSelection.createFromSelection( selection );
-
-				expect( liveSelection.getAttribute( 'bold' ) ).to.equal( true );
+				expect( selection.getAttribute( 'bold' ) ).to.equal( true );
 			} );
 
 			it( 'reads attributes when the entire selection inside an object', () => {
 				setData( doc, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
 
-				const liveSelection = DocumentSelection.createFromSelection( selection );
-
-				expect( liveSelection.getAttribute( 'bold' ) ).to.equal( true );
+				expect( selection.getAttribute( 'bold' ) ).to.equal( true );
 			} );
 
 			it( 'stops reading attributes if selection starts with an object', () => {
 				setData( doc, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
 
-				const liveSelection = DocumentSelection.createFromSelection( selection );
-
-				expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
+				expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
 			} );
 		} );
 	} );