8
0

texttransformation.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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( 'quotations', () => {
  45. describe( 'english US', () => {
  46. describe( 'primary', () => {
  47. testTransformation( '"Foo 1992 — bar(1) baz: xyz."', '“Foo 1992 — bar(1) baz: xyz.”' );
  48. testTransformation( '\' foo "bar"', '\' foo “bar”' );
  49. } );
  50. describe( 'secondary', () => {
  51. testTransformation( '\'Foo 1992 — bar(1) baz: xyz.\'', '‘Foo 1992 — bar(1) baz: xyz.’' );
  52. testTransformation( '" foo \'bar\'', '" foo ‘bar’' );
  53. } );
  54. } );
  55. } );
  56. function testTransformation( transformFrom, transformTo ) {
  57. it( `should transform "${ transformFrom }" to "${ transformTo }"`, () => {
  58. setData( model, '<paragraph>[]</paragraph>' );
  59. const letters = transformFrom.split( '' );
  60. for ( const letter of letters ) {
  61. model.change( writer => {
  62. writer.insertText( letter, doc.selection.focus );
  63. } );
  64. }
  65. expect( getData( model, { withoutSelection: true } ) ).to.equal( `<paragraph>${ transformTo }</paragraph>` );
  66. } );
  67. }
  68. } );
  69. describe( 'configuration', () => {
  70. it( 'should allow adding own rules with string pattern' );
  71. it( 'should allow adding own rules with RegExp object' );
  72. } );
  73. } );