浏览代码

Do not increase document version for non-document operations.

Oskar Wróbel 8 年之前
父节点
当前提交
4ea69cb273

+ 4 - 3
packages/ckeditor5-engine/src/model/document.js

@@ -163,9 +163,10 @@ export default class Document {
 
 
 		const changes = operation._execute();
 		const changes = operation._execute();
 
 
-		this.version++;
-
-		this.history.addDelta( operation.delta );
+		if ( operation.isDocumentOperation ) {
+			this.version++;
+			this.history.addDelta( operation.delta );
+		}
 
 
 		this.fire( 'change', operation.type, changes, operation.delta.batch, operation.delta.type );
 		this.fire( 'change', operation.type, changes, operation.delta.batch, operation.delta.type );
 	}
 	}

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

@@ -110,7 +110,8 @@ describe( 'Document', () => {
 	} );
 	} );
 
 
 	describe( 'applyOperation()', () => {
 	describe( 'applyOperation()', () => {
-		it( 'should increase document version, execute operation and fire event with proper data', () => {
+		it( 'should increase document version, execute operation and fire event with proper data ' +
+			'when operation is a document operation', () => {
 			const changeCallback = sinon.spy();
 			const changeCallback = sinon.spy();
 			const type = 't';
 			const type = 't';
 			const data = { data: 'x' };
 			const data = { data: 'x' };
@@ -121,6 +122,7 @@ describe( 'Document', () => {
 			const operation = {
 			const operation = {
 				type,
 				type,
 				baseVersion: 0,
 				baseVersion: 0,
+				isDocumentOperation: true,
 				_execute: sinon.stub().returns( data )
 				_execute: sinon.stub().returns( data )
 			};
 			};
 
 
@@ -131,6 +133,40 @@ describe( 'Document', () => {
 			doc.applyOperation( operation );
 			doc.applyOperation( operation );
 
 
 			expect( doc.version ).to.equal( 1 );
 			expect( doc.version ).to.equal( 1 );
+			expect( doc.history._deltas.length ).to.equal( 1 );
+			sinon.assert.calledOnce( operation._execute );
+
+			sinon.assert.calledOnce( changeCallback );
+			expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
+			expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
+			expect( changeCallback.args[ 0 ][ 3 ] ).to.deep.equal( batch );
+			expect( changeCallback.args[ 0 ][ 4 ] ).to.equal( delta.type );
+		} );
+
+		it( 'should execute operation, fire event with proper data and not increase document version ' +
+			'when operation is not a document operation', () => {
+			const changeCallback = sinon.spy();
+			const type = 't';
+			const data = { data: 'x' };
+			const batch = new Batch();
+			const delta = new Delta();
+			delta.type = 'type';
+
+			const operation = {
+				type,
+				baseVersion: 0,
+				isDocumentOperation: false,
+				_execute: sinon.stub().returns( data )
+			};
+
+			delta.addOperation( operation );
+			batch.addDelta( delta );
+
+			doc.on( 'change', changeCallback );
+			doc.applyOperation( operation );
+
+			expect( doc.version ).to.equal( 0 );
+			expect( doc.history._deltas.length ).to.equal( 0 );
 			sinon.assert.calledOnce( operation._execute );
 			sinon.assert.calledOnce( operation._execute );
 
 
 			sinon.assert.calledOnce( changeCallback );
 			sinon.assert.calledOnce( changeCallback );
@@ -149,7 +185,7 @@ describe( 'Document', () => {
 				() => {
 				() => {
 					doc.applyOperation( operation );
 					doc.applyOperation( operation );
 				}
 				}
-			).to.throw( CKEditorError, /model-document-applyOperation-wrong-version/ );
+			).to.throw( CKEditorError, /^model-document-applyOperation-wrong-version/ );
 		} );
 		} );
 	} );
 	} );