Parcourir la source

Fix textwatcher code after CR

panr il y a 5 ans
Parent
commit
3114a5bf8a

+ 13 - 7
packages/ckeditor5-typing/src/textwatcher.js

@@ -30,6 +30,8 @@ export default class TextWatcher {
 	 */
 	constructor( model, testCallback ) {
 		/**
+		 * The editor's model.
+		 *
 		 * @readonly
 		 * @member {module:engine/model/model~Model}
 		 */
@@ -45,6 +47,7 @@ export default class TextWatcher {
 		 * * an object if there is a match and we want to pass some additional information to the {@link #matched:data} event.
 		 *
 		 * @member {Function} #testCallback
+		 * @returns {Object} testResult
 		 */
 		this.testCallback = testCallback;
 
@@ -153,6 +156,7 @@ export default class TextWatcher {
 		if ( testResult ) {
 			const eventData = Object.assign( data, { text, range } );
 
+			// If the test callback returns an object with additional data, assign the data as well.
 			if ( typeof testResult == 'object' ) {
 				Object.assign( eventData, testResult );
 			}
@@ -165,19 +169,13 @@ export default class TextWatcher {
 mix( TextWatcher, ObservableMixin );
 
 /**
- * Fired whenever the text does not match anymore. Fired only when the text watcher found a match.
- *
- * @event unmatched
- */
-
-/**
  * Fired whenever the text watcher found a match for data changes.
  *
  * @event matched:data
  * @param {Object} data Event data.
  * @param {String} data.text The full text before selection to which the regexp was applied.
  * @param {module:engine/model/range~Range} data.range The range representing the position of the `data.text`.
- * @param {module:engine/model/batch~Batch} data.batch A batch associated with a change.
+ * @param {Object} data.testResult [Optional] The additional data returned from the {module:typing/textwatcher~TextWatcher#testCallback}.
  */
 
 /**
@@ -186,4 +184,12 @@ mix( TextWatcher, ObservableMixin );
  * @event matched:selection
  * @param {Object} data Event data.
  * @param {String} data.text The full text before selection.
+ * @param {module:engine/model/range~Range} data.range The range representing the position of the `data.text`.
+ * @param {Object} data.testResult [Optional] The additional data returned from the {module:typing/textwatcher~TextWatcher#testCallback}.
+ */
+
+/**
+ * Fired whenever the text does not match anymore. Fired only when the text watcher found a match.
+ *
+ * @event unmatched
  */

+ 13 - 11
packages/ckeditor5-typing/tests/textwatcher.js

@@ -141,9 +141,9 @@ describe( 'TextWatcher', () => {
 	} );
 
 	describe( 'events', () => {
-		describe( '"matched:data" should be fired when test callback returns true for model data changes', () => {
-			it( 'without additional data', () => {
-				testCallbackStub.returns( { match: true } );
+		describe( '"matched:data"', () => {
+			it( 'should be fired when test callback returns true for model data changes', () => {
+				testCallbackStub.returns( true );
 
 				model.change( writer => {
 					writer.insertText( '@', doc.selection.getFirstPosition() );
@@ -155,9 +155,10 @@ describe( 'TextWatcher', () => {
 				sinon.assert.notCalled( unmatchedSpy );
 			} );
 
-			it( 'with additional data', () => {
+			it( 'should be fired with additional data when test callback returns true for model data changes', () => {
 				const additionalData = { abc: 'xyz' };
-				testCallbackStub.returns( { match: true, data: additionalData } );
+
+				testCallbackStub.returns( additionalData );
 
 				model.change( writer => {
 					writer.insertText( '@', doc.selection.getFirstPosition() );
@@ -165,6 +166,7 @@ describe( 'TextWatcher', () => {
 
 				sinon.assert.calledOnce( testCallbackStub );
 				sinon.assert.calledOnce( matchedDataSpy );
+				sinon.assert.notCalled( matchedSelectionSpy );
 				sinon.assert.notCalled( unmatchedSpy );
 
 				expect( matchedDataSpy.firstCall.args[ 1 ] ).to.deep.include( additionalData );
@@ -175,7 +177,7 @@ describe( 'TextWatcher', () => {
 		' (even when test callback returns true for model data changes)', () => {
 			watcher.isEnabled = false;
 
-			testCallbackStub.returns( { match: true } );
+			testCallbackStub.returns( true );
 
 			model.change( writer => {
 				writer.insertText( '@', doc.selection.getFirstPosition() );
@@ -188,7 +190,7 @@ describe( 'TextWatcher', () => {
 		} );
 
 		it( 'should fire "matched:selection" event when test callback returns true for model data changes', () => {
-			testCallbackStub.returns( { match: true } );
+			testCallbackStub.returns( true );
 
 			model.enqueueChange( 'transparent', writer => {
 				writer.insertText( '@', doc.selection.getFirstPosition() );
@@ -208,7 +210,7 @@ describe( 'TextWatcher', () => {
 		' (even when test callback returns true for model data changes)', () => {
 			watcher.isEnabled = false;
 
-			testCallbackStub.returns( { match: true } );
+			testCallbackStub.returns( true );
 
 			model.enqueueChange( 'transparent', writer => {
 				writer.insertText( '@', doc.selection.getFirstPosition() );
@@ -225,7 +227,7 @@ describe( 'TextWatcher', () => {
 		} );
 
 		it( 'should not fire "matched" event when test callback returns false', () => {
-			testCallbackStub.returns( { match: false } );
+			testCallbackStub.returns( false );
 
 			model.change( writer => {
 				writer.insertText( '@', doc.selection.getFirstPosition() );
@@ -238,7 +240,7 @@ describe( 'TextWatcher', () => {
 		} );
 
 		it( 'should fire "unmatched" event when test callback returns false when it was previously matched', () => {
-			testCallbackStub.returns( { match: true } );
+			testCallbackStub.returns( true );
 
 			model.change( writer => {
 				writer.insertText( '@', doc.selection.getFirstPosition() );
@@ -260,7 +262,7 @@ describe( 'TextWatcher', () => {
 		} );
 
 		it( 'should fire "umatched" event when selection is expanded', () => {
-			testCallbackStub.returns( { match: true } );
+			testCallbackStub.returns( true );
 
 			model.change( writer => {
 				writer.insertText( '@', doc.selection.getFirstPosition() );