texttransformation.js 11 KB

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