pastefromoffice.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 PasteFromOffice from '../src/pastefromoffice';
  6. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  9. import { createDataTransfer } from './_utils/utils';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. describe( 'PasteFromOffice', () => {
  12. const htmlDataProcessor = new HtmlDataProcessor();
  13. let editor, pasteFromOffice, clipboard;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. return VirtualTestEditor.create( {
  17. plugins: [ PasteFromOffice ]
  18. } )
  19. .then( _editor => {
  20. editor = _editor;
  21. pasteFromOffice = editor.plugins.get( 'PasteFromOffice' );
  22. clipboard = editor.plugins.get( 'Clipboard' );
  23. } );
  24. } );
  25. it( 'should be loaded', () => {
  26. expect( pasteFromOffice ).to.be.instanceOf( PasteFromOffice );
  27. } );
  28. it( 'has proper name', () => {
  29. expect( PasteFromOffice.pluginName ).to.equal( 'PasteFromOffice' );
  30. } );
  31. it( 'should load Clipboard plugin', () => {
  32. expect( editor.plugins.get( Clipboard ) ).to.be.instanceOf( Clipboard );
  33. } );
  34. describe( 'isTransformedWithPasteFromOffice - flag', () => {
  35. it( 'should process data with microsoft word header', () => {
  36. checkDataProcessing( '<meta name=Generator content="Microsoft Word 15">', true );
  37. } );
  38. it( 'should process data with nested microsoft header', () => {
  39. checkDataProcessing( '<html><head><meta name="Generator" content=Microsoft Word 15></head></html>', true );
  40. } );
  41. it( 'should process data from google docs', () => {
  42. checkDataProcessing( '<p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab"></p>', true );
  43. } );
  44. it( 'should not process data with regular html', () => {
  45. checkDataProcessing( '<p>Hello world</p>', false );
  46. } );
  47. it( 'should not process data with similar headers to MS Word', () => {
  48. checkDataProcessing( '<meta name=Generator content="Other">', false );
  49. } );
  50. it( 'should not process again ms word data containing a flag', () => {
  51. checkDataProcessing( '<meta name=Generator content="Microsoft Word 15"><p class="MsoNormal">Hello world<o:p></o:p></p>',
  52. false, true );
  53. } );
  54. it( 'should not process again google docs data containing a flag', () => {
  55. checkDataProcessing( '<meta charset="utf-8"><b id="docs-internal-guid-30db46f5-7fff-15a1-e17c-1234567890ab"' +
  56. 'style="font-weight:normal;"><p dir="ltr">Hello world</p></b>', false, true );
  57. } );
  58. } );
  59. // @param {String} inputString html to be processed by paste from office
  60. // @param {Boolean} shouldBeProcessed determines if data should be marked as processed with isTransformedWithPasteFromOffice flag
  61. // @param {Boolean} [isAlreadyProcessed=false] apply flag before paste from office plugin will transform the data object
  62. function checkDataProcessing( inputString, shouldBeProcessed, isAlreadyProcessed = false ) {
  63. const data = {
  64. content: htmlDataProcessor.toView( inputString ),
  65. dataTransfer: createDataTransfer( { 'text/html': inputString } )
  66. };
  67. const getData = sinon.spy( data.dataTransfer, 'getData' );
  68. if ( isAlreadyProcessed ) {
  69. data.isTransformedWithPasteFromOffice = true;
  70. }
  71. clipboard.fire( 'inputTransformation', data );
  72. if ( shouldBeProcessed ) {
  73. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  74. sinon.assert.called( getData );
  75. } else if ( isAlreadyProcessed ) {
  76. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  77. sinon.assert.notCalled( getData );
  78. } else {
  79. expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
  80. sinon.assert.called( getData );
  81. }
  82. }
  83. } );