8
0
Pārlūkot izejas kodu

Changed: Minor changes in `model.Document`.

Szymon Cofalik 8 gadi atpakaļ
vecāks
revīzija
8eb6b2ac3e

+ 11 - 6
packages/ckeditor5-engine/src/model/document.js

@@ -96,8 +96,10 @@ export default class Document {
 					 * @error document-selection-wrong-position
 					 * @param {module:engine/model/range~Range} range
 					 */
-					throw new CKEditorError( 'document-selection-wrong-position: ' +
-						'Range from document selection starts or ends at incorrect position.', { range } );
+					throw new CKEditorError(
+						'document-selection-wrong-position: Range from document selection starts or ends at incorrect position.',
+						{ range }
+					);
 				}
 			}
 		} );
@@ -117,9 +119,12 @@ export default class Document {
 				 */
 				throw new CKEditorError(
 					'model-document-applyOperation-wrong-version: Only operations with matching versions can be applied.',
-					{ operation } );
+					{ operation }
+				);
 			}
-		}, { priority: 'high' } );
+
+			operation._validate();
+		}, { priority: 'highest' } );
 
 		this.listenTo( model, 'applyOperation', ( evt, args ) => {
 			const operation = args[ 0 ];
@@ -363,7 +368,7 @@ export default class Document {
 	/**
 	 * Fired when document changes by applying an operation.
 	 *
-	 * There are a few types of change:
+	 * There are several types of change:
 	 *
 	 * * 'insert' when nodes are inserted,
 	 * * 'remove' when nodes are removed,
@@ -379,7 +384,7 @@ export default class Document {
 	 * * 'changeRootAttribute' when attribute for root changes.
 	 *
 	 * @event change
-	 * @param {String} type Change type, possible option: 'insert', 'remove', 'reinsert', 'move', 'attribute'.
+	 * @param {String} type Change type.
 	 * @param {Object} data Additional information about the change.
 	 * @param {module:engine/model/range~Range} [data.range] Range in model containing changed nodes. Note that the range state is
 	 * after changes has been done, i.e. for 'remove' the range will be in the {@link #graveyard graveyard root}.

+ 29 - 30
packages/ckeditor5-engine/tests/model/document/document.js

@@ -42,24 +42,29 @@ describe( 'Document', () => {
 	} );
 
 	describe( 'model#applyOperation listener', () => {
-		it( 'should increase document version, execute operation and fire event with proper data ' +
-			'when operation is a document operation', () => {
-			const changeCallback = sinon.spy();
-			const type = 't';
-			const data = { data: 'x' };
-			const batch = new Batch();
-			const delta = new Delta();
-			delta.type = 'type';
+		let operation, data, delta, batch;
 
-			const operation = {
-				type,
+		beforeEach( () => {
+			data = { data: 'x' };
+
+			operation = {
+				type: 't',
 				baseVersion: 0,
 				isDocumentOperation: true,
-				_execute: sinon.stub().returns( data )
+				_execute: sinon.stub().returns( data ),
+				_validate: () => {}
 			};
 
+			delta = new Delta();
 			delta.addOperation( operation );
+			delta.type = 'delta';
+
+			batch = new Batch();
 			batch.addDelta( delta );
+		} );
+
+		it( 'for document operation: should increase document version, execute operation and fire change event with proper data', () => {
+			const changeCallback = sinon.spy();
 
 			doc.on( 'change', changeCallback );
 			model.applyOperation( operation );
@@ -69,30 +74,15 @@ describe( 'Document', () => {
 			sinon.assert.calledOnce( operation._execute );
 
 			sinon.assert.calledOnce( changeCallback );
-			expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
+			expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( 't' );
 			expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
-			expect( changeCallback.args[ 0 ][ 3 ] ).to.deep.equal( batch );
+			expect( changeCallback.args[ 0 ][ 3 ] ).to.equal( batch );
 			expect( changeCallback.args[ 0 ][ 4 ] ).to.equal( delta.type );
 		} );
 
-		it( 'should execute operation, not fire event and not increase document version ' +
-			'when operation is not a document operation', () => {
+		it( 'for non-document operation: should only execute operation', () => {
 			const changeCallback = sinon.spy();
-			const type = 't';
-			const data = { data: 'x' };
-			const batch = new Batch();
-			const delta = new Delta();
-			delta.type = 'type';
-
-			const operation = {
-				type,
-				baseVersion: 0,
-				isDocumentOperation: false,
-				_execute: sinon.stub().returns( data )
-			};
-
-			delta.addOperation( operation );
-			batch.addDelta( delta );
+			operation.isDocumentOperation = false;
 
 			doc.on( 'change', changeCallback );
 			model.applyOperation( operation );
@@ -104,6 +94,15 @@ describe( 'Document', () => {
 			sinon.assert.notCalled( changeCallback );
 		} );
 
+		it( 'should do nothing if operation event was cancelled', () => {
+			model.on( 'applyOperation', evt => evt.stop(), { priority: 'highest' } );
+
+			model.applyOperation( operation );
+
+			expect( doc.version ).to.equal( 0 );
+			expect( operation._execute.called ).to.be.false;
+		} );
+
 		it( 'should throw an error on the operation base version and the document version is different', () => {
 			const operation = {
 				baseVersion: 1,