contentnormalizer.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  6. import ContentNormalizer from '../src/contentnormalizer';
  7. import { createDataTransfer } from './_utils/utils';
  8. describe( 'ContentNormalizer', () => {
  9. let normalizer, sinonTrigger;
  10. const templateData = {
  11. dataTransfer: createDataTransfer( {
  12. 'text/html': 'test data'
  13. } )
  14. };
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. sinonTrigger = sinon.fake.returns( true );
  18. normalizer = new ContentNormalizer( {
  19. activationTrigger: sinonTrigger
  20. } );
  21. } );
  22. describe( 'constructor()', () => {
  23. it( 'should be not active at start', () => {
  24. expect( normalizer.isActive ).to.be.false;
  25. } );
  26. it( 'should not have assigned data', () => {
  27. expect( normalizer.data ).to.be.null;
  28. } );
  29. it( 'should have assigned activation trigger', () => {
  30. expect( normalizer.activationTrigger ).to.be.a( 'function' );
  31. expect( normalizer.activationTrigger ).to.equal( sinonTrigger );
  32. } );
  33. it( 'should initialize sets for filters', () => {
  34. expect( normalizer._filters ).to.be.a( 'set' );
  35. expect( normalizer._fullContentFilters ).to.be.a( 'set' );
  36. } );
  37. } );
  38. describe( 'setInputData()', () => {
  39. let data;
  40. beforeEach( () => {
  41. data = Object.assign( {}, templateData );
  42. } );
  43. describe( 'trigger activated', () => {
  44. beforeEach( () => {
  45. normalizer.setInputData( data );
  46. } );
  47. it( 'should set data', () => {
  48. expect( normalizer.data ).to.equal = data;
  49. } );
  50. it( 'should check if activates normalizer', () => {
  51. sinon.assert.calledOnce( sinonTrigger );
  52. sinon.assert.calledWith( sinonTrigger, 'test data' );
  53. expect( normalizer.isActive ).to.be.true;
  54. } );
  55. it( 'should add flag to data processed by paste from office plugin', () => {
  56. expect( data.isTransformedWithPasteFromOffice ).to.be.false;
  57. } );
  58. } );
  59. describe( 'trigger not activated', () => {
  60. beforeEach( () => {
  61. sinonTrigger = sinon.fake.returns( false );
  62. normalizer = new ContentNormalizer( {
  63. activationTrigger: sinonTrigger
  64. } );
  65. normalizer.transform( data );
  66. } );
  67. it( 'should not be active', () => {
  68. sinon.assert.calledOnce( sinonTrigger );
  69. sinon.assert.calledWith( sinonTrigger, 'test data' );
  70. expect( normalizer.isActive ).to.be.false;
  71. } );
  72. it( 'should not keep reference to data when is not active', () => {
  73. expect( normalizer.data ).to.be.null;
  74. } );
  75. it( 'should not add flag to not processed data', () => {
  76. expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
  77. } );
  78. } );
  79. describe( 'already processed data', () => {
  80. beforeEach( () => {
  81. data.isTransformedWithPasteFromOffice = true;
  82. normalizer.addFilter( {
  83. fullContent: true,
  84. // eslint-disable-next-line no-unused-vars
  85. exec: d => { d = {}; }
  86. } );
  87. } );
  88. it( 'should not change data', () => {
  89. normalizer.setInputData( data );
  90. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  91. expect( data ).to.deep.include( templateData );
  92. } );
  93. } );
  94. } );
  95. describe( 'addFilter()', () => {
  96. let filter;
  97. describe( 'fullContentFilters', () => {
  98. beforeEach( () => {
  99. filter = {
  100. fullContent: true,
  101. exec: () => {}
  102. };
  103. normalizer.addFilter( filter );
  104. } );
  105. it( 'should add filter to fullContentFilters set', () => {
  106. expect( normalizer._fullContentFilters.size ).to.equal( 1 );
  107. expect( normalizer._filters.size ).to.equal( 0 );
  108. const firstFilter = [ ...normalizer._fullContentFilters ][ 0 ];
  109. expect( firstFilter ).to.equal( filter );
  110. } );
  111. } );
  112. } );
  113. describe( 'exec()', () => {
  114. let data, filter;
  115. beforeEach( () => {
  116. data = Object.assign( {}, templateData );
  117. filter = {
  118. fullContent: true,
  119. exec: data => {
  120. data.content = 'Foo bar baz.';
  121. }
  122. };
  123. normalizer.addFilter( filter );
  124. } );
  125. it( 'should apply filter#exec to data', () => {
  126. normalizer.setInputData( data );
  127. expect( data.content ).to.be.undefined;
  128. normalizer.exec();
  129. expect( data.content ).to.equal( 'Foo bar baz.' );
  130. } );
  131. it( 'should mark data as processed with paste from office', () => {
  132. normalizer.setInputData( data );
  133. expect( data.isTransformedWithPasteFromOffice ).to.be.false;
  134. normalizer.exec();
  135. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  136. } );
  137. describe( 'already processed data', () => {
  138. let execFake;
  139. beforeEach( () => {
  140. execFake = sinon.fake();
  141. normalizer = new ContentNormalizer( { activationTrigger: () => true } );
  142. normalizer.addFilter( {
  143. fullContent: true,
  144. exec: execFake
  145. } );
  146. } );
  147. it( 'should not apply filter on already processed data', () => {
  148. normalizer.setInputData( data );
  149. sinon.assert.notCalled( execFake );
  150. expect( data.isTransformedWithPasteFromOffice ).to.be.false;
  151. normalizer.exec();
  152. sinon.assert.calledOnce( execFake );
  153. sinon.assert.calledWith( execFake, data );
  154. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  155. normalizer.exec();
  156. sinon.assert.calledOnce( execFake );
  157. } );
  158. } );
  159. describe( 'normalizer without filter', () => {
  160. beforeEach( () => {
  161. normalizer = new ContentNormalizer( { activationTrigger: () => true } );
  162. } );
  163. it( 'should do nothing with data', () => {
  164. normalizer.setInputData( data );
  165. expect( data ).to.deep.include( templateData );
  166. expect( data.isTransformedWithPasteFromOffice ).to.be.false;
  167. normalizer.exec();
  168. expect( data ).to.deep.include( templateData );
  169. expect( data.isTransformedWithPasteFromOffice ).to.be.true;
  170. } );
  171. } );
  172. } );
  173. } );