/** * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor'; import global from '@ckeditor/ckeditor5-utils/src/dom/global'; import Typing from '../src/typing'; import TextTransformation from '../src/texttransformation'; import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold'; import CodeBlock from '@ckeditor/ckeditor5-code-block/src/codeblock'; describe( 'Text transformation feature', () => { let editorElement, editor, model, doc; beforeEach( () => { editorElement = global.document.createElement( 'div' ); global.document.body.appendChild( editorElement ); } ); afterEach( () => { editorElement.remove(); if ( editor ) { return editor.destroy(); } } ); it( 'should be loaded', () => { return createEditorInstance().then( () => { expect( editor.plugins.get( TextTransformation ) ).to.instanceOf( TextTransformation ); } ); } ); it( 'has proper name', () => { return createEditorInstance().then( () => { expect( TextTransformation.pluginName ).to.equal( 'TextTransformation' ); } ); } ); describe( 'plugin', () => { let plugin; beforeEach( () => { return createEditorInstance().then( () => { plugin = editor.plugins.get( TextTransformation ); } ); } ); it( 'should be enabled after initialization', () => { plugin.init(); expect( plugin.isEnabled ).to.be.true; } ); it( 'should initialize watchers after initialization', () => { const enableWatchersSpy = sinon.spy( plugin, '_enableTransformationWatchers' ); plugin.init(); sinon.assert.calledOnce( enableWatchersSpy ); } ); it( 'should have active watchers when is enabled', () => { const enableWatchersSpy = sinon.spy( plugin, '_enableTransformationWatchers' ); plugin.isEnabled = false; plugin.isEnabled = true; expect( plugin.isEnabled ).to.be.true; expect( plugin._watchersStack.size ).to.be.at.least( 1 ); sinon.assert.calledOnce( enableWatchersSpy ); } ); it( 'should not have active watchers when is disabled', () => { const disableWatchersSpy = sinon.spy( plugin, '_disableTransformationWatchers' ); plugin.isEnabled = false; expect( plugin.isEnabled ).to.be.false; sinon.assert.calledOnce( disableWatchersSpy ); expect( plugin._watchersStack.size ).to.be.equal( 0 ); } ); } ); describe( 'transformations', () => { beforeEach( createEditorInstance ); it( 'should not work for selection changes', () => { setData( model, 'foo bar(tm) baz[]' ); model.change( writer => { writer.setSelection( doc.getRoot().getChild( 0 ), 11 ); } ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo bar(tm) baz' ); } ); it( 'should not work for deletion changes', () => { setData( model, 'foo bar(tm) []' ); // Simulate delete command. model.change( writer => { const selection = writer.createSelection( doc.selection ); model.modifySelection( selection, { direction: 'backward', unit: 'character' } ); model.deleteContent( selection, { doNotResetEntireContent: true } ); } ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo bar(tm)' ); } ); it( 'should not work for merging changes', () => { setData( model, 'foo bar(tm)[] baz' ); // Simulate delete command. model.change( writer => { writer.merge( writer.createPositionAfter( doc.getRoot().getChild( 0 ) ) ); } ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo bar(tm) baz' ); } ); describe( 'symbols', () => { testTransformation( '(c)', '©' ); testTransformation( '(r)', '®' ); testTransformation( '(tm)', '™' ); } ); describe( 'mathematical', () => { testTransformation( '1/2', '½' ); testTransformation( '<=', '≤' ); } ); describe( 'typography', () => { testTransformation( '...', '…' ); testTransformation( ' -- ', ' – ' ); testTransformation( ' --- ', ' — ' ); testTransformation( '-- ', '– ', '' ); testTransformation( '--- ', '— ', '' ); } ); 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”' ); testTransformation( 'Foo "Bar bar\'s it\'s a baz"', 'Foo “Bar bar\'s it\'s a baz”' ); testTransformation( ' ""', ' “”' ); testTransformation( ' "Bar baz"', ' “Bar baz”', '"A foo' ); } ); describe( 'secondary', () => { testTransformation( ' \'Foo 1992 — bar(1) baz: xyz.\'', ' ‘Foo 1992 — bar(1) baz: xyz.’' ); testTransformation( '" foo \'bar\'', '" foo ‘bar’' ); testTransformation( ' \'\'', ' ‘’' ); } ); } ); } ); // https://github.com/ckeditor/ckeditor5-typing/issues/203. it( 'should replace only the parts of content which changed', () => { setData( model, 'Foo "<$text bold="true">Bar[]' ); simulateTyping( '"' ); expect( getData( model, { withoutSelection: true } ) ) .to.equal( 'Foo “<$text bold="true">Bar”' ); } ); it( 'should keep styles of the replaced text #1', () => { setData( model, 'Foo <$text bold="true">"Bar[]' ); model.change( writer => { writer.setSelectionAttribute( { bold: true } ); } ); simulateTyping( '"' ); expect( getData( model, { withoutSelection: true } ) ) .to.equal( 'Foo <$text bold="true">“Bar<$text bold="true">”' ); } ); it( 'should keep styles of the replaced text #2', () => { setData( model, 'F<$text bold="true">oo "Bar[]' ); simulateTyping( '"' ); expect( getData( model, { withoutSelection: true } ) ) .to.equal( 'F<$text bold="true">oo “Bar”' ); } ); it( 'should work with soft breaks in parent', () => { setData( model, '"Foo "Bar[]' ); simulateTyping( '"' ); expect( getData( model, { withoutSelection: true } ) ) .to.equal( '"Foo “Bar”' ); } ); it( 'should be disabled inside code blocks', () => { setData( model, 'some [] code' ); simulateTyping( '1/2' ); const plugin = editor.plugins.get( 'TextTransformation' ); expect( plugin.isEnabled ).to.be.false; expect( getData( model, { withoutSelection: true } ) ) .to.equal( 'some 1/2 code' ); } ); function testTransformation( transformFrom, transformTo, textInParagraph = 'A foo' ) { it( `should transform "${ transformFrom }" to "${ transformTo }"`, () => { setData( model, `${ textInParagraph }[]` ); simulateTyping( transformFrom ); expect( getData( model, { withoutSelection: true } ) ) .to.equal( `${ textInParagraph }${ transformTo }` ); } ); it( `should not transform "${ transformFrom }" to "${ transformTo }" inside text`, () => { setData( model, '[]' ); // Insert text - should not be transformed. model.enqueueChange( model.createBatch(), writer => { 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( `${ textInParagraph }${ transformFrom } bar ` ); } ); } } ); describe( 'configuration', () => { it( 'should allow adding own rules with string pattern', () => { return createEditorInstance( { typing: { transformations: { extra: [ { from: 'CKE', to: 'CKEditor' } ] } } } ).then( () => { setData( model, '[]' ); simulateTyping( 'CKE' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'CKEditor' ); } ); } ); it( 'should allow adding own rules with RegExp object', () => { return createEditorInstance( { typing: { transformations: { extra: [ { from: /([a-z]+)(@)(example.com)$/, to: [ null, '.at.', null ] } ] } } } ).then( () => { setData( model, '[]' ); simulateTyping( 'user@example.com' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'user.at.example.com' ); } ); } ); it( 'should allow adding own rules with function as `to` value', () => { return createEditorInstance( { typing: { transformations: { extra: [ { from: /(\. )([a-z])$/, to: matches => [ null, matches[ 1 ].toUpperCase() ] } ] } } } ).then( () => { setData( model, 'Foo. []' ); simulateTyping( 'b' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'Foo. B' ); } ); } ); it( 'should not alter include rules adding own rules as extra', () => { return createEditorInstance( { typing: { transformations: { extra: [ { from: 'CKE', to: 'CKEditor' } ] } } } ).then( () => { setData( model, '[]' ); simulateTyping( 'CKE' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'CKEditor' ); simulateTyping( '(tm)' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'CKEditor™' ); } ); } ); it( 'should overwrite all rules when defining include rules', () => { return createEditorInstance( { typing: { transformations: { include: [ { from: 'CKE', to: 'CKEditor' } ] } } } ).then( () => { setData( model, '[]' ); simulateTyping( 'CKE' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'CKEditor' ); simulateTyping( '(tm)' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'CKEditor(tm)' ); } ); } ); it( 'should remove rules from group when defining remove rules', () => { return createEditorInstance( { typing: { transformations: { include: [ 'symbols' ], remove: [ 'trademark' ] } } } ).then( () => { setData( model, '[]' ); simulateTyping( '(tm)' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( '(tm)' ); simulateTyping( '(r)' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( '(tm)®' ); } ); } ); it( 'should remove all rules from group when group is in remove', () => { return createEditorInstance( { typing: { transformations: { include: [ 'symbols', 'typography' ], remove: [ 'symbols' ] } } } ).then( () => { setData( model, '[]' ); simulateTyping( '(tm)' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( '(tm)' ); simulateTyping( '...' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( '(tm)…' ); } ); } ); it( 'should not fail for unknown rule name', () => { return createEditorInstance( { typing: { transformations: { include: [ 'symbols', 'typo' ] } } } ); } ); it( 'should not fail for re-declared include rules config', () => { return createEditorInstance( { typing: { transformations: { extra: [ 'trademark' ] } } } ).then( () => { setData( model, '[]' ); simulateTyping( '(tm)' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( '' ); } ); } ); } ); function createEditorInstance( additionalConfig = {} ) { return ClassicTestEditor .create( editorElement, Object.assign( { plugins: [ Typing, Paragraph, Bold, TextTransformation, CodeBlock ] }, additionalConfig ) ) .then( newEditor => { editor = newEditor; model = editor.model; doc = model.document; editor.conversion.elementToElement( { model: 'softBreak', view: 'br' } ); } ); } function simulateTyping( transformFrom ) { const letters = transformFrom.split( '' ); for ( const letter of letters ) { editor.execute( 'input', { text: letter } ); } } } );