浏览代码

Changed: if no nodes are added by `Batch.insert`, do not create operation and delta.

Szymon Cofalik 8 年之前
父节点
当前提交
aa39e71b94

+ 11 - 3
packages/ckeditor5-engine/src/model/delta/insertdelta.js

@@ -8,10 +8,11 @@
  */
 
 import Delta from './delta';
-import DeltaFactory from './deltafactory';
 import RemoveDelta from './removedelta';
-import { register } from '../batch';
+import DeltaFactory from './deltafactory';
 import InsertOperation from '../operation/insertoperation';
+import { register } from '../batch';
+import { normalizeNodes } from './../writer';
 
 import DocumentFragment from '../documentfragment';
 import Range from '../../model/range.js';
@@ -95,8 +96,15 @@ export default class InsertDelta extends Delta {
  * @param {module:engine/model/node~NodeSet} nodes The list of nodes to be inserted.
  */
 register( 'insert', function( position, nodes ) {
+	const normalizedNodes = normalizeNodes( nodes );
+
+	// If nothing is inserted do not create delta and operation.
+	if ( normalizedNodes.length === 0 ) {
+		return this;
+	}
+
 	const delta = new InsertDelta();
-	const insert = new InsertOperation( position, nodes, this.document.version );
+	const insert = new InsertOperation( position, normalizedNodes, this.document.version );
 
 	this.addDelta( delta );
 	delta.addOperation( insert );

+ 11 - 0
packages/ckeditor5-engine/tests/model/delta/insertdelta.js

@@ -87,6 +87,17 @@ describe( 'Batch', () => {
 			expect( doc.applyOperation.secondCall.calledWith( sinon.match( ( operation ) => operation instanceof MarkerOperation ) ) );
 			expect( doc.applyOperation.thirdCall.calledWith( sinon.match( ( operation ) => operation instanceof MarkerOperation ) ) );
 		} );
+
+		it( 'should not create a delta and an operation if no nodes were inserted', () => {
+			sinon.spy( doc, 'applyOperation' );
+
+			batch = doc.batch();
+
+			batch.insert( new Position( root, [ 0 ] ), [] );
+
+			expect( batch.deltas.length ).to.equal( 0 );
+			expect( doc.applyOperation.called ).to.be.false;
+		} );
 	} );
 } );