texttransformation.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 CodeBlockEditing from '@ckeditor/ckeditor5-code-block/src/codeblockediting';
  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. function testTransformation( transformFrom, transformTo, textInParagraph = 'A foo' ) {
  158. it( `should transform "${ transformFrom }" to "${ transformTo }"`, () => {
  159. setData( model, `<paragraph>${ textInParagraph }[]</paragraph>` );
  160. simulateTyping( transformFrom );
  161. expect( getData( model, { withoutSelection: true } ) )
  162. .to.equal( `<paragraph>${ textInParagraph }${ transformTo }</paragraph>` );
  163. } );
  164. it( `should not transform "${ transformFrom }" to "${ transformTo }" inside text`, () => {
  165. setData( model, '<paragraph>[]</paragraph>' );
  166. // Insert text - should not be transformed.
  167. model.enqueueChange( model.createBatch(), writer => {
  168. writer.insertText( `${ textInParagraph }${ transformFrom } bar`, doc.selection.focus );
  169. } );
  170. // Enforce text watcher check after insertion.
  171. model.enqueueChange( model.createBatch(), writer => {
  172. writer.insertText( ' ', doc.selection.focus );
  173. } );
  174. expect( getData( model, { withoutSelection: true } ) )
  175. .to.equal( `<paragraph>${ textInParagraph }${ transformFrom } bar </paragraph>` );
  176. } );
  177. }
  178. } );
  179. describe( 'configuration', () => {
  180. it( 'should allow adding own rules with string pattern', () => {
  181. return createEditorInstance( {
  182. typing: {
  183. transformations: {
  184. extra: [
  185. { from: 'CKE', to: 'CKEditor' }
  186. ]
  187. }
  188. }
  189. } ).then( () => {
  190. setData( model, '<paragraph>[]</paragraph>' );
  191. simulateTyping( 'CKE' );
  192. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
  193. } );
  194. } );
  195. it( 'should allow adding own rules with RegExp object', () => {
  196. return createEditorInstance( {
  197. typing: {
  198. transformations: {
  199. extra: [
  200. { from: /([a-z]+)(@)(example.com)$/, to: [ null, '.at.', null ] }
  201. ]
  202. }
  203. }
  204. } ).then( () => {
  205. setData( model, '<paragraph>[]</paragraph>' );
  206. simulateTyping( 'user@example.com' );
  207. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>user.at.example.com</paragraph>' );
  208. } );
  209. } );
  210. it( 'should allow adding own rules with function as `to` value', () => {
  211. return createEditorInstance( {
  212. typing: {
  213. transformations: {
  214. extra: [
  215. { from: /(\. )([a-z])$/, to: matches => [ null, matches[ 1 ].toUpperCase() ] }
  216. ]
  217. }
  218. }
  219. } ).then( () => {
  220. setData( model, '<paragraph>Foo. []</paragraph>' );
  221. simulateTyping( 'b' );
  222. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>Foo. B</paragraph>' );
  223. } );
  224. } );
  225. it( 'should not alter include rules adding own rules as extra', () => {
  226. return createEditorInstance( {
  227. typing: {
  228. transformations: {
  229. extra: [
  230. { from: 'CKE', to: 'CKEditor' }
  231. ]
  232. }
  233. }
  234. } ).then( () => {
  235. setData( model, '<paragraph>[]</paragraph>' );
  236. simulateTyping( 'CKE' );
  237. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
  238. simulateTyping( '(tm)' );
  239. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor™</paragraph>' );
  240. } );
  241. } );
  242. it( 'should overwrite all rules when defining include rules', () => {
  243. return createEditorInstance( {
  244. typing: {
  245. transformations: {
  246. include: [
  247. { from: 'CKE', to: 'CKEditor' }
  248. ]
  249. }
  250. }
  251. } ).then( () => {
  252. setData( model, '<paragraph>[]</paragraph>' );
  253. simulateTyping( 'CKE' );
  254. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
  255. simulateTyping( '(tm)' );
  256. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor(tm)</paragraph>' );
  257. } );
  258. } );
  259. it( 'should remove rules from group when defining remove rules', () => {
  260. return createEditorInstance( {
  261. typing: {
  262. transformations: {
  263. include: [ 'symbols' ],
  264. remove: [ 'trademark' ]
  265. }
  266. }
  267. } ).then( () => {
  268. setData( model, '<paragraph>[]</paragraph>' );
  269. simulateTyping( '(tm)' );
  270. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)</paragraph>' );
  271. simulateTyping( '(r)' );
  272. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)®</paragraph>' );
  273. } );
  274. } );
  275. it( 'should remove all rules from group when group is in remove', () => {
  276. return createEditorInstance( {
  277. typing: {
  278. transformations: {
  279. include: [ 'symbols', 'typography' ],
  280. remove: [ 'symbols' ]
  281. }
  282. }
  283. } ).then( () => {
  284. setData( model, '<paragraph>[]</paragraph>' );
  285. simulateTyping( '(tm)' );
  286. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)</paragraph>' );
  287. simulateTyping( '...' );
  288. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>(tm)…</paragraph>' );
  289. } );
  290. } );
  291. it( 'should not fail for unknown rule name', () => {
  292. return createEditorInstance( {
  293. typing: {
  294. transformations: {
  295. include: [ 'symbols', 'typo' ]
  296. }
  297. }
  298. } );
  299. } );
  300. it( 'should not fail for re-declared include rules config', () => {
  301. return createEditorInstance( {
  302. typing: {
  303. transformations: {
  304. extra: [ 'trademark' ]
  305. }
  306. }
  307. } ).then( () => {
  308. setData( model, '<paragraph>[]</paragraph>' );
  309. simulateTyping( '(tm)' );
  310. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>™</paragraph>' );
  311. } );
  312. } );
  313. } );
  314. function createEditorInstance( additionalConfig = {} ) {
  315. return ClassicTestEditor
  316. .create( editorElement, Object.assign( {
  317. plugins: [ Typing, Paragraph, Bold, TextTransformation, CodeBlockEditing ]
  318. }, additionalConfig ) )
  319. .then( newEditor => {
  320. editor = newEditor;
  321. model = editor.model;
  322. doc = model.document;
  323. editor.conversion.elementToElement( {
  324. model: 'softBreak',
  325. view: 'br'
  326. } );
  327. } );
  328. }
  329. function simulateTyping( transformFrom ) {
  330. const letters = transformFrom.split( '' );
  331. for ( const letter of letters ) {
  332. editor.execute( 'input', { text: letter } );
  333. }
  334. }
  335. } );