viewconversiondispatcher.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ViewConversionDispatcher from '../../src/conversion/viewconversiondispatcher';
  6. import ViewContainerElement from '../../src/view/containerelement';
  7. import ViewAttributeElement from '../../src/view/attributeelement';
  8. import ViewDocumentFragment from '../../src/view/documentfragment';
  9. import ViewText from '../../src/view/text';
  10. describe( 'ViewConversionDispatcher', () => {
  11. describe( 'constructor()', () => {
  12. it( 'should create ViewConversionDispatcher with passed api', () => {
  13. const apiObj = {};
  14. const dispatcher = new ViewConversionDispatcher( { apiObj } );
  15. expect( dispatcher.conversionApi.apiObj ).to.equal( apiObj );
  16. expect( dispatcher.conversionApi ).to.have.property( 'convertItem' ).that.is.instanceof( Function );
  17. expect( dispatcher.conversionApi ).to.have.property( 'convertChildren' ).that.is.instanceof( Function );
  18. } );
  19. } );
  20. describe( 'convert', () => {
  21. let dispatcher;
  22. beforeEach( () => {
  23. dispatcher = new ViewConversionDispatcher();
  24. } );
  25. it( 'should fire viewCleanup event on converted view part', () => {
  26. sinon.spy( dispatcher, 'fire' );
  27. const viewP = new ViewContainerElement( 'p' );
  28. dispatcher.convert( viewP );
  29. expect( dispatcher.fire.calledWith( 'viewCleanup', viewP ) ).to.be.true;
  30. } );
  31. it( 'should fire proper events', () => {
  32. const viewText = new ViewText( 'foobar' );
  33. const viewElement = new ViewContainerElement( 'p', null, viewText );
  34. const viewFragment = new ViewDocumentFragment( viewElement );
  35. sinon.spy( dispatcher, 'fire' );
  36. dispatcher.convert( viewText );
  37. dispatcher.convert( viewElement );
  38. dispatcher.convert( viewFragment );
  39. expect( dispatcher.fire.calledWith( 'text' ) ).to.be.true;
  40. expect( dispatcher.fire.calledWith( 'element:p' ) ).to.be.true;
  41. expect( dispatcher.fire.calledWith( 'documentFragment' ) ).to.be.true;
  42. } );
  43. it( 'should convert ViewText', () => {
  44. const viewText = new ViewText( 'foobar' );
  45. dispatcher.on( 'text', ( evt, data, consumable, conversionApi ) => {
  46. const result = {
  47. eventName: evt.name,
  48. input: data.input,
  49. // Check whether additional data has been passed.
  50. foo: data.foo
  51. };
  52. // Check whether consumable has appropriate value to consume.
  53. expect( consumable.consume( data.input ) ).to.be.true;
  54. // Check whether conversionApi of `dispatcher` has been passed.
  55. expect( conversionApi ).to.equal( dispatcher.conversionApi );
  56. // Set conversion result to `output` property of `data`.
  57. // Later we will check if it was returned by `convert` method.
  58. data.output = result;
  59. } );
  60. // Use `additionalData` parameter to check if it was passed to the event.
  61. const result = dispatcher.convert( viewText, { foo: 'bar' } );
  62. // Check conversion result.
  63. expect( result ).to.deep.equal( {
  64. eventName: 'text',
  65. input: viewText,
  66. foo: 'bar'
  67. } );
  68. } );
  69. it( 'should convert ViewContainerElement', () => {
  70. const viewElement = new ViewContainerElement( 'p', { attrKey: 'attrValue' } );
  71. dispatcher.on( 'element', ( evt, data, consumable, conversionApi ) => {
  72. const result = {
  73. eventName: evt.name,
  74. input: data.input,
  75. // Check whether additional data has been passed.
  76. foo: data.foo
  77. };
  78. // Check whether consumable has appropriate value to consume.
  79. expect( consumable.consume( data.input, { name: true } ) ).to.be.true;
  80. expect( consumable.consume( data.input, { attribute: 'attrKey' } ) ).to.be.true;
  81. // Check whether conversionApi of `dispatcher` has been passed.
  82. expect( conversionApi ).to.equal( dispatcher.conversionApi );
  83. // Set conversion result to `output` property of `data`.
  84. // Later we will check if it was returned by `convert` method.
  85. data.output = result;
  86. } );
  87. // Use `additionalData` parameter to check if it was passed to the event.
  88. const result = dispatcher.convert( viewElement, { foo: 'bar' } );
  89. // Check conversion result.
  90. expect( result ).to.deep.equal( {
  91. eventName: 'element:p',
  92. input: viewElement,
  93. foo: 'bar'
  94. } );
  95. } );
  96. it( 'should convert ViewDocumentFragment', () => {
  97. const viewFragment = new ViewDocumentFragment();
  98. dispatcher.on( 'documentFragment', ( evt, data, consumable, conversionApi ) => {
  99. const result = {
  100. eventName: evt.name,
  101. input: data.input,
  102. // Check whether additional data has been passed.
  103. foo: data.foo
  104. };
  105. // Check whether consumable has appropriate value to consume.
  106. expect( consumable.consume( data.input ) ).to.be.true;
  107. // Check whether conversionApi of `dispatcher` has been passed.
  108. expect( conversionApi ).to.equal( dispatcher.conversionApi );
  109. // Set conversion result to `output` property of `data`.
  110. // Later we will check if it was returned by `convert` method.
  111. data.output = result;
  112. } );
  113. // Use `additionalData` parameter to check if it was passed to the event.
  114. const result = dispatcher.convert( viewFragment, { foo: 'bar' } );
  115. // Check conversion result.
  116. expect( result ).to.deep.equal( {
  117. eventName: 'documentFragment',
  118. input: viewFragment,
  119. foo: 'bar'
  120. } );
  121. } );
  122. } );
  123. describe( 'conversionApi#convertItem', () => {
  124. it( 'should convert view elements and view text', () => {
  125. const dispatcher = new ViewConversionDispatcher();
  126. const viewFragment = new ViewDocumentFragment( [
  127. new ViewContainerElement( 'p' ), new ViewText( 'foobar' )
  128. ] );
  129. dispatcher.on( 'text', ( evt, data ) => {
  130. data.output = { text: data.input.data };
  131. } );
  132. dispatcher.on( 'element:p', ( evt, data ) => {
  133. data.output = { name: 'p' };
  134. } );
  135. dispatcher.on( 'documentFragment', ( evt, data, consumable, conversionApi ) => {
  136. data.output = [];
  137. for ( let child of data.input.getChildren() ) {
  138. data.output.push( conversionApi.convertItem( child ) );
  139. }
  140. } );
  141. const result = dispatcher.convert( viewFragment );
  142. expect( result ).to.deep.equal( [
  143. { name: 'p' },
  144. { text: 'foobar' }
  145. ] );
  146. } );
  147. } );
  148. describe( 'conversionApi#convertChildren', () => {
  149. it( 'should fire proper events for all children of passed view part', () => {
  150. const dispatcher = new ViewConversionDispatcher();
  151. const viewFragment = new ViewDocumentFragment( [
  152. new ViewContainerElement( 'p' ), new ViewText( 'foobar' )
  153. ] );
  154. dispatcher.on( 'text', ( evt, data ) => {
  155. data.output = { text: data.input.data };
  156. } );
  157. dispatcher.on( 'element:p', ( evt, data ) => {
  158. data.output = { name: 'p' };
  159. } );
  160. dispatcher.on( 'documentFragment', ( evt, data, consumable, conversionApi ) => {
  161. data.output = conversionApi.convertChildren( data.input );
  162. } );
  163. const result = dispatcher.convert( viewFragment );
  164. expect( result ).to.deep.equal( [
  165. { name: 'p' },
  166. { text: 'foobar' }
  167. ] );
  168. } );
  169. it( 'should flatten structure of non-converted elements', () => {
  170. const dispatcher = new ViewConversionDispatcher();
  171. dispatcher.on( 'text', ( evt, data ) => {
  172. data.output = data.input.data;
  173. } );
  174. dispatcher.on( 'element', ( evt, data, consumable, conversionApi ) => {
  175. data.output = conversionApi.convertChildren( data.input, consumable );
  176. } );
  177. const viewStructure = new ViewContainerElement( 'div', null, [
  178. new ViewContainerElement( 'p', null, [
  179. new ViewContainerElement( 'span', { class: 'nice' }, [
  180. new ViewAttributeElement( 'a', { href: 'foo.html' }, new ViewText( 'foo' ) ),
  181. new ViewText( ' bar ' ),
  182. new ViewAttributeElement( 'i', null, new ViewText( 'xyz' ) )
  183. ] )
  184. ] ),
  185. new ViewContainerElement( 'p', null, [
  186. new ViewAttributeElement( 'strong', null, [
  187. new ViewText( 'aaa ' ),
  188. new ViewAttributeElement( 'span', null, new ViewText( 'bbb' ) ),
  189. new ViewText( ' ' ),
  190. new ViewAttributeElement( 'a', { href: 'bar.html' }, new ViewText( 'ccc' ) )
  191. ] )
  192. ] )
  193. ] );
  194. expect( dispatcher.convert( viewStructure ) ).to.deep.equal( [ 'foo', ' bar ', 'xyz', 'aaa ', 'bbb', ' ', 'ccc' ] );
  195. } );
  196. } );
  197. } );