8
0
Эх сурвалжийг харах

Fixed: UndoCommand crashed when delta to undo was not in history anymore.

Szymon Cofalik 9 жил өмнө
parent
commit
ca814ff6b2

+ 6 - 0
packages/ckeditor5-undo/src/undocommand.js

@@ -94,6 +94,12 @@ export default class UndoCommand extends BaseCommand {
 			// To prevent errors, we will take an updated version of it from the history, basing on delta's `baseVersion`.
 			const updatedDeltaToUndo = history.getDelta( baseVersion );
 
+			// This is a safe valve in case of not finding delta to undo in history. This may come up if that delta
+			// got updated into no deltas, or removed from history.
+			if ( updatedDeltaToUndo === null ) {
+				continue;
+			}
+
 			// 2. Reverse delta from the history.
 			updatedDeltaToUndo.reverse();
 			let reversedDelta = [];

+ 21 - 0
packages/ckeditor5-undo/tests/undocommand.js

@@ -341,5 +341,26 @@ describe( 'UndoCommand', () => {
 			expect( getText( root ) ).to.equal( 'adbcef' );
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 1, 4 ) ) ).to.be.true;
 		} );
+
+		it( 'does nothing (and not crashes) if delta to undo is no longer in history', () => {
+			// Also an edgy situation but it may come up if other plugins use `CompressedHistory` API.
+			root.appendChildren( 'abcdef' );
+			expect( getText( root ) ).to.equal( 'abcdef' );
+
+			editor.document.selection.setRanges( [ r( 0, 1 ) ] );
+			let batch0 = doc.batch();
+			undo.addBatch( batch0 );
+			batch0.setAttr( 'uppercase', true, r( 0, 1 ) );
+			expect( getText( root ) ).to.equal( 'Abcdef' );
+
+			doc.history.removeDelta( 0 );
+			root.getChild( 0 ).removeAttribute( 'uppercase' );
+			expect( getText( root ) ).to.equal( 'abcdef' );
+
+			undo._execute();
+
+			// Nothing happened. We are still alive.
+			expect( getText( root ) ).to.equal( 'abcdef' );
+		} );
 	} );
 } );