texttransformation.js 13 KB

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