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

Add support for english primary and secondary quotation marks.

Fix regexp.
Maciej Gołaszewski 6 лет назад
Родитель
Сommit
863365bc92

+ 16 - 5
packages/ckeditor5-typing/src/texttransformation.js

@@ -36,17 +36,20 @@ export default class TextTransformation extends Plugin {
 			{ in: '\\.\\.\\.$', out: '…' },
 			{ in: '\\.\\.\\.$', out: '…' },
 			{ in: '1/2', out: '½' },
 			{ in: '1/2', out: '½' },
 			{ in: '<=', out: '≤' },
 			{ in: '<=', out: '≤' },
+
 			// TODO: not nice - needs special order.
 			// TODO: not nice - needs special order.
-			{ in: ' --- $', out: ' — ' },
 			// TODO: not nice - needs spaces.
 			// TODO: not nice - needs spaces.
+			{ in: ' --- $', out: ' — ' },
 			{ in: ' -- $', out: ' – ' },
 			{ in: ' -- $', out: ' – ' },
-			// TODO: add language rules
-			// TODO: add secondary support
-			{ in: '"([a-zA-Z -]+)"$', out: '„$1”' }
+
+			// English quotation - primary.
+			{ in: buildQuotesPattern( '"' ), out: '“$1”' },
+			// English quotation - secondary.
+			{ in: buildQuotesPattern( '\'' ), out: '‘$1’' }
 		];
 		];
 
 
 		for ( const transformation of transformations ) {
 		for ( const transformation of transformations ) {
-			const regExp = new RegExp( transformation.in );
+			const regExp = new RegExp( transformation.in, 'u' );
 
 
 			// setup text watcher
 			// setup text watcher
 			const watcher = new TextWatcher( editor, text => regExp.test( text ), text => {
 			const watcher = new TextWatcher( editor, text => regExp.test( text ), text => {
@@ -77,3 +80,11 @@ export default class TextTransformation extends Plugin {
 		}
 		}
 	}
 	}
 }
 }
+
+// Returns a RegExp pattern string that detects a sentence inside a quote.
+//
+// @param {String} quoteCharacter a character to creat a pattern for.
+// @returns {String}
+function buildQuotesPattern( quoteCharacter ) {
+	return `${ quoteCharacter }([^${ quoteCharacter }]+)${ quoteCharacter }$`;
+}

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

@@ -48,17 +48,30 @@ describe( 'Text transformation feature', () => {
 		testTransformation( '(tm)', '™' );
 		testTransformation( '(tm)', '™' );
 		testTransformation( '1/2', '½' );
 		testTransformation( '1/2', '½' );
 		testTransformation( '<=', '≤' );
 		testTransformation( '<=', '≤' );
-		testTransformation( '"Foo bar baz"', '„Foo bar baz”' );
+
+		describe( 'quotations', () => {
+			describe( 'english US', () => {
+				describe( 'primary', () => {
+					testTransformation( '"Foo 1992 — bar(1) baz: xyz."', '“Foo 1992 — bar(1) baz: xyz.”' );
+					testTransformation( '\' foo "bar"', '\' foo “bar”' );
+				} );
+
+				describe( 'secondary', () => {
+					testTransformation( '\'Foo 1992 — bar(1) baz: xyz.\'', '‘Foo 1992 — bar(1) baz: xyz.’' );
+					testTransformation( '" foo \'bar\'', '" foo ‘bar’' );
+				} );
+			} );
+		} );
 
 
 		function testTransformation( transformFrom, transformTo ) {
 		function testTransformation( transformFrom, transformTo ) {
-			it( `should transform ${ transformFrom } to ${ transformTo }`, () => {
+			it( `should transform "${ transformFrom }" to "${ transformTo }"`, () => {
 				setData( model, '<paragraph>[]</paragraph>' );
 				setData( model, '<paragraph>[]</paragraph>' );
 
 
 				model.change( writer => {
 				model.change( writer => {
 					writer.insertText( transformFrom, doc.selection.getFirstPosition() );
 					writer.insertText( transformFrom, doc.selection.getFirstPosition() );
 				} );
 				} );
 
 
-				expect( getData( model ) ).to.equal( '<paragraph>' + transformTo + '[]</paragraph>' );
+				expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>${ transformTo }</paragraph>` );
 			} );
 			} );
 		}
 		}
 	} );
 	} );