googledocs.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 { googleDocsNormalizer as normalizer } from '../../src/normalizers/googledocs';
  6. import ContentNormalizer from '../../src/contentnormalizer';
  7. import { createDataTransfer } from '../_utils/utils';
  8. import DocumentFragment from '@ckeditor/ckeditor5-engine/src/view/documentfragment';
  9. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  10. // Functionality of the google docs normalizer is tested with autogenerated normalization tests.
  11. describe( 'PasteFromOffice/normalizers/googledocs', () => {
  12. const htmlDataProcessor = new HtmlDataProcessor();
  13. it( 'should be instance of content normalizers', () => {
  14. expect( normalizer ).to.be.instanceOf( ContentNormalizer );
  15. } );
  16. it( 'should mark data as processed', () => {
  17. const gDocs = '<p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab"></p>';
  18. const data = {
  19. dataTransfer: createDataTransfer( {
  20. 'text/html': gDocs
  21. } ),
  22. content: htmlDataProcessor.toView( gDocs )
  23. };
  24. normalizer.transform( data );
  25. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  26. } );
  27. it( 'should not mark non-google-docs data as processed', () => {
  28. const data = {
  29. dataTransfer: createDataTransfer( { 'text/html': 'foo bar' } )
  30. };
  31. normalizer.transform( data );
  32. expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
  33. } );
  34. it( 'outputs view#documentFragment', () => {
  35. const gDocs = '<p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab">Foo bar</p>';
  36. const data = {
  37. dataTransfer: createDataTransfer( {
  38. 'text/html': gDocs
  39. } ),
  40. content: htmlDataProcessor.toView( gDocs )
  41. };
  42. normalizer.transform( data );
  43. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  44. expect( data.content ).to.be.instanceOf( DocumentFragment );
  45. } );
  46. describe( 'activation trigger', () => {
  47. describe( 'correct markup', () => {
  48. [
  49. {
  50. 'text/html': '<meta charset="utf-8"><p id="docs-internal-guid-12345678-1234-1234-1234-1234567890ab">Foo bar</p>'
  51. },
  52. {
  53. // eslint-disable-next-line max-len
  54. 'text/html': '<meta charset="utf-8"><b style="font-weight:normal;" id="docs-internal-guid-30db46f5-7fff-15a1-e17c-1234567890ab"></b>'
  55. }
  56. ].forEach( ( html, index ) => {
  57. it( `should be active for markup #${ index }`, () => {
  58. const data = {
  59. dataTransfer: createDataTransfer( html ),
  60. content: htmlDataProcessor.toView( html[ 'text/html' ] )
  61. };
  62. expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
  63. normalizer.transform( data );
  64. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  65. } );
  66. } );
  67. } );
  68. describe( 'wrong markup', () => {
  69. [
  70. {
  71. 'text/html': '<p id="random_id">Hello world</p>'
  72. },
  73. {
  74. 'text/html': '<meta name=Generator content="Microsoft Word 15">'
  75. }
  76. ].forEach( ( html, index ) => {
  77. it( `should be not active for wrong markup #${ index }`, () => {
  78. const data = {
  79. dataTransfer: createDataTransfer( html ),
  80. content: htmlDataProcessor.toView( html[ 'text/html' ] )
  81. };
  82. expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
  83. normalizer.transform( data );
  84. expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
  85. } );
  86. } );
  87. } );
  88. } );
  89. } );