texttransformation.js 8.7 KB

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