pastefromoffice.js 4.5 KB

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