8
0
Просмотр исходного кода

Throw when RemoveOperation is executed on detached item.

Oskar Wróbel 8 лет назад
Родитель
Сommit
9e141a94af

+ 19 - 0
packages/ckeditor5-engine/src/model/operation/removeoperation.js

@@ -9,6 +9,7 @@
 
 import MoveOperation from './moveoperation';
 import ReinsertOperation from './reinsertoperation';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 /**
  * Operation to remove a range of nodes.
@@ -42,6 +43,24 @@ export default class RemoveOperation extends MoveOperation {
 		return new ReinsertOperation( this.getMovedRangeStart(), this.howMany, newTargetPosition, this.baseVersion + 1 );
 	}
 
+	/**
+	 * @inheritDoc
+	 */
+	_execute() {
+		if ( !this.sourcePosition.root.document ) {
+			/**
+			 * Item that is going to be removed needs to be a {@link module:engine/model/document~Document document} child.
+			 * To remove Item from detached document fragment use
+			 * {@link module:engine/model/operation/detachoperation~DetachOperation DetachOperation}.
+			 *
+			 * @error remove-operation-on-detached-item
+			 */
+			throw new CKEditorError( 'remove-operation-on-detached-item: Cannot remove detached item.' );
+		}
+
+		return super._execute();
+	}
+
 	/**
 	 * @inheritDoc
 	 */

+ 20 - 0
packages/ckeditor5-engine/tests/model/operation/removeoperation.js

@@ -10,6 +10,7 @@ import MoveOperation from '../../../src/model/operation/moveoperation';
 import Position from '../../../src/model/position';
 import Text from '../../../src/model/text';
 import Element from '../../../src/model/element';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import { jsonParseStringify, wrapInDelta } from '../../../tests/model/_utils/utils';
 
 describe( 'RemoveOperation', () => {
@@ -140,6 +141,25 @@ describe( 'RemoveOperation', () => {
 		expect( doc.graveyard.getChild( 2 ).name ).to.equal( 'y' );
 	} );
 
+	it( 'should throw when is executed on detached item', () => {
+		const batch = doc.batch();
+		const docFrag = batch.createDocumentFragment();
+		const item = batch.createElement( 'foo' );
+
+		batch.append( item, docFrag );
+
+		const op = new RemoveOperation(
+			new Position( docFrag, [ 0 ] ),
+			1,
+			new Position( doc.graveyard, [ 0 ] ),
+			doc.version
+		);
+
+		expect( () => {
+			op._execute();
+		} ).to.throw( CKEditorError, /^remove-operation-on-detached-item/ );
+	} );
+
 	it( 'should always be a document operation', () => {
 		const op = new RemoveOperation(
 			new Position( root, [ 2 ] ),