contentnormalizer.js 5.6 KB

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