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

Changed: `model.Document#event:change` should have a batch as a param.
Tests: Added missing `model.Document#event:change` tests.

Szymon Cofalik 8 лет назад
Родитель
Сommit
3ec5d6d2aa

+ 1 - 1
packages/ckeditor5-engine/src/model/document.js

@@ -152,7 +152,7 @@ export default class Document {
 			if ( !this.differ.isEmpty || hasSelectionChanged ) {
 			if ( !this.differ.isEmpty || hasSelectionChanged ) {
 				this._callPostFixers( writer );
 				this._callPostFixers( writer );
 
 
-				this.fire( 'change' );
+				this.fire( 'change', writer.batch );
 
 
 				this.differ.reset();
 				this.differ.reset();
 				hasSelectionChanged = false;
 				hasSelectionChanged = false;

+ 67 - 0
packages/ckeditor5-engine/tests/model/document.js

@@ -6,6 +6,7 @@
 import Model from '../../src/model/model';
 import Model from '../../src/model/model';
 import Document from '../../src/model/document';
 import Document from '../../src/model/document';
 import RootElement from '../../src/model/rootelement';
 import RootElement from '../../src/model/rootelement';
+import Text from '../../src/model/text';
 import Batch from '../../src/model/batch';
 import Batch from '../../src/model/batch';
 import Delta from '../../src/model/delta/delta';
 import Delta from '../../src/model/delta/delta';
 import Range from '../../src/model/range';
 import Range from '../../src/model/range';
@@ -535,6 +536,72 @@ describe( 'Document', () => {
 		} );
 		} );
 	} );
 	} );
 
 
+	describe( 'event change', () => {
+		it( 'should be fired if there was a change in a document tree in a change block and have a batch as a param', () => {
+			doc.createRoot();
+			const spy = sinon.spy();
+
+			doc.on( 'change', ( evt, batch ) => {
+				spy();
+				expect( batch ).to.be.instanceof( Batch );
+			} );
+
+			model.change( writer => {
+				writer.insertText( 'foo', doc.getRoot(), 0 );
+			} );
+
+			expect( spy.calledOnce ).to.be.true;
+		} );
+
+		it( 'should be fired if there was a change in a document tree in a change block and have a batch as param', () => {
+			doc.createRoot();
+			const spy = sinon.spy();
+
+			doc.on( 'change', ( evt, batch ) => {
+				spy();
+				expect( batch ).to.be.instanceof( Batch );
+			} );
+
+			model.enqueueChange( writer => {
+				writer.insertText( 'foo', doc.getRoot(), 0 );
+			} );
+
+			expect( spy.calledOnce ).to.be.true;
+		} );
+
+		it( 'should be fired if there was a selection change in an (enqueue)change block', () => {
+			doc.createRoot();
+			const spy = sinon.spy();
+
+			const root = doc.getRoot();
+			root.appendChildren( new Text( 'foo' ) );
+
+			doc.on( 'change', spy );
+
+			model.change( () => {
+				doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 2, root, 2 ) ] );
+			} );
+
+			expect( spy.calledOnce ).to.be.true;
+		} );
+
+		it( 'should not be fired if writer was used on non-document tree', () => {
+			const spy = sinon.spy();
+
+			doc.on( 'change', ( evt, batch ) => {
+				spy();
+				expect( batch ).to.be.instanceof( Batch );
+			} );
+
+			model.change( writer => {
+				const docFrag = writer.createDocumentFragment();
+				writer.insertText( 'foo', docFrag, 0 );
+			} );
+
+			expect( spy.calledOnce ).to.be.false;
+		} );
+	} );
+
 	it( 'should be correctly converted to json', () => {
 	it( 'should be correctly converted to json', () => {
 		const serialized = jsonParseStringify( doc );
 		const serialized = jsonParseStringify( doc );