8
0
Pārlūkot izejas kodu

Merge pull request #212 from ckeditor/t/200

Fix: Allow dashes on the begging of a line. Closes #200.
Szymon Cofalik 6 gadi atpakaļ
vecāks
revīzija
6cd1ef62d3

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

@@ -32,9 +32,8 @@ const TRANSFORMATIONS = {
 
 	// Typography:
 	horizontalEllipsis: { from: '...', to: '…' },
-	enDash: { from: ' -- ', to: ' – ' },
-	emDash: { from: ' --- ', to: ' — ' },
-
+	enDash: { from: /(^| )(--)( )$/, to: [ null, '–', null ] },
+	emDash: { from: /(^| )(---)( )$/, to: [ null, '—', null ] },
 	// Quotations:
 	// English, US
 	quotesPrimary: { from: buildQuotesRegExp( '"' ), to: [ null, '“', null, '”' ] },
@@ -151,7 +150,7 @@ export default class TextTransformation extends Plugin {
 // @returns {RegExp}
 function normalizeFrom( from ) {
 	if ( typeof from == 'string' ) {
-		return new RegExp( '(' + escapeRegExp( from ) + ')$' );
+		return new RegExp( `(${ escapeRegExp( from ) })$` );
 	}
 
 	// `from` is already a regular expression.

+ 11 - 5
packages/ckeditor5-typing/tests/texttransformation.js

@@ -67,6 +67,8 @@ describe( 'Text transformation feature', () => {
 			testTransformation( '...', '…' );
 			testTransformation( ' -- ', ' – ' );
 			testTransformation( ' --- ', ' — ' );
+			testTransformation( '-- ', '– ', '' );
+			testTransformation( '--- ', '— ', '' );
 		} );
 
 		describe( 'quotations', () => {
@@ -75,11 +77,13 @@ describe( 'Text transformation feature', () => {
 					testTransformation( ' "Foo 1992 — bar(1) baz: xyz."', ' “Foo 1992 — bar(1) baz: xyz.”' );
 					testTransformation( '\' foo "bar"', '\' foo “bar”' );
 					testTransformation( 'Foo "Bar bar\'s it\'s a baz"', 'Foo “Bar bar\'s it\'s a baz”' );
+					testTransformation( ' ""', ' “”' );
 				} );
 
 				describe( 'secondary', () => {
 					testTransformation( ' \'Foo 1992 — bar(1) baz: xyz.\'', ' ‘Foo 1992 — bar(1) baz: xyz.’' );
 					testTransformation( '" foo \'bar\'', '" foo ‘bar’' );
+					testTransformation( ' \'\'', ' ‘’' );
 				} );
 			} );
 		} );
@@ -118,9 +122,9 @@ describe( 'Text transformation feature', () => {
 				.to.equal( '<paragraph>F<$text bold="true">oo “B</$text>ar”</paragraph>' );
 		} );
 
-		function testTransformation( transformFrom, transformTo ) {
+		function testTransformation( transformFrom, transformTo, textInParagraph = 'A foo' ) {
 			it( `should transform "${ transformFrom }" to "${ transformTo }"`, () => {
-				setData( model, '<paragraph>A foo[]</paragraph>' );
+				setData( model, `<paragraph>${ textInParagraph }[]</paragraph>` );
 
 				const letters = transformFrom.split( '' );
 
@@ -130,7 +134,8 @@ describe( 'Text transformation feature', () => {
 					} );
 				}
 
-				expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>A foo${ transformTo }</paragraph>` );
+				expect( getData( model, { withoutSelection: true } ) )
+					.to.equal( `<paragraph>${ textInParagraph }${ transformTo }</paragraph>` );
 			} );
 
 			it( `should not transform "${ transformFrom }" to "${ transformTo }" inside text`, () => {
@@ -138,7 +143,7 @@ describe( 'Text transformation feature', () => {
 
 				// Insert text - should not be transformed.
 				model.enqueueChange( model.createBatch(), writer => {
-					writer.insertText( `foo ${ transformFrom } bar`, doc.selection.focus );
+					writer.insertText( `${ textInParagraph }${ transformFrom } bar`, doc.selection.focus );
 				} );
 
 				// Enforce text watcher check after insertion.
@@ -146,7 +151,8 @@ describe( 'Text transformation feature', () => {
 					writer.insertText( ' ', doc.selection.focus );
 				} );
 
-				expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>foo ${ transformFrom } bar </paragraph>` );
+				expect( getData( model, { withoutSelection: true } ) )
+					.to.equal( `<paragraph>${ textInParagraph }${ transformFrom } bar </paragraph>` );
 			} );
 		}
 	} );