8
0

pastefromoffice.js 4.6 KB

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