8
0

texttransformation.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  7. import TextTransformation from '../src/texttransformation';
  8. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. describe( 'Text transformation feature', () => {
  11. let editorElement, editor, model, doc;
  12. beforeEach( () => {
  13. editorElement = global.document.createElement( 'div' );
  14. global.document.body.appendChild( editorElement );
  15. } );
  16. afterEach( () => {
  17. editorElement.remove();
  18. if ( editor ) {
  19. return editor.destroy();
  20. }
  21. } );
  22. it( 'should be loaded', () => {
  23. return createEditorInstance().then( () => {
  24. expect( editor.plugins.get( TextTransformation ) ).to.instanceOf( TextTransformation );
  25. } );
  26. } );
  27. it( 'has proper name', () => {
  28. return createEditorInstance().then( () => {
  29. expect( TextTransformation.pluginName ).to.equal( 'TextTransformation' );
  30. } );
  31. } );
  32. describe( 'transformations', () => {
  33. beforeEach( createEditorInstance );
  34. describe( 'symbols', () => {
  35. testTransformation( '(c)', '©' );
  36. // TODO: skip because of CI: testTransformation( '(tm)', '™' );
  37. } );
  38. describe( 'mathematical', () => {
  39. testTransformation( '1/2', '½' );
  40. testTransformation( '<=', '≤' );
  41. } );
  42. describe( 'dashes', () => {
  43. testTransformation( ' -- ', ' – ' );
  44. testTransformation( ' --- ', ' — ' );
  45. } );
  46. describe( 'quotations', () => {
  47. describe( 'english US', () => {
  48. describe( 'primary', () => {
  49. testTransformation( '"Foo 1992 — bar(1) baz: xyz."', '“Foo 1992 — bar(1) baz: xyz.”' );
  50. testTransformation( '\' foo "bar"', '\' foo “bar”' );
  51. } );
  52. describe( 'secondary', () => {
  53. testTransformation( '\'Foo 1992 — bar(1) baz: xyz.\'', '‘Foo 1992 — bar(1) baz: xyz.’' );
  54. testTransformation( '" foo \'bar\'', '" foo ‘bar’' );
  55. } );
  56. } );
  57. } );
  58. function testTransformation( transformFrom, transformTo ) {
  59. it( `should transform "${ transformFrom }" to "${ transformTo }"`, () => {
  60. setData( model, '<paragraph>[]</paragraph>' );
  61. const letters = transformFrom.split( '' );
  62. for ( const letter of letters ) {
  63. model.enqueueChange( model.createBatch(), writer => {
  64. writer.insertText( letter, doc.selection.focus );
  65. } );
  66. }
  67. expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>${ transformTo }</paragraph>` );
  68. } );
  69. }
  70. } );
  71. describe( 'configuration', () => {
  72. it( 'should allow adding own rules with string pattern', () => {
  73. return createEditorInstance( {
  74. textTransformation: {
  75. transformations: [
  76. {
  77. from: '([a-z]+)@(example.com)$',
  78. to: '$1.at.$2'
  79. }
  80. ]
  81. }
  82. } ).then( () => {
  83. setData( model, '<paragraph>[]</paragraph>' );
  84. model.enqueueChange( model.createBatch(), writer => {
  85. writer.insertText( 'user@example.com', doc.selection.focus );
  86. } );
  87. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>user.at.example.com</paragraph>' );
  88. } );
  89. } );
  90. it( 'should allow adding own rules with RegExp object', () => {
  91. return createEditorInstance( {
  92. textTransformation: {
  93. transformations: [
  94. {
  95. from: /([a-z]+)@(example.com)$/,
  96. to: '$1.at.$2'
  97. }
  98. ]
  99. }
  100. } ).then( () => {
  101. setData( model, '<paragraph>[]</paragraph>' );
  102. model.enqueueChange( model.createBatch(), writer => {
  103. writer.insertText( 'user@example.com', doc.selection.focus );
  104. } );
  105. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>user.at.example.com</paragraph>' );
  106. } );
  107. } );
  108. } );
  109. function createEditorInstance( additionalConfig = {} ) {
  110. return ClassicTestEditor
  111. .create( editorElement, Object.assign( {
  112. plugins: [ Paragraph, TextTransformation ]
  113. }, additionalConfig ) )
  114. .then( newEditor => {
  115. editor = newEditor;
  116. model = editor.model;
  117. doc = model.document;
  118. } );
  119. }
  120. } );