Przeglądaj źródła

Fixed Writer failing tests.

Oskar Wróbel 8 lat temu
rodzic
commit
f26d192360

+ 11 - 11
packages/ckeditor5-engine/src/model/writer.js

@@ -169,7 +169,7 @@ export default class Writer {
 
 		const insert = new InsertOperation( position, item, this.model.document.version );
 
-		this._batch.addDelta( delta );
+		this.batch.addDelta( delta );
 		delta.addOperation( insert );
 		this.model.applyOperation( insert );
 
@@ -431,7 +431,7 @@ export default class Writer {
 		}
 
 		const delta = new MoveDelta();
-		this._batch.addDelta( delta );
+		this.batch.addDelta( delta );
 
 		const operation = new MoveOperation( range.start, range.end.offset - range.start.offset, position, this.model.document.version );
 		delta.addOperation( operation );
@@ -446,7 +446,7 @@ export default class Writer {
 	remove( itemOrRange ) {
 		const addRemoveDelta = ( position, howMany ) => {
 			const delta = new RemoveDelta();
-			this._batch.addDelta( delta );
+			this.batch.addDelta( delta );
 			let operation;
 
 			if ( position.root.document ) {
@@ -486,7 +486,7 @@ export default class Writer {
 	 */
 	merge( position ) {
 		const delta = new MergeDelta();
-		this._batch.addDelta( delta );
+		this.batch.addDelta( delta );
 
 		const nodeBefore = position.nodeBefore;
 		const nodeAfter = position.nodeAfter;
@@ -550,7 +550,7 @@ export default class Writer {
 		}
 
 		const delta = new RenameDelta();
-		this._batch.addDelta( delta );
+		this.batch.addDelta( delta );
 
 		const renameOperation = new RenameOperation( Position.createBefore( element ), element.name, newName, this.model.document.version );
 		delta.addOperation( renameOperation );
@@ -567,7 +567,7 @@ export default class Writer {
 	 */
 	split( position ) {
 		const delta = new SplitDelta();
-		this._batch.addDelta( delta );
+		this.batch.addDelta( delta );
 
 		const splitElement = position.parent;
 
@@ -642,7 +642,7 @@ export default class Writer {
 		}
 
 		const delta = new WrapDelta();
-		this._batch.addDelta( delta );
+		this.batch.addDelta( delta );
 
 		const insert = new InsertOperation( range.end, element, this.model.document.version );
 		delta.addOperation( insert );
@@ -676,7 +676,7 @@ export default class Writer {
 		}
 
 		const delta = new UnwrapDelta();
-		this._batch.addDelta( delta );
+		this.batch.addDelta( delta );
 
 		const sourcePosition = Position.createFromParentAndOffset( element, 0 );
 
@@ -820,7 +820,7 @@ function setAttributeToRange( writer, key, value, range ) {
 	function addOperation() {
 		// Add delta to the batch only if there is at least operation in the delta. Add delta only once.
 		if ( delta.operations.length === 0 ) {
-			writer._batch.addDelta( delta );
+			writer.batch.addDelta( delta );
 		}
 
 		const range = new Range( lastSplitPosition, position );
@@ -846,7 +846,7 @@ function setAttributeToItem( writer, key, value, item ) {
 
 	if ( previousValue != value ) {
 		const delta = item.root === item ? new RootAttributeDelta() : new AttributeDelta();
-		writer._batch.addDelta( delta );
+		writer.batch.addDelta( delta );
 
 		if ( item.root === item ) {
 			// If we change attributes of root element, we have to use `RootAttributeOperation`.
@@ -885,7 +885,7 @@ function addMarkerOperation( writer, name, oldRange, newRange ) {
 
 	const operation = new MarkerOperation( name, oldRange, newRange, model.markers, doc.version );
 
-	writer._batch.addDelta( delta );
+	writer.batch.addDelta( delta );
 	delta.addOperation( operation );
 	model.applyOperation( operation );
 }

+ 23 - 25
packages/ckeditor5-engine/tests/model/writer.js

@@ -34,6 +34,10 @@ describe( 'Writer', () => {
 		it( 'should have model instance', () => {
 			expect( writer.model ).to.instanceof( Model );
 		} );
+
+		it( 'should have batch instance', () => {
+			expect( writer.batch ).to.instanceof( Batch );
+		} );
 	} );
 
 	describe( 'createText()', () => {
@@ -330,7 +334,7 @@ describe( 'Writer', () => {
 			docFrag.markers.set( 'marker1', marker1 );
 			docFrag.markers.set( 'marker2', marker2 );
 
-			model.on( 'change', spy );
+			doc.on( 'change', spy );
 
 			writer.insert( docFrag, new Position( root, [ 2 ] ) );
 
@@ -911,7 +915,7 @@ describe( 'Writer', () => {
 			function getChangesAttrsCount() {
 				let totalNumber = 0;
 
-				for ( const delta of writer._batch.deltas ) {
+				for ( const delta of writer.batch.deltas ) {
 					for ( const operation of delta.operations ) {
 						if ( operation.range ) {
 							totalNumber += count( operation.range.getItems( { singleCharacters: true } ) );
@@ -1226,11 +1230,11 @@ describe( 'Writer', () => {
 
 			writer.setAttribute( 'a', 1, nodeA );
 
-			expect( writer._batch.deltas.length ).to.equal( 0 );
+			expect( writer.batch.deltas.length ).to.equal( 0 );
 
 			writer.removeAttribute( 'x', Range.createIn( root ) );
 
-			expect( writer._batch.deltas.length ).to.equal( 0 );
+			expect( writer.batch.deltas.length ).to.equal( 0 );
 		} );
 	} );
 
@@ -1457,15 +1461,15 @@ describe( 'Writer', () => {
 			it( 'should create minimal number of remove deltas, each with only one operation', () => {
 				writer.remove( range );
 
-				expect( writer._batch.deltas.length ).to.equal( 2 );
-				expect( writer._batch.deltas[ 0 ].operations.length ).to.equal( 1 );
-				expect( writer._batch.deltas[ 1 ].operations.length ).to.equal( 1 );
+				expect( writer.batch.deltas.length ).to.equal( 2 );
+				expect( writer.batch.deltas[ 0 ].operations.length ).to.equal( 1 );
+				expect( writer.batch.deltas[ 1 ].operations.length ).to.equal( 1 );
 			} );
 
 			it( 'should use RemoveOperation', () => {
 				writer.remove( div );
 
-				expect( writer._batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'remove' );
+				expect( writer.batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'remove' );
 			} );
 		} );
 
@@ -1510,15 +1514,15 @@ describe( 'Writer', () => {
 			it( 'should create minimal number of remove deltas, each with only one operation', () => {
 				writer.remove( range );
 
-				expect( writer._batch.deltas.length ).to.equal( 2 );
-				expect( writer._batch.deltas[ 0 ].operations.length ).to.equal( 1 );
-				expect( writer._batch.deltas[ 1 ].operations.length ).to.equal( 1 );
+				expect( writer.batch.deltas.length ).to.equal( 2 );
+				expect( writer.batch.deltas[ 0 ].operations.length ).to.equal( 1 );
+				expect( writer.batch.deltas[ 1 ].operations.length ).to.equal( 1 );
 			} );
 
 			it( 'should use DetachOperation', () => {
 				writer.remove( div );
 
-				expect( writer._batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'detach' );
+				expect( writer.batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'detach' );
 			} );
 		} );
 	} );
@@ -1726,12 +1730,11 @@ describe( 'Writer', () => {
 		} );
 
 		it( 'should accept marker instance', () => {
-			writer.setMarker( 'name', range );
-			const marker = model.markers.get( 'name' );
+			const marker = model.markers.set( 'name', range );
 			const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
 
 			writer.setMarker( marker, range2 );
-			const op = writer._batch.deltas[ 0 ].operations[ 0 ];
+			const op = writer.batch.deltas[ 0 ].operations[ 0 ];
 
 			expect( model.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
 			expect( op.oldRange.isEqual( range ) ).to.be.true;
@@ -1740,20 +1743,15 @@ describe( 'Writer', () => {
 
 		it( 'should accept empty range parameter if marker instance is passed', () => {
 			const marker = model.markers.set( 'name', range );
+			const spy = sinon.spy();
 
-			sinon.spy( model, 'fire' );
-
-			model.on( 'change', ( evt, type, changes ) => {
-				if ( type == 'marker' ) {
-					expect( changes.type ).to.equal( 'set' );
-					expect( changes.name ).to.equal( 'name' );
-				}
-			} );
+			doc.on( 'change', spy );
 
 			writer.setMarker( marker );
-			const op = writer._batch.deltas[ 0 ].operations[ 0 ];
+			const op = writer.batch.deltas[ 0 ].operations[ 0 ];
 
-			expect( model.fire.calledWith( 'change', 'marker' ) ).to.be.true;
+			sinon.assert.calledOnce( spy );
+			sinon.assert.calledWith( spy, sinon.match.any, 'marker' );
 			expect( op.oldRange ).to.be.null;
 			expect( op.newRange.isEqual( range ) ).to.be.true;
 		} );