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

Used DetachOperation for removing detached items.

Oskar Wróbel 8 лет назад
Родитель
Сommit
e28f13226d

+ 13 - 4
packages/ckeditor5-engine/src/model/batch.js

@@ -20,6 +20,7 @@ import WeakInsertDelta from './delta/weakinsertdelta';
 import WrapDelta from './delta/wrapdelta';
 
 import AttributeOperation from './operation/attributeoperation';
+import DetachOperation from './operation/detachoperation';
 import InsertOperation from './operation/insertoperation';
 import MarkerOperation from './operation/markeroperation';
 import MoveOperation from './operation/moveoperation';
@@ -131,7 +132,7 @@ export default class Batch {
 		}
 	}
 
-	createText( data, attributes = {} ) {
+	createText( data, attributes ) {
 		return new Text( data, attributes );
 	}
 
@@ -315,11 +316,19 @@ export default class Batch {
 		const addRemoveDelta = ( position, howMany ) => {
 			const delta = new RemoveDelta();
 			this.addDelta( delta );
+			let operation;
 
-			const graveyard = this.document.graveyard;
-			const gyPosition = new Position( graveyard, [ 0 ] );
+			if ( position.root.document ) {
+				const graveyard = this.document.graveyard;
+				const gyPosition = new Position( graveyard, [ 0 ] );
+
+				operation = new RemoveOperation( position, howMany, gyPosition, this.document.version );
+			} else {
+				const range = Range.createFromPositionAndShift( position, howMany );
+
+				operation = new DetachOperation( range, this.document.version );
+			}
 
-			const operation = new RemoveOperation( position, howMany, gyPosition, this.document.version );
 			delta.addOperation( operation );
 			this.document.applyOperation( operation );
 		};

+ 122 - 38
packages/ckeditor5-engine/tests/model/batch.js

@@ -1511,65 +1511,149 @@ describe( 'Batch', () => {
 	} );
 
 	describe( 'remove()', () => {
-		let doc, root, div, p, batch, chain, range;
+		let doc, batch, div, p, range;
 
 		beforeEach( () => {
 			doc = new Document();
-			root = doc.createRoot();
-
-			div = new Element( 'div', [], new Text( 'foobar' ) );
-			p = new Element( 'p', [], new Text( 'abcxyz' ) );
+			batch = doc.batch();
 
-			div.insertChildren( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
-			div.insertChildren( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
+			div = batch.createElement( 'div' );
+			batch.appendText( 'foobar', null, div );
 
-			root.insertChildren( 0, [ div, p ] );
+			p = batch.createElement( 'p' );
+			batch.appendText( 'abcxyz', null, p );
 
-			batch = doc.batch();
+			batch.insertElement( 'p', null, div );
+			batch.appendElement( 'p', null, div );
 
-			// Range starts in ROOT > DIV > P > gg|gg.
-			// Range ends in ROOT > DIV > ...|ar.
-			range = new Range( new Position( root, [ 0, 0, 2 ] ), new Position( root, [ 0, 5 ] ) );
+			batch.insertText( 'gggg', null, new Position( div, [ 0, 0 ] ) );
+			batch.insertText( 'hhhh', null, new Position( div, [ 7, 0 ] ) );
 		} );
 
-		it( 'should remove specified node', () => {
-			batch.remove( div );
+		describe( 'remove from document', () => {
+			let root;
 
-			expect( root.maxOffset ).to.equal( 1 );
-			expect( root.childCount ).to.equal( 1 );
-			expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
-		} );
+			beforeEach( () => {
+				root = doc.createRoot();
+				batch.append( div, root );
+				batch.append( p, root );
 
-		it( 'should remove any range of nodes', () => {
-			batch.remove( range );
+				// Reset batch.
+				batch = doc.batch();
 
-			expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
-			expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
-		} );
+				// Range starts in ROOT > DIV > P > gg|gg.
+				// Range ends in ROOT > DIV > ...|ar.
+				range = new Range( new Position( root, [ 0, 0, 2 ] ), new Position( root, [ 0, 5 ] ) );
+			} );
 
-		it( 'should create minimal number of remove deltas, each with only one operation', () => {
-			batch.remove( range );
+			it( 'should remove specified node', () => {
+				batch.remove( div );
 
-			expect( batch.deltas.length ).to.equal( 2 );
-			expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
-			expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
-		} );
+				expect( root.maxOffset ).to.equal( 1 );
+				expect( root.childCount ).to.equal( 1 );
+				expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
+			} );
 
-		it( 'should be chainable', () => {
-			chain = batch.remove( range );
+			it( 'should remove specified text node', () => {
+				batch.remove( p.getChild( 0 ) );
 
-			expect( chain ).to.equal( batch );
+				expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
+			} );
+
+			it( 'should remove any range of nodes', () => {
+				batch.remove( range );
+
+				expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
+				expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
+			} );
+
+			it( 'should create minimal number of remove deltas, each with only one operation', () => {
+				batch.remove( range );
+
+				expect( batch.deltas.length ).to.equal( 2 );
+				expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
+				expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
+			} );
+
+			it( 'should add delta to batch and operation to delta before applying operation', () => {
+				sinon.spy( doc, 'applyOperation' );
+				batch.remove( div );
+
+				const correctDeltaMatcher = sinon.match( operation => {
+					return operation.delta && operation.delta.batch && operation.delta.batch == batch;
+				} );
+
+				expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
+			} );
+
+			it( 'should use RemoveOperation', () => {
+				batch.remove( div );
+
+				expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'remove' );
+			} );
 		} );
 
-		it( 'should add delta to batch and operation to delta before applying operation', () => {
-			sinon.spy( doc, 'applyOperation' );
-			batch.remove( div );
+		describe( 'remove from document fragment', () => {
+			let frag;
 
-			const correctDeltaMatcher = sinon.match( operation => {
-				return operation.delta && operation.delta.batch && operation.delta.batch == batch;
+			beforeEach( () => {
+				frag = batch.createDocumentFragment();
+				batch.append( div, frag );
+				batch.append( p, frag );
+
+				// Reset batch.
+				batch = doc.batch();
+
+				// Range starts in FRAG > DIV > P > gg|gg.
+				// Range ends in FRAG > DIV > ...|ar.
+				range = new Range( new Position( frag, [ 0, 0, 2 ] ), new Position( frag, [ 0, 5 ] ) );
 			} );
 
-			expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
+			it( 'should remove specified node', () => {
+				batch.remove( div );
+
+				expect( frag.maxOffset ).to.equal( 1 );
+				expect( frag.childCount ).to.equal( 1 );
+				expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
+			} );
+
+			it( 'should remove specified text node', () => {
+				batch.remove( p.getChild( 0 ) );
+
+				expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
+			} );
+
+			it( 'should remove any range of nodes', () => {
+				batch.remove( range );
+
+				expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
+				expect( getNodesAndText( Range.createIn( frag.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
+			} );
+
+			it( 'should create minimal number of remove deltas, each with only one operation', () => {
+				batch.remove( range );
+
+				expect( batch.deltas.length ).to.equal( 2 );
+				expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
+				expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
+			} );
+
+			it( 'should add delta to batch and operation to delta before applying operation', () => {
+				sinon.spy( doc, 'applyOperation' );
+				batch.remove( div );
+
+				const correctDeltaMatcher = sinon.match( operation => {
+					return operation.delta && operation.delta.batch && operation.delta.batch == batch;
+				} );
+
+				expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
+			} );
+
+			it( 'should use DetachOperation', () => {
+				batch.remove( div );
+
+				expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'detach' );
+			} );
 		} );
 	} );