pastefromoffice.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. describe( 'data which should be marked with flag', () => {
  36. it( 'should process data with microsoft word header', () => {
  37. checkCorrectData( '<meta name=Generator content="Microsoft Word 15">' );
  38. } );
  39. it( 'should process data with nested microsoft header', () => {
  40. checkCorrectData( '<html><head><meta name="Generator" content=Microsoft Word 15></head></html>' );
  41. } );
  42. it( 'should process data from google docs', () => {
  43. checkCorrectData( '<p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab"></p>' );
  44. } );
  45. function checkCorrectData( inputString ) {
  46. const data = setUpData( inputString );
  47. const getDataSpy = sinon.spy( data.dataTransfer, 'getData' );
  48. clipboard.fire( 'inputTransformation', data );
  49. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  50. sinon.assert.called( getDataSpy );
  51. }
  52. } );
  53. describe( 'data which should not be marked with flag', () => {
  54. it( 'should process data with regular html', () => {
  55. checkNotProcessedData( '<p>Hello world</p>' );
  56. } );
  57. it( 'should process data with similar headers to MS Word', () => {
  58. checkNotProcessedData( '<meta name=Generator content="Other">' );
  59. } );
  60. function checkNotProcessedData( inputString ) {
  61. const data = setUpData( inputString );
  62. const getDataSpy = sinon.spy( data.dataTransfer, 'getData' );
  63. clipboard.fire( 'inputTransformation', data );
  64. expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
  65. sinon.assert.called( getDataSpy );
  66. }
  67. } );
  68. describe( 'data which already have the flag', () => {
  69. it( 'should not process again ms word data containing a flag', () => {
  70. checkAlreadyProcessedData( '<meta name=Generator content="Microsoft Word 15">' +
  71. '<p class="MsoNormal">Hello world<o:p></o:p></p>' );
  72. } );
  73. it( 'should not process again google docs data containing a flag', () => {
  74. checkAlreadyProcessedData( '<meta charset="utf-8"><b id="docs-internal-guid-30db46f5-7fff-15a1-e17c-1234567890ab"' +
  75. 'style="font-weight:normal;"><p dir="ltr">Hello world</p></b>' );
  76. } );
  77. function checkAlreadyProcessedData( inputString ) {
  78. const data = setUpData( inputString, true );
  79. const getDataSpy = sinon.spy( data.dataTransfer, 'getData' );
  80. clipboard.fire( 'inputTransformation', data );
  81. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  82. sinon.assert.notCalled( getDataSpy );
  83. }
  84. } );
  85. } );
  86. // @param {String} inputString html to be processed by paste from office
  87. // @param {Boolean} [isTransformedWithPasteFromOffice=false] if set, marks output data with isTransformedWithPasteFromOffice flag
  88. // @returns {Object} data object simulating content obtained from the clipboard
  89. function setUpData( inputString, isTransformedWithPasteFromOffice = false ) {
  90. const data = {
  91. content: htmlDataProcessor.toView( inputString ),
  92. dataTransfer: createDataTransfer( { 'text/html': inputString } )
  93. };
  94. if ( isTransformedWithPasteFromOffice ) {
  95. data.isTransformedWithPasteFromOffice = true;
  96. }
  97. return data;
  98. }
  99. } );