texttransformation.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. return ClassicTestEditor
  16. .create( editorElement, {
  17. plugins: [ Paragraph, TextTransformation ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. doc = model.document;
  23. } );
  24. } );
  25. afterEach( () => {
  26. editorElement.remove();
  27. return editor.destroy();
  28. } );
  29. it( 'should be loaded', () => {
  30. expect( editor.plugins.get( TextTransformation ) ).to.instanceOf( TextTransformation );
  31. } );
  32. it( 'has proper name', () => {
  33. expect( TextTransformation.pluginName ).to.equal( 'TextTransformation' );
  34. } );
  35. describe( 'transformations', () => {
  36. describe( 'symbols', () => {
  37. testTransformation( '(c)', '©' );
  38. // TODO: skip because of CI: testTransformation( '(tm)', '™' );
  39. } );
  40. describe( 'mathematical', () => {
  41. testTransformation( '1/2', '½' );
  42. testTransformation( '<=', '≤' );
  43. } );
  44. describe( 'dashes', () => {
  45. testTransformation( ' -- ', ' – ' );
  46. testTransformation( ' --- ', ' — ' );
  47. } );
  48. describe( 'quotations', () => {
  49. describe( 'english US', () => {
  50. describe( 'primary', () => {
  51. testTransformation( '"Foo 1992 — bar(1) baz: xyz."', '“Foo 1992 — bar(1) baz: xyz.”' );
  52. testTransformation( '\' foo "bar"', '\' foo “bar”' );
  53. } );
  54. describe( 'secondary', () => {
  55. testTransformation( '\'Foo 1992 — bar(1) baz: xyz.\'', '‘Foo 1992 — bar(1) baz: xyz.’' );
  56. testTransformation( '" foo \'bar\'', '" foo ‘bar’' );
  57. } );
  58. } );
  59. } );
  60. function testTransformation( transformFrom, transformTo ) {
  61. it( `should transform "${ transformFrom }" to "${ transformTo }"`, () => {
  62. setData( model, '<paragraph>[]</paragraph>' );
  63. const letters = transformFrom.split( '' );
  64. for ( const letter of letters ) {
  65. model.change( writer => {
  66. writer.insertText( letter, doc.selection.focus );
  67. } );
  68. }
  69. expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>${ transformTo }</paragraph>` );
  70. } );
  71. }
  72. } );
  73. describe( 'configuration', () => {
  74. it( 'should allow adding own rules with string pattern' );
  75. it( 'should allow adding own rules with RegExp object' );
  76. } );
  77. } );