Przeglądaj źródła

Avoided unnecessary operation.

Piotrek Koszuliński 8 lat temu
rodzic
commit
6c0432b1e4

+ 6 - 1
packages/ckeditor5-engine/src/controller/deletecontent.js

@@ -104,7 +104,12 @@ function mergeBranches( batch, startPos, endPos ) {
 		// <a><b>x[]</b></a><c><d>[]y</d></c>
 		// becomes:
 		// <a><b>x</b>[]<d>y</d></a><c>[]</c>
-		batch.move( endParent, startPos );
+
+		// Move the end parent only if needed.
+		// E.g. not in this case: <p>ab</p>[]{}<p>cd</p>
+		if ( !endPos.isEqual( startPos ) ) {
+			batch.move( endParent, startPos );
+		}
 
 		// To then become:
 		// <a><b>xy</b>[]</a><c>[]</c>

+ 15 - 0
packages/ckeditor5-engine/tests/controller/deletecontent.js

@@ -255,6 +255,21 @@ describe( 'DataController', () => {
 				expect( spyRemove.called ).to.be.true;
 			} );
 
+			it( 'does not try to move the second block if not needed', () => {
+				setData( doc, '<paragraph>ab[cd</paragraph><paragraph>ef]gh</paragraph>' );
+
+				const batch = doc.batch();
+				const spyMerge = sinon.spy( batch, 'merge' );
+				const spyMove = sinon.spy( batch, 'move' );
+
+				deleteContent( doc.selection, batch, { merge: true } );
+
+				expect( getData( doc ) ).to.equal( '<paragraph>ab[]gh</paragraph>' );
+
+				expect( spyMove.called ).to.be.false;
+				expect( spyMerge.called ).to.be.true;
+			} );
+
 			// Note: in all these cases we ignore the direction of merge.
 			// If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
 			// forward and backward delete.