8
0

texttransformation.js 13 KB

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