viewconversiondispatcher.js 8.1 KB

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