8
0

texttransformation.js 13 KB

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