texttransformation.js 11 KB

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