texttransformation.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. } );
  75. describe( 'quotations', () => {
  76. describe( 'english US', () => {
  77. describe( 'primary', () => {
  78. testTransformation( ' "Foo 1992 — bar(1) baz: xyz."', ' “Foo 1992 — bar(1) baz: xyz.”' );
  79. testTransformation( '\' foo "bar"', '\' foo “bar”' );
  80. testTransformation( 'Foo "Bar bar\'s it\'s a baz"', 'Foo “Bar bar\'s it\'s a baz”' );
  81. } );
  82. describe( 'secondary', () => {
  83. testTransformation( ' \'Foo 1992 — bar(1) baz: xyz.\'', ' ‘Foo 1992 — bar(1) baz: xyz.’' );
  84. testTransformation( '" foo \'bar\'', '" foo ‘bar’' );
  85. } );
  86. } );
  87. } );
  88. // https://github.com/ckeditor/ckeditor5-typing/issues/203.
  89. it( 'should replace only the parts of content which changed', () => {
  90. setData( model, '<paragraph>Foo "<$text bold="true">Bar</$text>[]</paragraph>' );
  91. simulateTyping( '"' );
  92. expect( getData( model, { withoutSelection: true } ) )
  93. .to.equal( '<paragraph>Foo “<$text bold="true">Bar”</$text></paragraph>' );
  94. } );
  95. it( 'should keep styles of the replaced text #1', () => {
  96. setData( model, '<paragraph>Foo <$text bold="true">"</$text>Bar[]</paragraph>' );
  97. model.change( writer => {
  98. writer.setSelectionAttribute( { bold: true } );
  99. } );
  100. simulateTyping( '"' );
  101. expect( getData( model, { withoutSelection: true } ) )
  102. .to.equal( '<paragraph>Foo <$text bold="true">“</$text>Bar<$text bold="true">”</$text></paragraph>' );
  103. } );
  104. it( 'should keep styles of the replaced text #2', () => {
  105. setData( model, '<paragraph>F<$text bold="true">oo "B</$text>ar[]</paragraph>' );
  106. simulateTyping( '"' );
  107. expect( getData( model, { withoutSelection: true } ) )
  108. .to.equal( '<paragraph>F<$text bold="true">oo “B</$text>ar”</paragraph>' );
  109. } );
  110. function testTransformation( transformFrom, transformTo ) {
  111. it( `should transform "${ transformFrom }" to "${ transformTo }"`, () => {
  112. setData( model, '<paragraph>A foo[]</paragraph>' );
  113. simulateTyping( transformFrom );
  114. expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>A foo${ transformTo }</paragraph>` );
  115. } );
  116. it( `should not transform "${ transformFrom }" to "${ transformTo }" inside text`, () => {
  117. setData( model, '<paragraph>[]</paragraph>' );
  118. // Insert text - should not be transformed.
  119. model.enqueueChange( model.createBatch(), writer => {
  120. writer.insertText( `foo ${ transformFrom } bar`, doc.selection.focus );
  121. } );
  122. // Enforce text watcher check after insertion.
  123. model.enqueueChange( model.createBatch(), writer => {
  124. writer.insertText( ' ', doc.selection.focus );
  125. } );
  126. expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>foo ${ transformFrom } bar </paragraph>` );
  127. } );
  128. }
  129. } );
  130. describe( 'configuration', () => {
  131. it( 'should allow adding own rules with string pattern', () => {
  132. return createEditorInstance( {
  133. typing: {
  134. transformations: {
  135. extra: [
  136. { from: 'CKE', to: 'CKEditor' }
  137. ]
  138. }
  139. }
  140. } ).then( () => {
  141. setData( model, '<paragraph>[]</paragraph>' );
  142. simulateTyping( 'CKE' );
  143. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
  144. } );
  145. } );
  146. it( 'should allow adding own rules with RegExp object', () => {
  147. return createEditorInstance( {
  148. typing: {
  149. transformations: {
  150. extra: [
  151. { from: /([a-z]+)(@)(example.com)$/, to: [ null, '.at.', null ] }
  152. ]
  153. }
  154. }
  155. } ).then( () => {
  156. setData( model, '<paragraph>[]</paragraph>' );
  157. simulateTyping( 'user@example.com' );
  158. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>user.at.example.com</paragraph>' );
  159. } );
  160. } );
  161. it( 'should allow adding own rules with function as `to` value', () => {
  162. return createEditorInstance( {
  163. typing: {
  164. transformations: {
  165. extra: [
  166. { from: /(\. )([a-z])$/, to: matches => [ null, matches[ 1 ].toUpperCase() ] }
  167. ]
  168. }
  169. }
  170. } ).then( () => {
  171. setData( model, '<paragraph>Foo. []</paragraph>' );
  172. simulateTyping( 'b' );
  173. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>Foo. B</paragraph>' );
  174. } );
  175. } );
  176. it( 'should not alter include rules adding own rules as extra', () => {
  177. return createEditorInstance( {
  178. typing: {
  179. transformations: {
  180. extra: [
  181. { from: 'CKE', to: 'CKEditor' }
  182. ]
  183. }
  184. }
  185. } ).then( () => {
  186. setData( model, '<paragraph>[]</paragraph>' );
  187. simulateTyping( 'CKE' );
  188. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
  189. simulateTyping( '(tm)' );
  190. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor™</paragraph>' );
  191. } );
  192. } );
  193. it( 'should overwrite all rules when defining include rules', () => {
  194. return createEditorInstance( {
  195. typing: {
  196. transformations: {
  197. include: [
  198. { from: 'CKE', to: 'CKEditor' }
  199. ]
  200. }
  201. }
  202. } ).then( () => {
  203. setData( model, '<paragraph>[]</paragraph>' );
  204. simulateTyping( 'CKE' );
  205. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
  206. simulateTyping( '(tm)' );
  207. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor(tm)</paragraph>' );
  208. } );
  209. } );
  210. it( 'should remove rules from group when defining remove rules', () => {
  211. return createEditorInstance( {
  212. typing: {
  213. transformations: {
  214. include: [ 'symbols' ],
  215. remove: [ 'trademark' ]
  216. }
  217. }
  218. } ).then( () => {
  219. setData( model, '<paragraph>[]</paragraph>' );
  220. simulateTyping( '(tm)' );
  221. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)</paragraph>' );
  222. simulateTyping( '(r)' );
  223. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)®</paragraph>' );
  224. } );
  225. } );
  226. it( 'should remove all rules from group when group is in remove', () => {
  227. return createEditorInstance( {
  228. typing: {
  229. transformations: {
  230. include: [ 'symbols', 'typography' ],
  231. remove: [ 'symbols' ]
  232. }
  233. }
  234. } ).then( () => {
  235. setData( model, '<paragraph>[]</paragraph>' );
  236. simulateTyping( '(tm)' );
  237. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)</paragraph>' );
  238. simulateTyping( '...' );
  239. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)…</paragraph>' );
  240. } );
  241. } );
  242. it( 'should not fail for unknown rule name', () => {
  243. return createEditorInstance( {
  244. typing: {
  245. transformations: {
  246. include: [ 'symbols', 'typo' ]
  247. }
  248. }
  249. } );
  250. } );
  251. it( 'should not fail for re-declared include rules config', () => {
  252. return createEditorInstance( {
  253. typing: {
  254. transformations: {
  255. extra: [ 'trademark' ]
  256. }
  257. }
  258. } ).then( () => {
  259. setData( model, '<paragraph>[]</paragraph>' );
  260. simulateTyping( '(tm)' );
  261. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>™</paragraph>' );
  262. } );
  263. } );
  264. } );
  265. function createEditorInstance( additionalConfig = {} ) {
  266. return ClassicTestEditor
  267. .create( editorElement, Object.assign( {
  268. plugins: [ Typing, Paragraph, Bold, TextTransformation ]
  269. }, additionalConfig ) )
  270. .then( newEditor => {
  271. editor = newEditor;
  272. model = editor.model;
  273. doc = model.document;
  274. } );
  275. }
  276. function simulateTyping( transformFrom ) {
  277. const letters = transformFrom.split( '' );
  278. for ( const letter of letters ) {
  279. editor.execute( 'input', { text: letter } );
  280. }
  281. }
  282. } );