Skip to content

Commit

Permalink
Merge pull request #10528 from ckeditor/ck/9170
Browse files Browse the repository at this point in the history
Fix (typing): Restricts mathematical text transformation, so it requires no alphanumeric character before and after the fraction. Closes #9170.
  • Loading branch information
niegowski committed Sep 17, 2021
2 parents 038fac7 + 0c30cef commit e228a7e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
10 changes: 5 additions & 5 deletions packages/ckeditor5-typing/src/texttransformation.js
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
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

0 comments on commit e228a7e

Please sign in to comment.