texttransformation.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  11. describe( 'Text transformation feature', () => {
  12. let editorElement, editor, model, doc;
  13. beforeEach( () => {
  14. editorElement = global.document.createElement( 'div' );
  15. global.document.body.appendChild( editorElement );
  16. } );
  17. afterEach( () => {
  18. editorElement.remove();
  19. if ( editor ) {
  20. return editor.destroy();
  21. }
  22. } );
  23. it( 'should be loaded', () => {
  24. return createEditorInstance().then( () => {
  25. expect( editor.plugins.get( TextTransformation ) ).to.instanceOf( TextTransformation );
  26. } );
  27. } );
  28. it( 'has proper name', () => {
  29. return createEditorInstance().then( () => {
  30. expect( TextTransformation.pluginName ).to.equal( 'TextTransformation' );
  31. } );
  32. } );
  33. describe( 'transformations', () => {
  34. beforeEach( createEditorInstance );
  35. it( 'should not work for selection changes', () => {
  36. setData( model, '<paragraph>foo bar(tm) baz[]</paragraph>' );
  37. model.change( writer => {
  38. writer.setSelection( doc.getRoot().getChild( 0 ), 11 );
  39. } );
  40. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo bar(tm) baz</paragraph>' );
  41. } );
  42. it( 'should not work for deletion changes', () => {
  43. setData( model, '<paragraph>foo bar(tm) []</paragraph>' );
  44. // Simulate delete command.
  45. model.change( writer => {
  46. const selection = writer.createSelection( doc.selection );
  47. model.modifySelection( selection, { direction: 'backward', unit: 'character' } );
  48. model.deleteContent( selection, { doNotResetEntireContent: true } );
  49. } );
  50. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo bar(tm)</paragraph>' );
  51. } );
  52. it( 'should not work for merging changes', () => {
  53. setData( model, '<paragraph>foo bar(tm)</paragraph><paragraph>[] baz</paragraph>' );
  54. // Simulate delete command.
  55. model.change( writer => {
  56. writer.merge( writer.createPositionAfter( doc.getRoot().getChild( 0 ) ) );
  57. } );
  58. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo bar(tm) baz</paragraph>' );
  59. } );
  60. describe( 'symbols', () => {
  61. testTransformation( '(c)', '©' );
  62. testTransformation( '(r)', '®' );
  63. testTransformation( '(tm)', '™' );
  64. } );
  65. describe( 'mathematical', () => {
  66. testTransformation( '1/2', '½' );
  67. testTransformation( '<=', '≤' );
  68. } );
  69. describe( 'typography', () => {
  70. testTransformation( '...', '…' );
  71. testTransformation( ' -- ', ' – ' );
  72. testTransformation( ' --- ', ' — ' );
  73. } );
  74. describe( 'quotations', () => {
  75. describe( 'english US', () => {
  76. describe( 'primary', () => {
  77. testTransformation( ' "Foo 1992 — bar(1) baz: xyz."', ' “Foo 1992 — bar(1) baz: xyz.”' );
  78. testTransformation( '\' foo "bar"', '\' foo “bar”' );
  79. testTransformation( 'Foo "Bar bar\'s it\'s a baz"', 'Foo “Bar bar\'s it\'s a baz”' );
  80. } );
  81. describe( 'secondary', () => {
  82. testTransformation( ' \'Foo 1992 — bar(1) baz: xyz.\'', ' ‘Foo 1992 — bar(1) baz: xyz.’' );
  83. testTransformation( '" foo \'bar\'', '" foo ‘bar’' );
  84. } );
  85. } );
  86. } );
  87. // https://github.com/ckeditor/ckeditor5-typing/issues/203.
  88. it( 'should replace only the parts of content which changed', () => {
  89. setData( model, '<paragraph>Foo "<$text bold="true">Bar</$text>[]</paragraph>' );
  90. simulateTyping( '"' );
  91. expect( getData( model, { withoutSelection: true } ) )
  92. .to.equal( '<paragraph>Foo “<$text bold="true">Bar</$text>”</paragraph>' );
  93. } );
  94. it( 'should keep styles of the replaced text #1', () => {
  95. setData( model, '<paragraph>Foo <$text bold="true">"</$text>Bar[]</paragraph>' );
  96. model.change( writer => {
  97. writer.insertText( '"', { bold: true }, doc.selection.focus );
  98. } );
  99. expect( getData( model, { withoutSelection: true } ) )
  100. .to.equal( '<paragraph>Foo <$text bold="true">“</$text>Bar<$text bold="true">”</$text></paragraph>' );
  101. } );
  102. it( 'should keep styles of the replaced text #2', () => {
  103. setData( model, '<paragraph>F<$text bold="true">oo "B</$text>ar[]</paragraph>' );
  104. simulateTyping( '"' );
  105. expect( getData( model, { withoutSelection: true } ) )
  106. .to.equal( '<paragraph>F<$text bold="true">oo “B</$text>ar”</paragraph>' );
  107. } );
  108. function testTransformation( transformFrom, transformTo ) {
  109. it( `should transform "${ transformFrom }" to "${ transformTo }"`, () => {
  110. setData( model, '<paragraph>A foo[]</paragraph>' );
  111. simulateTyping( transformFrom );
  112. expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>A foo${ transformTo }</paragraph>` );
  113. } );
  114. it( `should not transform "${ transformFrom }" to "${ transformTo }" inside text`, () => {
  115. setData( model, '<paragraph>[]</paragraph>' );
  116. // Insert text - should not be transformed.
  117. model.enqueueChange( model.createBatch(), writer => {
  118. writer.insertText( `foo ${ transformFrom } bar`, doc.selection.focus );
  119. } );
  120. // Enforce text watcher check after insertion.
  121. model.enqueueChange( model.createBatch(), writer => {
  122. writer.insertText( ' ', doc.selection.focus );
  123. } );
  124. expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>foo ${ transformFrom } bar </paragraph>` );
  125. } );
  126. }
  127. } );
  128. describe( 'configuration', () => {
  129. it( 'should allow adding own rules with string pattern', () => {
  130. return createEditorInstance( {
  131. typing: {
  132. transformations: {
  133. extra: [
  134. { from: 'CKE', to: 'CKEditor' }
  135. ]
  136. }
  137. }
  138. } ).then( () => {
  139. setData( model, '<paragraph>[]</paragraph>' );
  140. simulateTyping( 'CKE' );
  141. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
  142. } );
  143. } );
  144. it( 'should allow adding own rules with RegExp object', () => {
  145. return createEditorInstance( {
  146. typing: {
  147. transformations: {
  148. extra: [
  149. { from: /([a-z]+)(@)(example.com)$/, to: [ null, '.at.', null ] }
  150. ]
  151. }
  152. }
  153. } ).then( () => {
  154. setData( model, '<paragraph>[]</paragraph>' );
  155. simulateTyping( 'user@example.com' );
  156. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>user.at.example.com</paragraph>' );
  157. } );
  158. } );
  159. it( 'should allow adding own rules with function as `to` value', () => {
  160. return createEditorInstance( {
  161. typing: {
  162. transformations: {
  163. extra: [
  164. { from: /(\. )([a-z])$/, to: matches => [ null, matches[ 1 ].toUpperCase() ] }
  165. ]
  166. }
  167. }
  168. } ).then( () => {
  169. setData( model, '<paragraph>Foo. []</paragraph>' );
  170. simulateTyping( 'b' );
  171. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>Foo. B</paragraph>' );
  172. } );
  173. } );
  174. it( 'should not alter include rules adding own rules as extra', () => {
  175. return createEditorInstance( {
  176. typing: {
  177. transformations: {
  178. extra: [
  179. { from: 'CKE', to: 'CKEditor' }
  180. ]
  181. }
  182. }
  183. } ).then( () => {
  184. setData( model, '<paragraph>[]</paragraph>' );
  185. simulateTyping( 'CKE' );
  186. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
  187. simulateTyping( '(tm)' );
  188. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor™</paragraph>' );
  189. } );
  190. } );
  191. it( 'should overwrite all rules when defining include rules', () => {
  192. return createEditorInstance( {
  193. typing: {
  194. transformations: {
  195. include: [
  196. { from: 'CKE', to: 'CKEditor' }
  197. ]
  198. }
  199. }
  200. } ).then( () => {
  201. setData( model, '<paragraph>[]</paragraph>' );
  202. simulateTyping( 'CKE' );
  203. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
  204. simulateTyping( '(tm)' );
  205. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor(tm)</paragraph>' );
  206. } );
  207. } );
  208. it( 'should remove rules from group when defining remove rules', () => {
  209. return createEditorInstance( {
  210. typing: {
  211. transformations: {
  212. include: [ 'symbols' ],
  213. remove: [ 'trademark' ]
  214. }
  215. }
  216. } ).then( () => {
  217. setData( model, '<paragraph>[]</paragraph>' );
  218. simulateTyping( '(tm)' );
  219. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)</paragraph>' );
  220. simulateTyping( '(r)' );
  221. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)®</paragraph>' );
  222. } );
  223. } );
  224. it( 'should remove all rules from group when group is in remove', () => {
  225. return createEditorInstance( {
  226. typing: {
  227. transformations: {
  228. include: [ 'symbols', 'typography' ],
  229. remove: [ 'symbols' ]
  230. }
  231. }
  232. } ).then( () => {
  233. setData( model, '<paragraph>[]</paragraph>' );
  234. simulateTyping( '(tm)' );
  235. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)</paragraph>' );
  236. simulateTyping( '...' );
  237. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)…</paragraph>' );
  238. } );
  239. } );
  240. it( 'should not fail for unknown rule name', () => {
  241. return createEditorInstance( {
  242. typing: {
  243. transformations: {
  244. include: [ 'symbols', 'typo' ]
  245. }
  246. }
  247. } );
  248. } );
  249. it( 'should not fail for re-declared include rules config', () => {
  250. return createEditorInstance( {
  251. typing: {
  252. transformations: {
  253. extra: [ 'trademark' ]
  254. }
  255. }
  256. } ).then( () => {
  257. setData( model, '<paragraph>[]</paragraph>' );
  258. simulateTyping( '(tm)' );
  259. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>™</paragraph>' );
  260. } );
  261. } );
  262. } );
  263. function createEditorInstance( additionalConfig = {} ) {
  264. return ClassicTestEditor
  265. .create( editorElement, Object.assign( {
  266. plugins: [ Paragraph, Bold, TextTransformation ]
  267. }, additionalConfig ) )
  268. .then( newEditor => {
  269. editor = newEditor;
  270. model = editor.model;
  271. doc = model.document;
  272. } );
  273. }
  274. function simulateTyping( transformFrom ) {
  275. const letters = transformFrom.split( '' );
  276. for ( const letter of letters ) {
  277. model.enqueueChange( model.createBatch(), writer => {
  278. writer.insertText( letter, doc.selection.focus );
  279. } );
  280. }
  281. }
  282. } );