Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #212 from ckeditor/t/200
Browse files Browse the repository at this point in the history
Fix: Allow dashes on the begging of a line. Closes #200.
  • Loading branch information
scofalik authored Jul 19, 2019
2 parents 58f5696 + d089a6f commit 6ef7d47
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/texttransformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '”' ] },
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 11 additions & 5 deletions tests/texttransformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ describe( 'Text transformation feature', () => {
testTransformation( '...', '…' );
testTransformation( ' -- ', ' – ' );
testTransformation( ' --- ', ' — ' );
testTransformation( '-- ', '– ', '' );
testTransformation( '--- ', '— ', '' );
} );

describe( 'quotations', () => {
Expand All @@ -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( ' \'\'', ' ‘’' );
} );
} );
} );
Expand Down Expand Up @@ -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( '' );

Expand All @@ -130,23 +134,25 @@ 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`, () => {
setData( model, '<paragraph>[]</paragraph>' );

// 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.
model.enqueueChange( model.createBatch(), writer => {
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>` );
} );
}
} );
Expand Down

0 comments on commit 6ef7d47

Please sign in to comment.