瀏覽代碼

Refactor and add missing tests

panr 5 年之前
父節點
當前提交
b1ab9ec9dc

+ 3 - 1
packages/ckeditor5-typing/src/texttransformation.js

@@ -136,7 +136,9 @@ export default class TextTransformation extends Plugin {
 				if ( match ) {
 					return {
 						match,
-						normalizedTransformation
+						data: {
+							normalizedTransformation
+						}
 					};
 				}
 			}

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

@@ -119,9 +119,9 @@ export default class TextWatcher {
 
 		const { text, range } = getLastTextLine( rangeBeforeSelection, model );
 
-		const textHasMatch = this.testCallback( text );
+		const textMatcher = this.testCallback( text );
 
-		if ( !textHasMatch && this.hasMatch ) {
+		if ( !textMatcher && this.hasMatch ) {
 			/**
 			 * Fired whenever the text does not match anymore. Fired only when the text watcher found a match.
 			 *
@@ -130,13 +130,12 @@ export default class TextWatcher {
 			this.fire( 'unmatched' );
 		}
 
-		this.hasMatch = textHasMatch;
+		this.hasMatch = textMatcher && textMatcher.match;
 
-		if ( textHasMatch ) {
-			// If text matches, get the normalized transformation from the match and then pass it to the
-			// event data object.
-			const normalizedTransformation = textHasMatch.normalizedTransformation;
-			const eventData = Object.assign( data, { normalizedTransformation, text, range } );
+		if ( this.hasMatch ) {
+			// If text matches and testCallback() returns additional data, pass the data to the eventData object.
+			const additionalData = textMatcher.data;
+			const eventData = Object.assign( data, { text, range, ...additionalData } );
 
 			/**
 			 * Fired whenever the text watcher found a match for data changes.

+ 32 - 15
packages/ckeditor5-typing/tests/textwatcher.js

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