8
0

texttransformation.js 12 KB

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