pastefromoffice.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import DocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  10. import PasteFromOffice from '../src/pastefromoffice';
  11. import { createDataTransfer } from './_utils/utils';
  12. describe.skip( 'PasteFromOffice', () => {
  13. let editor, content, normalizeSpy;
  14. testUtils.createSinonSandbox();
  15. describe( '_normalizeWordInput()', () => {
  16. before( () => {
  17. content = new DocumentFragment();
  18. } );
  19. beforeEach( () => {
  20. return VirtualTestEditor
  21. .create( {
  22. plugins: [ Clipboard, PasteFromOffice ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. normalizeSpy = testUtils.sinon.spy( PasteFromOffice, '_normalizeWordInput' );
  27. } );
  28. } );
  29. afterEach( () => {
  30. editor.destroy();
  31. } );
  32. it( 'runs normalizations if Word meta tag detected #1', () => {
  33. const dataTransfer = createDataTransfer( {
  34. 'text/html': '<meta name=Generator content="Microsoft Word 15">'
  35. } );
  36. editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', { content, dataTransfer } );
  37. expect( normalizeSpy.calledOnce ).to.true;
  38. } );
  39. it( 'runs normalizations if Word meta tag detected #2', () => {
  40. const dataTransfer = createDataTransfer( {
  41. 'text/html': '<html><head><meta name="Generator" content=Microsoft Word 15></head></html>'
  42. } );
  43. editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', { content, dataTransfer } );
  44. expect( normalizeSpy.calledOnce ).to.true;
  45. } );
  46. it( 'does not normalize the content without Word meta tag', () => {
  47. const dataTransfer = createDataTransfer( {
  48. 'text/html': '<meta name=Generator content="Other">'
  49. } );
  50. editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', { content, dataTransfer } );
  51. expect( normalizeSpy.called ).to.false;
  52. } );
  53. it( 'does not process content many times for the same `inputTransformation` event', () => {
  54. const clipboard = editor.plugins.get( 'Clipboard' );
  55. const dataTransfer = createDataTransfer( {
  56. 'text/html': '<html><head><meta name="Generator" content=Microsoft Word 15></head></html>'
  57. } );
  58. let eventRefired = false;
  59. clipboard.on( 'inputTransformation', ( evt, data ) => {
  60. if ( !eventRefired ) {
  61. eventRefired = true;
  62. evt.stop();
  63. clipboard.fire( 'inputTransformation', data );
  64. }
  65. expect( data.pasteFromOfficeProcessed ).to.true;
  66. expect( normalizeSpy.calledOnce ).to.true;
  67. }, { priority: 'low' } );
  68. editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', { content, dataTransfer } );
  69. expect( normalizeSpy.calledOnce ).to.true;
  70. } );
  71. } );
  72. describe( '_normalizeGoogleDocsInput()', () => {
  73. before( () => {
  74. content = new DocumentFragment();
  75. } );
  76. beforeEach( () => {
  77. return VirtualTestEditor
  78. .create( {
  79. plugins: [ Clipboard, PasteFromOffice ]
  80. } )
  81. .then( newEditor => {
  82. editor = newEditor;
  83. normalizeSpy = testUtils.sinon.spy( PasteFromOffice, '_normalizeGoogleDocsInput' );
  84. } );
  85. } );
  86. afterEach( () => {
  87. editor.destroy();
  88. } );
  89. it( 'runs normalizations if Google docs meta tag detected #1', () => {
  90. const fakeClipboardData = '<meta charset=\'utf-8\'><meta charset="utf-8"><b style="font-weight:normal;"' +
  91. ' id="docs-internal-guid-45309eee-7fff-33a3-6dbd-a7e55da535b5"></b>';
  92. const dataTransfer = createDataTransfer( {
  93. 'text/html': fakeClipboardData
  94. } );
  95. const htmlDataProcessor = new HtmlDataProcessor();
  96. const content = htmlDataProcessor.toView( fakeClipboardData );
  97. editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', { content, dataTransfer } );
  98. expect( normalizeSpy.calledOnce ).to.true;
  99. } );
  100. } );
  101. describe( '_getInputType()', () => {
  102. [
  103. {
  104. input: '<html><head><meta name="Generator" content=Microsoft Word 15></head></html>',
  105. output: 'msword'
  106. },
  107. {
  108. input: '<meta name=Generator content="Microsoft Word 15"></meta>',
  109. output: 'msword'
  110. },
  111. {
  112. input: '<meta name=Generator content="Other"></meta>',
  113. output: null
  114. },
  115. {
  116. input: '<meta charset=\'utf-8\'><meta charset="utf-8"><b style="font-weight:normal;"' +
  117. ' id="docs-internal-guid-45309eee-7fff-33a3-6dbd-a7e55da535b5"></b>',
  118. output: 'gdocs'
  119. }
  120. ].forEach( ( value, index ) => {
  121. it( `should return proper input type for test case: #${ index }.`, () => {
  122. if ( value.output ) {
  123. expect( PasteFromOffice._getInputType( value.input ) ).to.equal( value.output );
  124. } else {
  125. expect( PasteFromOffice._getInputType( value.input ) ).to.be.null;
  126. }
  127. } );
  128. } );
  129. } );
  130. } );