pastefromoffice.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @license Copyright (c) 2003-2020, 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. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
  13. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  14. describe( 'PasteFromOffice', () => {
  15. const htmlDataProcessor = new HtmlDataProcessor( new ViewDocument( new StylesProcessor() ) );
  16. let editor, pasteFromOffice, clipboard;
  17. testUtils.createSinonSandbox();
  18. beforeEach( () => {
  19. return VirtualTestEditor.create( {
  20. plugins: [ PasteFromOffice, Paragraph ]
  21. } )
  22. .then( _editor => {
  23. editor = _editor;
  24. pasteFromOffice = editor.plugins.get( 'PasteFromOffice' );
  25. clipboard = editor.plugins.get( 'Clipboard' );
  26. } );
  27. } );
  28. it( 'should be loaded', () => {
  29. expect( pasteFromOffice ).to.be.instanceOf( PasteFromOffice, Paragraph );
  30. } );
  31. it( 'has proper name', () => {
  32. expect( PasteFromOffice.pluginName ).to.equal( 'PasteFromOffice' );
  33. } );
  34. it( 'should load Clipboard plugin', () => {
  35. expect( editor.plugins.get( Clipboard ) ).to.be.instanceOf( Clipboard );
  36. } );
  37. describe( 'isTransformedWithPasteFromOffice - flag', () => {
  38. describe( 'data which should be marked with flag', () => {
  39. it( 'should process data with microsoft word header', () => {
  40. checkCorrectData( '<meta name=Generator content="Microsoft Word 15">' );
  41. } );
  42. it( 'should process data with nested microsoft header', () => {
  43. checkCorrectData( '<html><head><meta name="Generator" content=Microsoft Word 15></head></html>' );
  44. } );
  45. it( 'should process data from google docs', () => {
  46. checkCorrectData( '<p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab"></p>' );
  47. } );
  48. function checkCorrectData( inputString ) {
  49. const data = setUpData( inputString );
  50. const getDataSpy = sinon.spy( data.dataTransfer, 'getData' );
  51. clipboard.fire( 'inputTransformation', data );
  52. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  53. sinon.assert.called( getDataSpy );
  54. }
  55. } );
  56. describe( 'data which should not be marked with flag', () => {
  57. it( 'should process data with regular html', () => {
  58. checkNotProcessedData( '<p>Hello world</p>' );
  59. } );
  60. it( 'should process data with similar headers to MS Word', () => {
  61. checkNotProcessedData( '<meta name=Generator content="Other">' );
  62. } );
  63. function checkNotProcessedData( inputString ) {
  64. const data = setUpData( inputString );
  65. const getDataSpy = sinon.spy( data.dataTransfer, 'getData' );
  66. clipboard.fire( 'inputTransformation', data );
  67. expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
  68. sinon.assert.called( getDataSpy );
  69. }
  70. } );
  71. describe( 'data which already have the flag', () => {
  72. it( 'should not process again ms word data containing a flag', () => {
  73. checkAlreadyProcessedData( '<meta name=Generator content="Microsoft Word 15">' +
  74. '<p class="MsoNormal">Hello world<o:p></o:p></p>' );
  75. } );
  76. it( 'should not process again google docs data containing a flag', () => {
  77. checkAlreadyProcessedData( '<meta charset="utf-8"><b id="docs-internal-guid-30db46f5-7fff-15a1-e17c-1234567890ab"' +
  78. 'style="font-weight:normal;"><p dir="ltr">Hello world</p></b>' );
  79. } );
  80. function checkAlreadyProcessedData( inputString ) {
  81. const data = setUpData( inputString, true );
  82. const getDataSpy = sinon.spy( data.dataTransfer, 'getData' );
  83. clipboard.fire( 'inputTransformation', data );
  84. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  85. sinon.assert.notCalled( getDataSpy );
  86. }
  87. } );
  88. } );
  89. // @param {String} inputString html to be processed by paste from office
  90. // @param {Boolean} [isTransformedWithPasteFromOffice=false] if set, marks output data with isTransformedWithPasteFromOffice flag
  91. // @returns {Object} data object simulating content obtained from the clipboard
  92. function setUpData( inputString, isTransformedWithPasteFromOffice = false ) {
  93. const data = {
  94. content: htmlDataProcessor.toView( inputString ),
  95. dataTransfer: createDataTransfer( { 'text/html': inputString } )
  96. };
  97. if ( isTransformedWithPasteFromOffice ) {
  98. data.isTransformedWithPasteFromOffice = true;
  99. }
  100. return data;
  101. }
  102. } );