8
0

pastefromoffice.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import DocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import PasteFromOffice from '../src/pastefromoffice';
  10. import { createDataTransfer } from './_utils/utils';
  11. describe( 'Paste from Office plugin', () => {
  12. let editor, content, normalizeSpy;
  13. testUtils.createSinonSandbox();
  14. before( () => {
  15. content = new DocumentFragment();
  16. } );
  17. beforeEach( () => {
  18. return VirtualTestEditor
  19. .create( {
  20. plugins: [ Clipboard, PasteFromOffice ]
  21. } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. normalizeSpy = testUtils.sinon.spy( editor.plugins.get( 'PasteFromOffice' ), '_normalizeWordInput' );
  25. } );
  26. } );
  27. it( 'runs normalizations if Word meta tag detected #1', () => {
  28. const dataTransfer = createDataTransfer( {
  29. 'text/html': '<meta name=Generator content="Microsoft Word 15">'
  30. } );
  31. editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', { content, dataTransfer } );
  32. expect( normalizeSpy.calledOnce ).to.true;
  33. } );
  34. it( 'runs normalizations if Word meta tag detected #2', () => {
  35. const dataTransfer = createDataTransfer( {
  36. 'text/html': '<html><head><meta name="Generator" content=Microsoft Word 15></head></html>'
  37. } );
  38. editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', { content, dataTransfer } );
  39. expect( normalizeSpy.calledOnce ).to.true;
  40. } );
  41. it( 'does not normalize the content without Word meta tag', () => {
  42. const dataTransfer = createDataTransfer( {
  43. 'text/html': '<meta name=Generator content="Other">'
  44. } );
  45. editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', { content, dataTransfer } );
  46. expect( normalizeSpy.called ).to.false;
  47. } );
  48. it( 'does not process content many times for the same `inputTransformation` event', () => {
  49. const clipboard = editor.plugins.get( 'Clipboard' );
  50. const dataTransfer = createDataTransfer( {
  51. 'text/html': '<html><head><meta name="Generator" content=Microsoft Word 15></head></html>'
  52. } );
  53. let eventRefired = false;
  54. clipboard.on( 'inputTransformation', ( evt, data ) => {
  55. if ( !eventRefired ) {
  56. eventRefired = true;
  57. evt.stop();
  58. clipboard.fire( 'inputTransformation', data );
  59. }
  60. expect( data.pasteFromOfficeProcessed ).to.true;
  61. expect( normalizeSpy.calledOnce ).to.true;
  62. }, { priority: 'low' } );
  63. editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', { content, dataTransfer } );
  64. expect( normalizeSpy.calledOnce ).to.true;
  65. } );
  66. } );