Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict mathematical text transformation not to interfere with dates #10528

Merged
merged 2 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/ckeditor5-typing/src/texttransformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const TRANSFORMATIONS = {
trademark: { from: '(tm)', to: '™' },

// Mathematical:
oneHalf: { from: '1/2', to: '½' },
oneThird: { from: '1/3', to: '⅓' },
twoThirds: { from: '2/3', to: '⅔' },
oneForth: { from: '1/4', to: '¼' },
threeQuarters: { from: '3/4', to: '¾' },
oneHalf: { from: /(^|[^/a-z0-9])(1\/2)([^/a-z0-9])$/i, to: [ null, '½', null ] },
oneThird: { from: /(^|[^/a-z0-9])(1\/3)([^/a-z0-9])$/i, to: [ null, '⅓', null ] },
twoThirds: { from: /(^|[^/a-z0-9])(2\/3)([^/a-z0-9])$/i, to: [ null, '⅔', null ] },
oneForth: { from: /(^|[^/a-z0-9])(1\/4)([^/a-z0-9])$/i, to: [ null, '¼', null ] },
threeQuarters: { from: /(^|[^/a-z0-9])(3\/4)([^/a-z0-9])$/i, to: [ null, '¾', null ] },
lessThanOrEqual: { from: '<=', to: '≤' },
greaterThanOrEqual: { from: '>=', to: '≥' },
notEqual: { from: '!=', to: '≠' },
Expand Down
41 changes: 36 additions & 5 deletions packages/ckeditor5-typing/tests/texttransformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ describe( 'Text transformation feature', () => {
} );

describe( 'mathematical', () => {
testTransformation( '1/2', '½' );
testTransformation( '1/2 ', '½ ', '' );
testTransformation( '1/2.', '½.', 'A foo ' );
testTransformation( '1/2+', '½+', '+' );
testShouldNotTransform( 'x1/2 ', '½ ' );
testShouldNotTransform( '1/22', '½2' );
testShouldNotTransform( '11/2', '1½' );
testShouldNotTransform( '1/2A', '½A' );
testTransformation( '<=', '≤' );
} );

Expand Down Expand Up @@ -191,12 +197,12 @@ describe( 'Text transformation feature', () => {
it( 'can undo transformation', () => {
setData( model, '<paragraph>Foo[]</paragraph>' );

simulateTyping( '1/2' );
simulateTyping( '(c)' );

editor.commands.execute( 'undo' );

expect( getData( model, { withoutSelection: true } ) )
.to.equal( '<paragraph>Foo1/2</paragraph>' );
.to.equal( '<paragraph>Foo(c)</paragraph>' );
} );

it( 'can undo transformation by pressing backspace', () => {
Expand All @@ -209,12 +215,12 @@ describe( 'Text transformation feature', () => {

setData( model, '<paragraph>Foo[]</paragraph>' );

simulateTyping( '1/2' );
simulateTyping( '(c)' );

viewDocument.fire( 'delete', deleteEvent );

expect( getData( model, { withoutSelection: true } ) )
.to.equal( '<paragraph>Foo1/2</paragraph>' );
.to.equal( '<paragraph>Foo(c)</paragraph>' );
} );

function testTransformation( transformFrom, transformTo, textInParagraph = 'A foo' ) {
Expand Down Expand Up @@ -243,6 +249,31 @@ describe( 'Text transformation feature', () => {
expect( getData( model, { withoutSelection: true } ) )
.to.equal( `<paragraph>${ textInParagraph }${ transformFrom } bar </paragraph>` );
} );

it( `should not transform "${ transformFrom }" to "${ transformTo } if not right before selection"`, () => {
setData( model, '<paragraph>[]</paragraph>' );

// Insert text - should not be transformed.
model.enqueueChange( model.createBatch(), writer => {
writer.insertText( `${ textInParagraph }${ transformFrom }`, doc.selection.focus );
} );

simulateTyping( ' ' );

expect( getData( model, { withoutSelection: true } ) )
.to.equal( `<paragraph>${ textInParagraph }${ transformFrom } </paragraph>` );
} );
}

function testShouldNotTransform( transformFrom, transformTo ) {
it( `should not transform "${ transformFrom }" to "${ transformTo }"`, () => {
setData( model, '<paragraph>[]</paragraph>' );

simulateTyping( transformFrom );

expect( getData( model, { withoutSelection: true } ) )
.to.equal( `<paragraph>${ transformFrom }</paragraph>` );
} );
}
} );

Expand Down