Bladeren bron

Align code to the removed matcher callback from text watcher.

Maciej Gołaszewski 6 jaren geleden
bovenliggende
commit
a001603106

+ 22 - 22
packages/ckeditor5-typing/src/texttransformation.js

@@ -13,23 +13,23 @@ import TextWatcher from '@ckeditor/ckeditor5-utils/src/textwatcher';
 // Set of default transformations provided by the feature.
 const DEFAULT_TRANSFORMATIONS = [
 	// Common symbols:
-	{ from: '\\(c\\)', to: '©' },
-	{ from: '\\(tm\\)', to: '™' },
+	{ from: '(c)', to: '©' },
+	{ from: '(tm)', to: '™' },
 
 	// Mathematical:
 	{ from: '1/2', to: '½' },
 	{ from: '<=', to: '≤' },
 
 	// Typography:
-	{ from: '\\.\\.\\.', to: '…' },
+	{ from: '...', to: '…' },
 	{ from: ' -- ', to: ' – ' },
 	{ from: ' --- ', to: ' — ' },
 
 	// Quotations:
 	// English primary.
-	{ from: buildQuotesPattern( '"' ), to: '“$1”' },
+	{ from: buildQuotesRegExp( '"' ), to: '“$1”' },
 	// English secondary.
-	{ from: buildQuotesPattern( '\'' ), to: '‘$1’' }
+	{ from: buildQuotesRegExp( '\'' ), to: '‘$1’' }
 ];
 
 /**
@@ -70,25 +70,24 @@ export default class TextTransformation extends Plugin {
 		for ( const transformation of transformations ) {
 			const { from, to } = transformation;
 
-			const regExp = from instanceof RegExp ? from : new RegExp( `${ from }$`, 'u' );
+			let testCallback;
+			let textReplacer;
 
-			// setup text watcher
-			const watcher = new TextWatcher( editor.model, text => regExp.test( text ), text => {
-				const match = text.match( regExp );
+			if ( from instanceof RegExp ) {
+				testCallback = text => from.test( text );
+				textReplacer = message => message.replace( from, to );
+			} else {
+				testCallback = text => text.endsWith( from );
+				textReplacer = message => message.slice( 0, message.length - from.length ) + to;
+			}
 
-				return {
-					text: match[ 0 ],
-					groups: match
-				};
-			} );
+			const watcher = new TextWatcher( editor.model, testCallback );
 
 			watcher.on( 'matched', ( evt, data ) => {
 				const selection = editor.model.document.selection;
 				const focus = selection.focus;
-				const message = data.matched.text;
-				const textToReplaceLength = message.length;
-
-				const textToInsert = message.replace( regExp, to );
+				const textToReplaceLength = data.text.length;
+				const textToInsert = textReplacer( data.text );
 
 				model.enqueueChange( model.createBatch(), writer => {
 					const replaceRange = writer.createRange( focus.getShiftedBy( -textToReplaceLength ), focus );
@@ -104,8 +103,8 @@ export default class TextTransformation extends Plugin {
 //
 // @param {String} quoteCharacter a character to creat a pattern for.
 // @returns {String}
-function buildQuotesPattern( quoteCharacter ) {
-	return `${ quoteCharacter }([^${ quoteCharacter }]+)${ quoteCharacter }$`;
+function buildQuotesRegExp( quoteCharacter ) {
+	return new RegExp( `${ quoteCharacter }([^${ quoteCharacter }]+)${ quoteCharacter }$` );
 }
 
 /**
@@ -119,10 +118,11 @@ function buildQuotesPattern( quoteCharacter ) {
  *			{ from: /([a-z-])@(example.com)$/i, to: '$1.at.$2' }
  *		]
  *
- * *Note*: When passing a regex pattern as a string the end of string token (`$`) is always inserted.
+ * *Note*: The text watcher always evaluates end of an input - a text before a caret in single node. If passing a RegExp object you must
+ * include `$` token to match end of string.
  *
  * @typedef {Object} module:typing/texttransformation~TextTransformationDescription
- * @property {String|RegExp} from The pattern to transform.
+ * @property {String|RegExp} from The string or RegExp to transform.
  * @property {String} to The text to transform compatible with `String.replace()`
  */
 

+ 7 - 7
packages/ckeditor5-typing/tests/texttransformation.js

@@ -43,7 +43,7 @@ describe( 'Text transformation feature', () => {
 
 		describe( 'symbols', () => {
 			testTransformation( '(c)', '©' );
-			// TODO: skip because of CI: testTransformation( '(tm)', '™' );
+			testTransformation( '(tm)', '™' );
 		} );
 
 		describe( 'mathematical', () => {
@@ -73,7 +73,7 @@ describe( 'Text transformation feature', () => {
 
 		function testTransformation( transformFrom, transformTo ) {
 			it( `should transform "${ transformFrom }" to "${ transformTo }"`, () => {
-				setData( model, '<paragraph>[]</paragraph>' );
+				setData( model, '<paragraph>A foo[]</paragraph>' );
 
 				const letters = transformFrom.split( '' );
 
@@ -83,7 +83,7 @@ describe( 'Text transformation feature', () => {
 					} );
 				}
 
-				expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>${ transformTo }</paragraph>` );
+				expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>A foo${ transformTo }</paragraph>` );
 			} );
 
 			it( `should not transform "${ transformFrom }" to "${ transformTo }" inside text`, () => {
@@ -110,8 +110,8 @@ describe( 'Text transformation feature', () => {
 				textTransformation: {
 					transformations: [
 						{
-							from: '([a-z]+)@(example.com)$',
-							to: '$1.at.$2'
+							from: 'CKS',
+							to: 'CKSource'
 						}
 					]
 				}
@@ -119,10 +119,10 @@ describe( 'Text transformation feature', () => {
 				setData( model, '<paragraph>[]</paragraph>' );
 
 				model.enqueueChange( model.createBatch(), writer => {
-					writer.insertText( 'user@example.com', doc.selection.focus );
+					writer.insertText( 'CKS', doc.selection.focus );
 				} );
 
-				expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>user.at.example.com</paragraph>' );
+				expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKSource</paragraph>' );
 			} );
 		} );