viewconversiondispatcher.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/conversion/viewconversiondispatcher
  7. */
  8. import ViewConsumable from './viewconsumable.js';
  9. import ViewElement from '../view/element.js';
  10. import ViewText from '../view/text.js';
  11. import EmitterMixin from '../../utils/emittermixin.js';
  12. import mix from '../../utils/mix.js';
  13. import extend from '../../utils/lib/lodash/extend.js';
  14. /**
  15. * `ViewConversionDispatcher` is a central point of {@link module:engine/view/view view} conversion, which is a process of
  16. * converting given {@link module:engine/view/documentfragment~DocumentFragment view document fragment} or
  17. * {@link module:engine/view/element~Element}
  18. * into another structure. In default application, {@link module:engine/view/view view} is converted to {@link module:engine/model/model}.
  19. *
  20. * During conversion process, for all {@link module:engine/view/node~Node view nodes} from the converted view document fragment,
  21. * `ViewConversionDispatcher` fires corresponding events. Special callbacks called "converters" should listen to
  22. * `ViewConversionDispatcher` for those events.
  23. *
  24. * Each callback, as a first argument, is passed a special object `data` that has `input` and `output` properties.
  25. * `input` property contains {@link module:engine/view/node~Node view node} or
  26. * {@link module:engine/view/documentfragment~DocumentFragment view document fragment}
  27. * that is converted at the moment and might be handled by the callback. `output` property should be used to save the result
  28. * of conversion. Keep in mind that the `data` parameter is customizable and may contain other values - see
  29. * {@link ~ViewConversionDispatcher#convert}. It is also shared by reference by all callbacks
  30. * listening to given event. **Note**: in view to model conversion - `data` contains `context` property that is an array
  31. * of {@link module:engine/model/element~Element model elements}. These are model elements that will be the parent of currently
  32. * converted view item. `context` property is used in examples below.
  33. *
  34. * The second parameter passed to a callback is an instance of {@link module:engine/conversion/viewconsumable~ViewConsumable}. It stores
  35. * information about what parts of processed view item are still waiting to be handled. After a piece of view item
  36. * was converted, appropriate consumable value should be {@link module:engine/conversion/viewconsumable~ViewConsumable#consume consumed}.
  37. *
  38. * The third parameter passed to a callback is an instance of {@link ~ViewConversionDispatcher}
  39. * which provides additional tools for converters.
  40. *
  41. * Examples of providing callbacks for `ViewConversionDispatcher`:
  42. *
  43. * // Converter for paragraphs (<p>).
  44. * viewDispatcher.on( 'element:p', ( evt, data, consumable, conversionApi ) => {
  45. * const paragraph = new ModelElement( 'paragraph' );
  46. * const schemaQuery = {
  47. * name: 'paragraph',
  48. * inside: data.context
  49. * };
  50. *
  51. * if ( conversionApi.schema.check( schemaQuery ) ) {
  52. * if ( !consumable.consume( data.input, { name: true } ) ) {
  53. * // Before converting this paragraph's children we have to update their context by this paragraph.
  54. * data.context.push( paragraph );
  55. * const children = conversionApi.convertChildren( data.input, consumable, data );
  56. * data.context.pop();
  57. * paragraph.appendChildren( children );
  58. * data.output = paragraph;
  59. * }
  60. * }
  61. * } );
  62. *
  63. * // Converter for links (<a>).
  64. * viewDispatcher.on( 'element:a', ( evt, data, consumable, conversionApi ) => {
  65. * if ( consumable.consume( data.input, { name: true, attributes: [ 'href' ] } ) ) {
  66. * // <a> element is inline and is represented by an attribute in the model.
  67. * // This is why we are not updating `context` property.
  68. * data.output = conversionApi.convertChildren( data.input, consumable, data );
  69. *
  70. * for ( let item of Range.createFrom( data.output ) ) {
  71. * const schemaQuery = {
  72. * name: item.name || '$text',
  73. * attribute: 'link',
  74. * inside: data.context
  75. * };
  76. *
  77. * if ( conversionApi.schema.check( schemaQuery ) ) {
  78. * item.setAttribute( 'link', data.input.getAttribute( 'href' ) );
  79. * }
  80. * }
  81. * }
  82. * } );
  83. *
  84. * // Fire conversion.
  85. * // Always take care where the converted model structure will be appended to. If this `viewDocumentFragment`
  86. * // is going to be appended directly to a '$root' element, use that in `context`.
  87. * viewDispatcher.convert( viewDocumentFragment, { context: [ '$root' ] } );
  88. *
  89. * Before each conversion process, `ViewConversionDispatcher` fires {@link ~ViewConversionDispatcher#event:viewCleanup}
  90. * event which can be used to prepare tree view for conversion.
  91. *
  92. * @mixes module:utils/emittermixin~EmitterMixin
  93. * @fires viewCleanup
  94. * @fires element
  95. * @fires text
  96. * @fires documentFragment
  97. */
  98. export default class ViewConversionDispatcher {
  99. /**
  100. * Creates a `ViewConversionDispatcher` that operates using passed API.
  101. *
  102. * @see module:engine/conversion/viewconversiondispatcher~ViewConversionApi
  103. * @param {Object} [conversionApi] Additional properties for interface that will be passed to events fired
  104. * by `ViewConversionDispatcher`.
  105. */
  106. constructor( conversionApi = {} ) {
  107. /**
  108. * Interface passed by dispatcher to the events callbacks.
  109. *
  110. * @member {module:engine/conversion/viewconversiondispatcher~ViewConversionApi}
  111. */
  112. this.conversionApi = extend( {}, conversionApi );
  113. // `convertItem` and `convertChildren` are bound to this `ViewConversionDispatcher` instance and
  114. // set on `conversionApi`. This way only a part of `ViewConversionDispatcher` API is exposed.
  115. this.conversionApi.convertItem = this._convertItem.bind( this );
  116. this.conversionApi.convertChildren = this._convertChildren.bind( this );
  117. }
  118. /**
  119. * Starts the conversion process. The entry point for the conversion.
  120. *
  121. * @fires element
  122. * @fires text
  123. * @fires documentFragment
  124. * @param {module:engine/view/documentfragment~DocumentFragment|module:engine/view/element~Element}
  125. * viewItem Part of the view to be converted.
  126. * @param {Object} [additionalData] Additional data to be passed in `data` argument when firing `ViewConversionDispatcher`
  127. * events. See also {@link ~ViewConversionDispatcher#event:element element event}.
  128. * @returns {module:engine/model/documentfragment~DocumentFragment} Model document fragment that is a result of the conversion process.
  129. */
  130. convert( viewItem, additionalData = {} ) {
  131. this.fire( 'viewCleanup', viewItem );
  132. const consumable = ViewConsumable.createFrom( viewItem );
  133. return this._convertItem( viewItem, consumable, additionalData );
  134. }
  135. /**
  136. * @private
  137. * @see module:engine/conversion/viewconversiondispatcher~ViewConversionApi#convertItem
  138. */
  139. _convertItem( input, consumable, additionalData = {} ) {
  140. const data = extend( {}, additionalData, {
  141. input: input,
  142. output: null
  143. } );
  144. if ( input instanceof ViewElement ) {
  145. this.fire( 'element:' + input.name, data, consumable, this.conversionApi );
  146. } else if ( input instanceof ViewText ) {
  147. this.fire( 'text', data, consumable, this.conversionApi );
  148. } else {
  149. this.fire( 'documentFragment', data, consumable, this.conversionApi );
  150. }
  151. return data.output;
  152. }
  153. /**
  154. * @private
  155. * @see module:engine/conversion/viewconversiondispatcher~ViewConversionApi#convertChildren
  156. */
  157. _convertChildren( input, consumable, additionalData = {} ) {
  158. const viewChildren = Array.from( input.getChildren() );
  159. const convertedChildren = viewChildren.map( ( viewChild ) => this._convertItem( viewChild, consumable, additionalData ) );
  160. // Flatten and remove nulls.
  161. return convertedChildren.reduce( ( a, b ) => b ? a.concat( b ) : a, [] );
  162. }
  163. /**
  164. * Fired before the first conversion event, at the beginning of view to model conversion process.
  165. *
  166. * @event viewCleanup
  167. * @param {module:engine/view/documentfragment~DocumentFragment|module:engine/view/element~Element}
  168. * viewItem Part of the view to be converted.
  169. */
  170. /**
  171. * Fired when {@link module:engine/view/element~Element} is converted.
  172. *
  173. * `element` is a namespace event for a class of events. Names of actually called events follow this pattern:
  174. * `element:<elementName>` where `elementName` is the name of converted element. This way listeners may listen to
  175. * all elements conversion or to conversion of specific elements.
  176. *
  177. * @event element
  178. * @param {Object} data Object containing conversion input and a placeholder for conversion output and possibly other
  179. * values (see {@link #convert}).
  180. * Keep in mind that this object is shared by reference between all callbacks that will be called.
  181. * This means that callbacks can add their own values if needed,
  182. * and those values will be available in other callbacks.
  183. * @param {module:engine/view/element~Element} data.input Converted element.
  184. * @param {*} data.output The current state of conversion result. Every change to converted element should
  185. * be reflected by setting or modifying this property.
  186. * @param {module:engine/model/schema~SchemaPath} data.context The conversion context.
  187. * @param {module:engine/conversion/viewconsumable~ViewConsumable} consumable Values to consume.
  188. * @param {Object} conversionApi Conversion interface to be used by callback, passed in `ViewConversionDispatcher` constructor.
  189. * Besides of properties passed in constructor, it also has `convertItem` and `convertChildren` methods which are references
  190. * to {@link #_convertItem} and
  191. * {@link ~ViewConversionDispatcher#_convertChildren}. Those methods are needed to convert
  192. * the whole view-tree they were exposed in `conversionApi` for callbacks.
  193. */
  194. /**
  195. * Fired when {@link module:engine/view/text~Text} is converted.
  196. *
  197. * @event text
  198. * @see #event:element
  199. */
  200. /**
  201. * Fired when {@link module:engine/view/documentfragment~DocumentFragment} is converted.
  202. *
  203. * @event documentFragment
  204. * @see #event:element
  205. */
  206. }
  207. mix( ViewConversionDispatcher, EmitterMixin );
  208. /**
  209. * Conversion interface that is registered for given {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher}
  210. * and is passed as one of parameters when {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher dispatcher}
  211. * fires it's events.
  212. *
  213. * `ViewConversionApi` object is built by {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher} constructor.
  214. * The exact list of properties of this object is determined by the object passed to the constructor.
  215. *
  216. * @interface ViewConversionApi
  217. */
  218. /**
  219. * Starts conversion of given item by firing an appropriate event.
  220. *
  221. * Every fired event is passed (as first parameter) an object with `output` property. Every event may set and/or
  222. * modify that property. When all callbacks are done, the final value of `output` property is returned by this method.
  223. *
  224. * @method #convertItem
  225. * @fires module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#event:element
  226. * @fires module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#event:text
  227. * @fires module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#event:documentFragment
  228. * @param {module:engine/view/documentfragment~DocumentFragment|module:engine/view/element~Element|module:engine/view/text~Text}
  229. * input Item to convert.
  230. * @param {module:engine/conversion/viewconsumable~ViewConsumable} consumable Values to consume.
  231. * @param {Object} [additionalData] Additional data to be passed in `data` argument when firing `ViewConversionDispatcher`
  232. * events. See also {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#event:element element event}.
  233. * @returns {*} The result of item conversion, created and modified by callbacks attached to fired event.
  234. */
  235. /**
  236. * Starts conversion of all children of given item by firing appropriate events for all those children.
  237. *
  238. * @method #convertChildren
  239. * @fires module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#event:element
  240. * @fires module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#event:text
  241. * @fires module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#event:documentFragment
  242. * @param {module:engine/view/documentfragment~DocumentFragment|module:engine/view/element~Element}
  243. * input Item which children will be converted.
  244. * @param {module:engine/conversion/viewconsumable~ViewConsumable} consumable Values to consume.
  245. * @param {Object} [additionalData] Additional data to be passed in `data` argument when firing `ViewConversionDispatcher`
  246. * events. See also {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#event:element element event}.
  247. * @returns {Array.<*>} Array containing results of conversion of all children of given item.
  248. */