googledocs.js 3.2 KB

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