Browse Source

Fix: `Model#deleteContent` will not throw if the passed selection is in the graveyard root.

Szymon Cofalik 6 years ago
parent
commit
a8b54e1bd4

+ 7 - 1
packages/ckeditor5-engine/src/model/utils/deletecontent.js

@@ -63,6 +63,13 @@ export default function deleteContent( model, selection, options = {} ) {
 	const schema = model.schema;
 
 	model.change( writer => {
+		const selRange = selection.getFirstRange();
+
+		// If the selection is already removed, don't do anything.
+		if ( selRange.root.rootName == '$graveyard' ) {
+			return;
+		}
+
 		// 1. Replace the entire content with paragraph.
 		// See: https://github.com/ckeditor/ckeditor5-engine/issues/1012#issuecomment-315017594.
 		if ( !options.doNotResetEntireContent && shouldEntireContentBeReplacedWithParagraph( schema, selection ) ) {
@@ -71,7 +78,6 @@ export default function deleteContent( model, selection, options = {} ) {
 			return;
 		}
 
-		const selRange = selection.getFirstRange();
 		const startPos = selRange.start;
 		const endPos = LivePosition.fromPosition( selRange.end, 'toNext' );
 

+ 23 - 0
packages/ckeditor5-engine/tests/model/utils/deletecontent.js

@@ -29,6 +29,29 @@ describe( 'DataController utils', () => {
 			} );
 		} );
 
+		it( 'should not do anything if the selection is already in graveyard', () => {
+			model = new Model();
+			doc = model.document;
+
+			const gy = model.document.graveyard;
+
+			gy._appendChild( new Element( 'paragraph' ) );
+
+			const baseVersion = model.document.baseVersion;
+
+			model.change( writer => {
+				sinon.spy( writer, 'remove' );
+
+				const selection = writer.createSelection( writer.createRangeIn( gy ) );
+
+				deleteContent( model, selection );
+
+				expect( writer.remove.called ).to.be.false;
+			} );
+
+			expect( model.document.baseVersion ).to.equal( baseVersion );
+		} );
+
 		describe( 'in simple scenarios', () => {
 			beforeEach( () => {
 				model = new Model();