Procházet zdrojové kódy

Append $ token to string pattern for text transformation definition.

Maciej Gołaszewski před 6 roky
rodič
revize
d328a4a2c8

+ 15 - 13
packages/ckeditor5-typing/src/texttransformation.js

@@ -26,17 +26,16 @@ export default class TextTransformation extends Plugin {
 
 		editor.config.define( 'textTransformation', {
 			transformations: [
-				// TODO: not nice definition - needs RegExp escaping.
-				{ from: '\\(c\\)$', to: '©' },
-				{ from: '\\(tm\\)$', to: '™' },
-				{ from: '\\.\\.\\.$', to: '…' },
-				{ from: '1/2$', to: '½' },
-				{ from: '<=$', to: '≤' },
+				{ from: '\\(c\\)', to: '©' },
+				{ from: '\\(tm\\)', to: '™' },
+				{ from: '\\.\\.\\.', to: '…' },
+				{ from: '1/2', to: '½' },
+				{ from: '<=', to: '≤' },
 
 				// TODO: not nice - needs special order.
 				// TODO: not nice - needs spaces.
-				{ from: ' --- $', to: ' — ' },
-				{ from: ' -- $', to: ' – ' },
+				{ from: ' --- ', to: ' — ' },
+				{ from: ' -- ', to: ' – ' },
 
 				// English quotation - primary.
 				{ from: buildQuotesPattern( '"' ), to: '“$1”' },
@@ -60,8 +59,9 @@ export default class TextTransformation extends Plugin {
 		const transformations = editor.config.get( 'textTransformation.transformations' );
 
 		for ( const transformation of transformations ) {
-			// TODO: always add $ to the string regexp!
-			const regExp = new RegExp( transformation.from, 'u' );
+			const { from, to } = transformation;
+
+			const regExp = from instanceof RegExp ? from : new RegExp( `${ from }$`, 'u' );
 
 			// setup text watcher
 			const watcher = new TextWatcher( editor, text => regExp.test( text ), text => {
@@ -79,7 +79,7 @@ export default class TextTransformation extends Plugin {
 				const message = data.matched.text;
 				const textToReplaceLength = message.length;
 
-				const textToInsert = message.replace( regExp, transformation.to );
+				const textToInsert = message.replace( regExp, to );
 
 				model.enqueueChange( model.createBatch(), writer => {
 					const replaceRange = writer.createRange( focus.getShiftedBy( -textToReplaceLength ), focus );
@@ -106,10 +106,12 @@ function buildQuotesPattern( quoteCharacter ) {
  *			// Will replace foo text before caret (ie typed) by user to bar:
  *			{ from: 'foo', to: 'bar' },
  *
- *			// Will remove @ from emails on example.com domain, ie from user@example.com -> user.at.example.com
- *			{ from: /([a-z-])@(example.com)$/, to: '$1.at.$2' }
+ *			// Will remove @ from emails on example.com domain, ie from user@example.com -> user.at.example.com:
+ *			{ 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.
+ *
  * @typedef {Object} module:typing/texttransformation~TextTransformationDescription
  * @property {String|RegExp} from The pattern to transform.
  * @property {String} to The text to transform compatible with `String.replace()`

+ 16 - 0
packages/ckeditor5-typing/tests/texttransformation.js

@@ -84,6 +84,22 @@ describe( 'Text transformation feature', () => {
 
 				expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>${ transformTo }</paragraph>` );
 			} );
+
+			it( `should not transform "${ transformFrom }" to "${ transformTo }" inside text`, () => {
+				setData( model, '<paragraph>[]</paragraph>' );
+
+				// Insert text - should not be transformed.
+				model.enqueueChange( model.createBatch(), writer => {
+					writer.insertText( `foo ${ transformFrom } bar`, doc.selection.focus );
+				} );
+
+				// Enforce text watcher check after insertion.
+				model.enqueueChange( model.createBatch(), writer => {
+					writer.insertText( ' ', doc.selection.focus );
+				} );
+
+				expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>foo ${ transformFrom } bar </paragraph>` );
+			} );
 		}
 	} );