8
0

pastefromoffice.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. let editor, pasteFromOffice, clipboard;
  13. testUtils.createSinonSandbox();
  14. beforeEach( () => {
  15. return VirtualTestEditor.create( {
  16. plugins: [ PasteFromOffice ]
  17. } )
  18. .then( _editor => {
  19. editor = _editor;
  20. pasteFromOffice = editor.plugins.get( 'PasteFromOffice' );
  21. clipboard = editor.plugins.get( 'Clipboard' );
  22. } );
  23. } );
  24. it( 'should be loaded', () => {
  25. expect( pasteFromOffice ).to.be.instanceOf( PasteFromOffice );
  26. } );
  27. it( 'has proper name', () => {
  28. expect( PasteFromOffice.pluginName ).to.equal( 'PasteFromOffice' );
  29. } );
  30. it( 'should load Clipboard plugin', () => {
  31. expect( editor.plugins.get( Clipboard ) ).to.be.instanceOf( Clipboard );
  32. } );
  33. describe( 'isTransformedWithPasteFromOffice - flag', () => {
  34. const htmlDataProcessor = new HtmlDataProcessor();
  35. describe( 'data which should be processed', () => {
  36. [
  37. {
  38. 'text/html': '<html><head><meta name="Generator" content=Microsoft Word 15></head></html>'
  39. },
  40. {
  41. 'text/html': '<meta name=Generator content="Microsoft Word 15">'
  42. },
  43. {
  44. 'text/html': '<p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab"></p>'
  45. }
  46. ].forEach( ( inputData, index ) => {
  47. it( `should mark data as transformed with paste from office - data set: #${ index }`, () => {
  48. const data = {
  49. content: htmlDataProcessor.toView( inputData[ 'text/html' ] ),
  50. dataTransfer: createDataTransfer( inputData )
  51. };
  52. clipboard.fire( 'inputTransformation', data );
  53. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  54. } );
  55. } );
  56. } );
  57. describe( 'not recognized data', () => {
  58. [
  59. {
  60. 'text/html': '<p>Hello world</p>'
  61. },
  62. {
  63. 'text/html': '<meta name=Generator content="Other">'
  64. }
  65. ].forEach( ( inputData, index ) => {
  66. it( `should not modify data set: #${ index }`, () => {
  67. const data = {
  68. content: htmlDataProcessor.toView( inputData[ 'text/html' ] ),
  69. dataTransfer: createDataTransfer( inputData )
  70. };
  71. clipboard.fire( 'inputTransformation', data );
  72. expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
  73. } );
  74. } );
  75. } );
  76. describe( 'already processed data', () => {
  77. [
  78. {
  79. // eslint-disable-next-line max-len
  80. 'text/html': '<meta charset="utf-8"><b id="docs-internal-guid-30db46f5-7fff-15a1-e17c-1234567890ab" style="font-weight:normal;"><p dir="ltr">Hello world</p></b>'
  81. },
  82. {
  83. 'text/html': '<meta name=Generator content="Microsoft Word 15"><p class="MsoNormal">Hello world<o:p></o:p></p>'
  84. }
  85. ].forEach( ( inputData, index ) => {
  86. it( `should not modify already processed data: #${ index }`, () => {
  87. const data = {
  88. content: htmlDataProcessor.toView( inputData[ 'text/html' ] ),
  89. dataTransfer: createDataTransfer( inputData ),
  90. isTransformedWithPasteFromOffice: true
  91. };
  92. const getData = sinon.spy( data.dataTransfer, 'getData' );
  93. clipboard.fire( 'inputTransformation', data );
  94. // Data object should not be processed
  95. sinon.assert.notCalled( getData );
  96. } );
  97. } );
  98. } );
  99. } );
  100. } );