texttransformation.js 11 KB

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