Selaa lähdekoodia

Fixed _setTo usage.

Maciej Bukowski 8 vuotta sitten
vanhempi
commit
d1a5753c2a

+ 5 - 2
packages/ckeditor5-engine/src/controller/datacontroller.js

@@ -194,8 +194,11 @@ export default class DataController {
 		this.model.enqueueChange( 'transparent', writer => {
 			// Clearing selection is a workaround for ticket #569 (LiveRange loses position after removing data from document).
 			// After fixing it this code should be removed.
-			this.model.document.selection._setTo( null );
-			this.model.document.selection._clearAttributes();
+			writer.setSelection( null );
+
+			for ( const key of this.model.document.selection.getAttributeKeys() ) {
+				writer.removeSelectionAttribute( key );
+			}
 
 			writer.remove( ModelRange.createIn( modelRoot ) );
 			writer.insert( this.parse( data ), modelRoot );

+ 2 - 2
packages/ckeditor5-engine/src/conversion/view-selection-to-model-converters.js

@@ -40,8 +40,8 @@ export function convertSelectionChange( model, mapper ) {
 		modelSelection.setTo( ranges, viewSelection.isBackward );
 
 		if ( !modelSelection.isEqual( model.document.selection ) ) {
-			model.change( () => {
-				model.document.selection._setTo( modelSelection );
+			model.change( writer => {
+				writer.setSelection( modelSelection );
 			} );
 		}
 	};

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

@@ -55,8 +55,6 @@ export default function insertContent( model, content, selection ) {
 
 		/* istanbul ignore else */
 		if ( newRange ) {
-			// TODO:
-
 			if ( selection instanceof DocumentSelection ) {
 				selection._setTo( newRange );
 			} else {

+ 4 - 1
packages/ckeditor5-engine/tests/controller/editingcontroller.js

@@ -94,7 +94,10 @@ describe( 'EditingController', () => {
 			buildModelConverter().for( editing.modelToView ).fromMarker( 'marker' ).toHighlight( {} );
 
 			// Note: The below code is highly overcomplicated due to #455.
-			model.document.selection._setTo( null );
+			model.change( writer => {
+				writer.setSelection( null );
+			} );
+
 			modelRoot.removeChildren( 0, modelRoot.childCount );
 
 			viewRoot.removeChildren( 0, viewRoot.childCount );

+ 17 - 14
packages/ckeditor5-engine/tests/conversion/model-selection-to-view-converters.js

@@ -429,7 +429,9 @@ describe( 'model-selection-to-view-converters', () => {
 				);
 
 				const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
-				modelDoc.selection._setTo( modelRange );
+				model.change( writer => {
+					writer.setSelection( modelRange );
+				} );
 
 				dispatcher.convertSelection( modelDoc.selection, [] );
 
@@ -451,7 +453,9 @@ describe( 'model-selection-to-view-converters', () => {
 				mergeAttributes( viewSelection.getFirstPosition() );
 
 				const modelRange = ModelRange.createFromParentsAndOffsets( modelRoot, 1, modelRoot, 1 );
-				modelDoc.selection._setTo( modelRange );
+				model.change( writer => {
+					writer.setSelection( modelRange );
+				} );
 
 				dispatcher.convertSelection( modelDoc.selection, [] );
 
@@ -556,21 +560,20 @@ describe( 'model-selection-to-view-converters', () => {
 		const endPos = new ModelPosition( modelRoot, endPath );
 
 		const isBackward = selectionPaths[ 2 ] === 'backward';
-		docSelection._setTo( new ModelRange( startPos, endPos ), isBackward );
+		model.change( writer => {
+			writer.setSelection( new ModelRange( startPos, endPos ), isBackward );
 
-		// Update selection attributes according to model.
-		// TODO: docSelection.refreshAttributes();
+			// And add or remove passed attributes.
+			for ( const key in selectionAttributes ) {
+				const value = selectionAttributes[ key ];
 
-		// And add or remove passed attributes.
-		for ( const key in selectionAttributes ) {
-			const value = selectionAttributes[ key ];
-
-			if ( value ) {
-				docSelection._setAttribute( key, value );
-			} else {
-				docSelection._removeAttribute( key );
+				if ( value ) {
+					writer.setSelectionAttribute( key, value );
+				} else {
+					writer.removeSelectionAttribute( key );
+				}
 			}
-		}
+		} );
 
 		// Remove view children manually (without firing additional conversion).
 		viewRoot.removeChildren( 0, viewRoot.childCount );

+ 11 - 8
packages/ckeditor5-engine/tests/conversion/modelconversiondispatcher.js

@@ -239,10 +239,12 @@ describe( 'ModelConversionDispatcher', () => {
 			dispatcher.off( 'selection' );
 
 			root.appendChildren( new ModelText( 'foobar' ) );
-			doc.selection._setTo( [
-				new ModelRange( new ModelPosition( root, [ 1 ] ), new ModelPosition( root, [ 3 ] ) ),
-				new ModelRange( new ModelPosition( root, [ 4 ] ), new ModelPosition( root, [ 5 ] ) )
-			] );
+			model.change( writer => {
+				writer.setSelection( [
+					new ModelRange( new ModelPosition( root, [ 1 ] ), new ModelPosition( root, [ 3 ] ) ),
+					new ModelRange( new ModelPosition( root, [ 4 ] ), new ModelPosition( root, [ 5 ] ) )
+				] );
+			} );
 		} );
 
 		it( 'should fire selection event', () => {
@@ -362,8 +364,8 @@ describe( 'ModelConversionDispatcher', () => {
 			const viewFigure = new ViewContainerElement( 'figure', null, viewCaption );
 
 			// Create custom highlight handler mock.
-			viewFigure.setCustomProperty( 'addHighlight', () => {} );
-			viewFigure.setCustomProperty( 'removeHighlight', () => {} );
+			viewFigure.setCustomProperty( 'addHighlight', () => { } );
+			viewFigure.setCustomProperty( 'removeHighlight', () => { } );
 
 			// Create mapper mock.
 			dispatcher.conversionApi.mapper = {
@@ -377,8 +379,9 @@ describe( 'ModelConversionDispatcher', () => {
 			};
 
 			model.markers.set( 'name', ModelRange.createFromParentsAndOffsets( root, 0, root, 1 ) );
-			doc.selection._setTo( ModelRange.createFromParentsAndOffsets( caption, 1, caption, 1 ) );
-
+			model.change( writer => {
+				writer.setSelection( ModelRange.createFromParentsAndOffsets( caption, 1, caption, 1 ) );
+			} );
 			sinon.spy( dispatcher, 'fire' );
 
 			const markers = Array.from( model.markers.getMarkersAtPosition( doc.selection.getFirstPosition() ) );

+ 54 - 31
packages/ckeditor5-engine/tests/dev-utils/model.js

@@ -21,7 +21,10 @@ describe( 'model test utils', () => {
 		root = document.createRoot();
 		selection = document.selection;
 		sandbox = sinon.sandbox.create();
-		selection._setTo( null );
+
+		model.change( writer => {
+			writer.setSelection( null );
+		} );
 
 		model.schema.register( 'a', {
 			allowWhere: '$text',
@@ -64,8 +67,9 @@ describe( 'model test utils', () => {
 		it( 'should use stringify method with selection', () => {
 			const stringifySpy = sandbox.spy( getData, '_stringify' );
 			root.appendChildren( new Element( 'b', null, new Text( 'btext' ) ) );
-			document.selection._setTo( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
-
+			model.change( writer => {
+				writer.setSelection( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
+			} );
 			expect( getData( model ) ).to.equal( '[<b>btext</b>]' );
 			sinon.assert.calledOnce( stringifySpy );
 			sinon.assert.calledWithExactly( stringifySpy, root, document.selection );
@@ -278,7 +282,9 @@ describe( 'model test utils', () => {
 
 			it( 'writes selection in an empty root', () => {
 				const root = document.createRoot( '$root', 'empty' );
-				selection._setTo( root );
+				model.change( writer => {
+					writer.setSelection( root );
+				} );
 
 				expect( stringify( root, selection ) ).to.equal(
 					'[]'
@@ -286,7 +292,9 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'writes selection collapsed in an element', () => {
-				selection._setTo( root );
+				model.change( writer => {
+					writer.setSelection( root );
+				} );
 
 				expect( stringify( root, selection ) ).to.equal(
 					'[]<a></a>foo<$text bold="true">bar</$text><b></b>'
@@ -294,7 +302,9 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'writes selection collapsed in a text', () => {
-				selection._setTo( root, 3 );
+				model.change( writer => {
+					writer.setSelection( root, 3 );
+				} );
 
 				expect( stringify( root, selection ) ).to.equal(
 					'<a></a>fo[]o<$text bold="true">bar</$text><b></b>'
@@ -302,7 +312,9 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'writes selection collapsed at the text left boundary', () => {
-				selection._setTo( elA, 'after' );
+				model.change( writer => {
+					writer.setSelection( elA, 'after' );
+				} );
 
 				expect( stringify( root, selection ) ).to.equal(
 					'<a></a>[]foo<$text bold="true">bar</$text><b></b>'
@@ -310,7 +322,9 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'writes selection collapsed at the text right boundary', () => {
-				selection._setTo( elB, 'before' );
+				model.change( writer => {
+					writer.setSelection( elB, 'before' );
+				} );
 
 				expect( stringify( root, selection ) ).to.equal(
 					'<a></a>foo<$text bold="true">bar[]</$text><b></b>'
@@ -318,7 +332,9 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'writes selection collapsed at the end of the root', () => {
-				selection._setTo( root, 'end' );
+				model.change( writer => {
+					writer.setSelection( root, 'end' );
+				} );
 
 				// Needed due to https://github.com/ckeditor/ckeditor5-engine/issues/320.
 				selection._clearAttributes();
@@ -329,7 +345,9 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'writes selection collapsed selection in a text with attributes', () => {
-				selection._setTo( root, 5 );
+				model.change( writer => {
+					writer.setSelection( root, 5 );
+				} );
 
 				expect( stringify( root, selection ) ).to.equal(
 					'<a></a>foo<$text bold="true">b[]ar</$text><b></b>'
@@ -337,9 +355,9 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'writes flat selection containing couple of nodes', () => {
-				selection._setTo(
-					Range.createFromParentsAndOffsets( root, 0, root, 4 )
-				);
+				model.change( writer => {
+					writer.setSelection( Range.createFromParentsAndOffsets( root, 0, root, 4 ) );
+				} );
 
 				expect( stringify( root, selection ) ).to.equal(
 					'[<a></a>foo]<$text bold="true">bar</$text><b></b>'
@@ -347,9 +365,9 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'writes flat selection within text', () => {
-				selection._setTo(
-					Range.createFromParentsAndOffsets( root, 2, root, 3 )
-				);
+				model.change( writer => {
+					writer.setSelection( Range.createFromParentsAndOffsets( root, 2, root, 3 ) );
+				} );
 
 				expect( stringify( root, selection ) ).to.equal(
 					'<a></a>f[o]o<$text bold="true">bar</$text><b></b>'
@@ -357,9 +375,9 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'writes multi-level selection', () => {
-				selection._setTo(
-					Range.createFromParentsAndOffsets( elA, 0, elB, 0 )
-				);
+				model.change( writer => {
+					writer.setSelection( Range.createFromParentsAndOffsets( elA, 0, elB, 0 ) );
+				} );
 
 				expect( stringify( root, selection ) ).to.equal(
 					'<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
@@ -367,10 +385,9 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'writes selection when is backward', () => {
-				selection._setTo(
-					Range.createFromParentsAndOffsets( elA, 0, elB, 0 ),
-					true
-				);
+				model.change( writer => {
+					writer.setSelection( Range.createFromParentsAndOffsets( elA, 0, elB, 0 ), true );
+				} );
 
 				expect( stringify( root, selection ) ).to.equal(
 					'<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
@@ -381,7 +398,9 @@ describe( 'model test utils', () => {
 				const root = document.createRoot( '$root', 'empty' );
 
 				root.appendChildren( new Text( 'நிலைக்கு' ) );
-				selection._setTo( Range.createFromParentsAndOffsets( root, 2, root, 6 ) );
+				model.change( writer => {
+					writer.setSelection( Range.createFromParentsAndOffsets( root, 2, root, 6 ) );
+				} );
 
 				expect( stringify( root, selection ) ).to.equal( 'நி[லைக்]கு' );
 			} );
@@ -562,10 +581,12 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'sets selection attributes', () => {
-				const result = parse( 'foo[]bar', model.schema, { selectionAttributes: {
-					bold: true,
-					italic: true
-				} } );
+				const result = parse( 'foo[]bar', model.schema, {
+					selectionAttributes: {
+						bold: true,
+						italic: true
+					}
+				} );
 
 				expect( stringify( result.model, result.selection ) ).to.equal( 'foo<$text bold="true" italic="true">[]</$text>bar' );
 			} );
@@ -583,9 +604,11 @@ describe( 'model test utils', () => {
 			} );
 
 			it( 'sets selection with attribute containing an element', () => {
-				const result = parse( 'x[<a></a>]', model.schema, { selectionAttributes: {
-					bold: true
-				} } );
+				const result = parse( 'x[<a></a>]', model.schema, {
+					selectionAttributes: {
+						bold: true
+					}
+				} );
 
 				expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
 				expect( result.selection.getAttribute( 'bold' ) ).to.be.true;

+ 2 - 2
packages/ckeditor5-engine/tests/model/document.js

@@ -580,8 +580,8 @@ describe( 'Document', () => {
 
 			doc.on( 'change', spy );
 
-			model.change( () => {
-				doc.selection._setTo( Range.createFromParentsAndOffsets( root, 2, root, 2 ) );
+			model.change( writer => {
+				writer.setSelection( Range.createFromParentsAndOffsets( root, 2, root, 2 ) );
 			} );
 
 			expect( spy.calledOnce ).to.be.true;

+ 9 - 3
packages/ckeditor5-engine/tests/model/utils/deletecontent.js

@@ -335,7 +335,9 @@ describe( 'DataController utils', () => {
 						new Position( doc.getRoot(), [ 1, 0, 0, 1 ] ) // b]ar
 					);
 
-					doc.selection._setTo( range );
+					model.change( writer => {
+						writer.setSelection( range );
+					} );
 
 					deleteContent( model, doc.selection );
 
@@ -380,7 +382,9 @@ describe( 'DataController utils', () => {
 						new Position( doc.getRoot(), [ 1, 1 ] ) // b]om
 					);
 
-					doc.selection._setTo( range );
+					model.change( writer => {
+						writer.setSelection( range );
+					} );
 
 					deleteContent( model, doc.selection );
 
@@ -423,7 +427,9 @@ describe( 'DataController utils', () => {
 						new Position( doc.getRoot(), [ 1, 0, 0, 3 ] ) // bar]
 					);
 
-					doc.selection._setTo( range );
+					model.change( writer => {
+						writer.setSelection( range );
+					} );
 
 					deleteContent( model, doc.selection );