conversion.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/conversion/conversion
  7. */
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import UpcastHelpers from './upcasthelpers';
  10. import DowncastHelpers from './downcasthelpers';
  11. /**
  12. * A utility class that helps add converters to upcast and downcast dispatchers.
  13. *
  14. * We recommend reading the {@glink framework/guides/architecture/editing-engine Editing engine architecture} guide first to
  15. * understand the core concepts of the conversion mechanisms.
  16. *
  17. * An instance of the conversion manager is available in the
  18. * {@link module:core/editor/editor~Editor#conversion `editor.conversion`} property
  19. * and by default has the following groups of dispatchers (i.e. directions of conversion):
  20. *
  21. * * `downcast` (editing and data downcasts)
  22. * * `editingDowncast`
  23. * * `dataDowncast`
  24. * * `upcast`
  25. *
  26. * # One-way converters
  27. *
  28. * To add a converter to a specific group, use the {@link module:engine/conversion/conversion~Conversion#for `for()`}
  29. * method:
  30. *
  31. * // Add a converter to editing downcast and data downcast.
  32. * editor.conversion.for( 'downcast' ).elementToElement( config ) );
  33. *
  34. * // Add a converter to the data pipepline only:
  35. * editor.conversion.for( 'dataDowncast' ).elementToElement( dataConversionConfig ) );
  36. *
  37. * // And a slightly different one for the editing pipeline:
  38. * editor.conversion.for( 'editingDowncast' ).elementToElement( editingConversionConfig ) );
  39. *
  40. * See {@link module:engine/conversion/conversion~Conversion#for `for()`} method documentation to learn more about
  41. * available conversion helpers and how to use your custom ones.
  42. *
  43. * # Two-way converters
  44. *
  45. * Besides using one-way converters via the `for()` method, you can also use other methods available in this
  46. * class to add two-way converters (upcast and downcast):
  47. *
  48. * * {@link module:engine/conversion/conversion~Conversion#elementToElement `elementToElement()`} –
  49. * Model element to view element and vice versa.
  50. * * {@link module:engine/conversion/conversion~Conversion#attributeToElement `attributeToElement()`} –
  51. * Model attribute to view element and vice versa.
  52. * * {@link module:engine/conversion/conversion~Conversion#attributeToAttribute `attributeToAttribute()`} –
  53. * Model attribute to view element and vice versa.
  54. */
  55. export default class Conversion {
  56. /**
  57. * Creates a new conversion instance.
  58. *
  59. * @param {module:engine/conversion/downcastdispatcher~DowncastDispatcher|
  60. * Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher>} downcastDispatchers
  61. * @param {module:engine/conversion/upcastdispatcher~UpcastDispatcher|
  62. * Array.<module:engine/conversion/upcastdispatcher~UpcastDispatcher>} upcastDispatchers
  63. */
  64. constructor( downcastDispatchers, upcastDispatchers ) {
  65. /**
  66. * Maps dispatchers group name to ConversionHelpers instances.
  67. *
  68. * @private
  69. * @member {Map.<String,module:engine/conversion/conversionhelpers~ConversionHelpers>}
  70. */
  71. this._helpers = new Map();
  72. // Define default 'downcast' & 'upcast' dispatchers groups. Those groups are always available as two-way converters needs them.
  73. this._downcast = Array.isArray( downcastDispatchers ) ? downcastDispatchers : [ downcastDispatchers ];
  74. this._createConversionHelpers( { name: 'downcast', dispatchers: this._downcast, isDowncast: true } );
  75. this._upcast = Array.isArray( upcastDispatchers ) ? upcastDispatchers : [ upcastDispatchers ];
  76. this._createConversionHelpers( { name: 'upcast', dispatchers: this._upcast, isDowncast: false } );
  77. }
  78. /**
  79. * Define an alias for registered dispatcher.
  80. *
  81. * const conversion = new Conversion(
  82. * [ dataDowncastDispatcher, editingDowncastDispatcher ],
  83. * upcastDispatcher
  84. * );
  85. *
  86. * conversion.addAlias( 'dataDowncast', dataDowncastDispatcher );
  87. *
  88. * @param {String} alias An alias of a dispatcher.
  89. * @param {module:engine/conversion/downcastdispatcher~DowncastDispatcher|
  90. * module:engine/conversion/upcastdispatcher~UpcastDispatcher} dispatcher Dispatcher which should have an alias.
  91. */
  92. addAlias( alias, dispatcher ) {
  93. const isDowncast = this._downcast.includes( dispatcher );
  94. const isUpcast = this._upcast.includes( dispatcher );
  95. if ( !isUpcast && !isDowncast ) {
  96. /**
  97. * Trying to register and alias for a dispatcher that nas not been registered.
  98. *
  99. * @error conversion-add-alias-dispatcher-not-registered
  100. */
  101. throw new CKEditorError( 'conversion-add-alias-dispatcher-not-registered: ' +
  102. 'Trying to register and alias for a dispatcher that nas not been registered.' );
  103. }
  104. this._createConversionHelpers( { name: alias, dispatchers: [ dispatcher ], isDowncast: isDowncast } );
  105. }
  106. /**
  107. * Provides a chainable API to assign converters to conversion dispatchers group.
  108. *
  109. * If the given group name has not been registered, the
  110. * {@link module:utils/ckeditorerror~CKEditorError `conversion-for-unknown-group` error} is thrown.
  111. *
  112. * You can use conversion helpers available directly in the `for()` chain or your custom ones via
  113. * the {@link module:engine/conversion/conversionhelpers~ConversionHelpers#add `add()`} method.
  114. *
  115. * # Using bulit-in conversion helpers
  116. *
  117. * The `for()` chain comes with a set of conversion helpers which you can use like this:
  118. *
  119. * editor.conversion.for( 'downcast' )
  120. * .elementToElement( config1 ) // Adds an element-to-element downcast converter.
  121. * .attributeToElement( config2 ); // Adds an attribute-to-element downcast converter.
  122. *
  123. * editor.conversion.for( 'upcast' )
  124. * .elementToAttribute( config3 ); // Adds an element-to-attribute upcast converter.
  125. *
  126. * Refer to the documentation of built-in conversion helpers to learn about their configuration options.
  127. *
  128. * * downcast (model-to-view) conversion helpers:
  129. *
  130. * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#elementToElement `elementToElement()`},
  131. * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToElement `attributeToElement()`},
  132. * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToAttribute `attributeToAttribute()`}.
  133. * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToElement `markerToElement()`}.
  134. * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToHighlight `markerToHighlight()`}.
  135. *
  136. * * upcast (view-to-model) conversion helpers:
  137. *
  138. * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToElement `elementToElement()`},
  139. * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToAttribute `elementToAttribute()`},
  140. * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#attributeToAttribute `attributeToAttribute()`}.
  141. * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToMarker `elementToMarker()`}.
  142. *
  143. * # Using custom conversion helpers
  144. *
  145. * If you need to implement a nontypical converter, you can do so by calling:
  146. *
  147. * editor.conversion.for( direction ).add( customHelper );
  148. *
  149. * The `.add()` method takes exactly one parameter, which is a function. This function should accept one parameter that
  150. * is a dispatcher instance. The function should add an actual converter to the passed dispatcher instance.
  151. *
  152. * Example:
  153. *
  154. * editor.conversion.for( 'upcast' ).add( dispatcher => {
  155. * dispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
  156. * // Do something with a view <a> element.
  157. * } );
  158. * } );
  159. *
  160. * Refer to the documentation of {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher}
  161. * and {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher} to learn how to write
  162. * custom converters.
  163. *
  164. * @param {String} groupName The name of dispatchers group to add the converters to.
  165. * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers}
  166. */
  167. for( groupName ) {
  168. if ( !this._helpers.has( groupName ) ) {
  169. /**
  170. * Trying to add a converter to an unknown dispatchers group.
  171. *
  172. * @error conversion-for-unknown-group
  173. */
  174. throw new CKEditorError( 'conversion-for-unknown-group: Trying to add a converter to an unknown dispatchers group.' );
  175. }
  176. return this._helpers.get( groupName );
  177. }
  178. /**
  179. * Sets up converters between the model and the view that convert a model element to a view element (and vice versa).
  180. * For example, the model `<paragraph>Foo</paragraph>` is `<p>Foo</p>` in the view.
  181. *
  182. * // A simple conversion from the `paragraph` model element to the `<p>` view element (and vice versa).
  183. * editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  184. *
  185. * // Override other converters by specifying a converter definition with a higher priority.
  186. * editor.conversion.elementToElement( { model: 'paragraph', view: 'div', converterPriority: 'high' } );
  187. *
  188. * // View specified as an object instead of a string.
  189. * editor.conversion.elementToElement( {
  190. * model: 'fancyParagraph',
  191. * view: {
  192. * name: 'p',
  193. * classes: 'fancy'
  194. * }
  195. * } );
  196. *
  197. * // Use `upcastAlso` to define other view elements that should also be converted to a `paragraph` element.
  198. * editor.conversion.elementToElement( {
  199. * model: 'paragraph',
  200. * view: 'p',
  201. * upcastAlso: [
  202. * 'div',
  203. * {
  204. * // Any element with the `display: block` style.
  205. * styles: {
  206. * display: 'block'
  207. * }
  208. * }
  209. * ]
  210. * } );
  211. *
  212. * // `upcastAlso` set as callback enables a conversion of a wide range of different view elements.
  213. * editor.conversion.elementToElement( {
  214. * model: 'heading',
  215. * view: 'h2',
  216. * // Convert "headling-like" paragraphs to headings.
  217. * upcastAlso: viewElement => {
  218. * const fontSize = viewElement.getStyle( 'font-size' );
  219. *
  220. * if ( !fontSize ) {
  221. * return null;
  222. * }
  223. *
  224. * const match = fontSize.match( /(\d+)\s*px/ );
  225. *
  226. * if ( !match ) {
  227. * return null;
  228. * }
  229. *
  230. * const size = Number( match[ 1 ] );
  231. *
  232. * if ( size > 26 ) {
  233. * // Returned value can be an object with the matched properties.
  234. * // These properties will be "consumed" during the conversion.
  235. * // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
  236. *
  237. * return { name: true, styles: [ 'font-size' ] };
  238. * }
  239. *
  240. * return null;
  241. * }
  242. * } );
  243. *
  244. * `definition.model` is a `String` with a model element name to convert from or to.
  245. * See {@link module:engine/conversion/conversion~ConverterDefinition} to learn about other parameters.
  246. *
  247. * @param {module:engine/conversion/conversion~ConverterDefinition} definition The converter definition.
  248. */
  249. elementToElement( definition ) {
  250. // Set up downcast converter.
  251. this.for( 'downcast' ).elementToElement( definition );
  252. // Set up upcast converter.
  253. for ( const { model, view } of _getAllUpcastDefinitions( definition ) ) {
  254. this.for( 'upcast' )
  255. .elementToElement( {
  256. model,
  257. view,
  258. converterPriority: definition.converterPriority
  259. } );
  260. }
  261. }
  262. /**
  263. * Sets up converters between the model and the view that convert a model attribute to a view element (and vice versa).
  264. * For example, a model text node with `"Foo"` as data and the `bold` attribute is `<strong>Foo</strong>` in the view.
  265. *
  266. * // A simple conversion from the `bold=true` attribute to the `<strong>` view element (and vice versa).
  267. * editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  268. *
  269. * // Override other converters by specifying a converter definition with a higher priority.
  270. * editor.conversion.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
  271. *
  272. * // View specified as an object instead of a string.
  273. * editor.conversion.attributeToElement( {
  274. * model: 'bold',
  275. * view: {
  276. * name: 'span',
  277. * classes: 'bold'
  278. * }
  279. * } );
  280. *
  281. * // Use `config.model.name` to define the conversion only from a given node type, `$text` in this case.
  282. * // The same attribute on different elements may then be handled by a different converter.
  283. * editor.conversion.attributeToElement( {
  284. * model: {
  285. * key: 'textDecoration',
  286. * values: [ 'underline', 'lineThrough' ],
  287. * name: '$text'
  288. * },
  289. * view: {
  290. * underline: {
  291. * name: 'span',
  292. * styles: {
  293. * 'text-decoration': 'underline'
  294. * }
  295. * },
  296. * lineThrough: {
  297. * name: 'span',
  298. * styles: {
  299. * 'text-decoration': 'line-through'
  300. * }
  301. * }
  302. * }
  303. * } );
  304. *
  305. * // Use `upcastAlso` to define other view elements that should also be converted to the `bold` attribute.
  306. * editor.conversion.attributeToElement( {
  307. * model: 'bold',
  308. * view: 'strong',
  309. * upcastAlso: [
  310. * 'b',
  311. * {
  312. * name: 'span',
  313. * classes: 'bold'
  314. * },
  315. * {
  316. * name: 'span',
  317. * styles: {
  318. * 'font-weight': 'bold'
  319. * }
  320. * },
  321. * viewElement => {
  322. * const fontWeight = viewElement.getStyle( 'font-weight' );
  323. *
  324. * if ( viewElement.is( 'span' ) && fontWeight && /\d+/.test() && Number( fontWeight ) > 500 ) {
  325. * // Returned value can be an object with the matched properties.
  326. * // These properties will be "consumed" during the conversion.
  327. * // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
  328. *
  329. * return {
  330. * name: true,
  331. * styles: [ 'font-weight' ]
  332. * };
  333. * }
  334. * }
  335. * ]
  336. * } );
  337. *
  338. * // Conversion from and to a model attribute key whose value is an enum (`fontSize=big|small`).
  339. * // `upcastAlso` set as callback enables a conversion of a wide range of different view elements.
  340. * editor.conversion.attributeToElement( {
  341. * model: {
  342. * key: 'fontSize',
  343. * values: [ 'big', 'small' ]
  344. * },
  345. * view: {
  346. * big: {
  347. * name: 'span',
  348. * styles: {
  349. * 'font-size': '1.2em'
  350. * }
  351. * },
  352. * small: {
  353. * name: 'span',
  354. * styles: {
  355. * 'font-size': '0.8em'
  356. * }
  357. * }
  358. * },
  359. * upcastAlso: {
  360. * big: viewElement => {
  361. * const fontSize = viewElement.getStyle( 'font-size' );
  362. *
  363. * if ( !fontSize ) {
  364. * return null;
  365. * }
  366. *
  367. * const match = fontSize.match( /(\d+)\s*px/ );
  368. *
  369. * if ( !match ) {
  370. * return null;
  371. * }
  372. *
  373. * const size = Number( match[ 1 ] );
  374. *
  375. * if ( viewElement.is( 'span' ) && size > 10 ) {
  376. * // Returned value can be an object with the matched properties.
  377. * // These properties will be "consumed" during the conversion.
  378. * // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
  379. *
  380. * return { name: true, styles: [ 'font-size' ] };
  381. * }
  382. *
  383. * return null;
  384. * },
  385. * small: viewElement => {
  386. * const fontSize = viewElement.getStyle( 'font-size' );
  387. *
  388. * if ( !fontSize ) {
  389. * return null;
  390. * }
  391. *
  392. * const match = fontSize.match( /(\d+)\s*px/ );
  393. *
  394. * if ( !match ) {
  395. * return null;
  396. * }
  397. *
  398. * const size = Number( match[ 1 ] );
  399. *
  400. * if ( viewElement.is( 'span' ) && size < 10 ) {
  401. * // Returned value can be an object with the matched properties.
  402. * // These properties will be "consumed" during the conversion.
  403. * // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
  404. *
  405. * return { name: true, styles: [ 'font-size' ] };
  406. * }
  407. *
  408. * return null;
  409. * }
  410. * }
  411. * } );
  412. *
  413. * The `definition.model` parameter specifies which model attribute should be converted from or to. It can be a `{ key, value }` object
  414. * describing the attribute key and value to convert or a `String` specifying just the attribute key (then `value` is set to `true`).
  415. * See {@link module:engine/conversion/conversion~ConverterDefinition} to learn about other parameters.
  416. *
  417. * @param {module:engine/conversion/conversion~ConverterDefinition} definition The converter definition.
  418. */
  419. attributeToElement( definition ) {
  420. // Set up downcast converter.
  421. this.for( 'downcast' ).attributeToElement( definition );
  422. // Set up upcast converter.
  423. for ( const { model, view } of _getAllUpcastDefinitions( definition ) ) {
  424. this.for( 'upcast' )
  425. .elementToAttribute( {
  426. view,
  427. model,
  428. converterPriority: definition.converterPriority
  429. } );
  430. }
  431. }
  432. /**
  433. * Sets up converters between the model and the view that convert a model attribute to a view attribute (and vice versa).
  434. * For example, `<image src='foo.jpg'></image>` is converted to `<img src='foo.jpg'></img>` (the same attribute key and value).
  435. * This type of converters is intended to be used with {@link module:engine/model/element~Element model element} nodes.
  436. * To convert text attributes {@link module:engine/conversion/conversion~Conversion#attributeToElement `attributeToElement converter`}
  437. * should be set up.
  438. *
  439. * // A simple conversion from the `source` model attribute to the `src` view attribute (and vice versa).
  440. * editor.conversion.attributeToAttribute( { model: 'source', view: 'src' } );
  441. *
  442. * // Attribute values are strictly specified.
  443. * editor.conversion.attributeToAttribute( {
  444. * model: {
  445. * name: 'image',
  446. * key: 'aside',
  447. * values: [ 'aside' ]
  448. * },
  449. * view: {
  450. * aside: {
  451. * name: 'img',
  452. * key: 'class',
  453. * value: [ 'aside', 'half-size' ]
  454. * }
  455. * }
  456. * } );
  457. *
  458. * // Set the style attribute.
  459. * editor.conversion.attributeToAttribute( {
  460. * model: {
  461. * name: 'image',
  462. * key: 'aside',
  463. * values: [ 'aside' ]
  464. * },
  465. * view: {
  466. * aside: {
  467. * name: 'img',
  468. * key: 'style',
  469. * value: {
  470. * float: 'right',
  471. * width: '50%',
  472. * margin: '5px'
  473. * }
  474. * }
  475. * }
  476. * } );
  477. *
  478. * // Conversion from and to a model attribute key whose value is an enum (`align=right|center`).
  479. * // Use `upcastAlso` to define other view elements that should also be converted to the `align=right` attribute.
  480. * editor.conversion.attributeToAttribute( {
  481. * model: {
  482. * key: 'align',
  483. * values: [ 'right', 'center' ]
  484. * },
  485. * view: {
  486. * right: {
  487. * key: 'class',
  488. * value: 'align-right'
  489. * },
  490. * center: {
  491. * key: 'class',
  492. * value: 'align-center'
  493. * }
  494. * },
  495. * upcastAlso: {
  496. * right: {
  497. * styles: {
  498. * 'text-align': 'right'
  499. * }
  500. * },
  501. * center: {
  502. * styles: {
  503. * 'text-align': 'center'
  504. * }
  505. * }
  506. * }
  507. * } );
  508. *
  509. * The `definition.model` parameter specifies which model attribute should be converted from and to.
  510. * It can be a `{ key, [ values ], [ name ] }` object or a `String`, which will be treated like `{ key: definition.model }`.
  511. * The `key` property is the model attribute key to convert from and to.
  512. * The `values` are the possible model attribute values. If `values` is not set, the model attribute value will be the same as the
  513. * view attribute value.
  514. * If `name` is set, the conversion will be set up only for model elements with the given name.
  515. *
  516. * The `definition.view` parameter specifies which view attribute should be converted from and to.
  517. * It can be a `{ key, value, [ name ] }` object or a `String`, which will be treated like `{ key: definition.view }`.
  518. * The `key` property is the view attribute key to convert from and to.
  519. * The `value` is the view attribute value to convert from and to. If `definition.value` is not set, the view attribute value will be
  520. * the same as the model attribute value.
  521. * If `key` is `'class'`, `value` can be a `String` or an array of `String`s.
  522. * If `key` is `'style'`, `value` is an object with key-value pairs.
  523. * In other cases, `value` is a `String`.
  524. * If `name` is set, the conversion will be set up only for model elements with the given name.
  525. * If `definition.model.values` is set, `definition.view` is an object that assigns values from `definition.model.values`
  526. * to `{ key, value, [ name ] }` objects.
  527. *
  528. * `definition.upcastAlso` specifies which other matching view elements should also be upcast to the given model configuration.
  529. * If `definition.model.values` is set, `definition.upcastAlso` should be an object assigning values from `definition.model.values`
  530. * to {@link module:engine/view/matcher~MatcherPattern}s or arrays of {@link module:engine/view/matcher~MatcherPattern}s.
  531. *
  532. * **Note:** `definition.model` and `definition.view` form should be mirrored, so the same types of parameters should
  533. * be given in both parameters.
  534. *
  535. * @param {Object} definition The converter definition.
  536. * @param {String|Object} definition.model The model attribute to convert from and to.
  537. * @param {String|Object} definition.view The view attribute to convert from and to.
  538. * @param {module:engine/view/matcher~MatcherPattern|Array.<module:engine/view/matcher~MatcherPattern>} [definition.upcastAlso]
  539. * Any view element matching `definition.upcastAlso` will also be converted to the given model attribute. `definition.upcastAlso`
  540. * is used only if `config.model.values` is specified.
  541. */
  542. attributeToAttribute( definition ) {
  543. // Set up downcast converter.
  544. this.for( 'downcast' ).attributeToAttribute( definition );
  545. // Set up upcast converter.
  546. for ( const { model, view } of _getAllUpcastDefinitions( definition ) ) {
  547. this.for( 'upcast' )
  548. .attributeToAttribute( {
  549. view,
  550. model
  551. } );
  552. }
  553. }
  554. /**
  555. * Creates and caches conversion helpers for given dispatchers group.
  556. *
  557. * @private
  558. * @param {Object} options
  559. * @param {String} options.name Group name.
  560. * @param {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
  561. * module:engine/conversion/upcastdispatcher~UpcastDispatcher>} options.dispatchers
  562. * @param {Boolean} options.isDowncast
  563. */
  564. _createConversionHelpers( { name, dispatchers, isDowncast } ) {
  565. if ( this._helpers.has( name ) ) {
  566. /**
  567. * Trying to register a group name that has already been registered.
  568. *
  569. * @error conversion-group-exists
  570. */
  571. throw new CKEditorError( 'conversion-group-exists: Trying to register a group name that has already been registered.' );
  572. }
  573. const helpers = isDowncast ? new DowncastHelpers( dispatchers ) : new UpcastHelpers( dispatchers );
  574. this._helpers.set( name, helpers );
  575. }
  576. }
  577. /**
  578. * Defines how the model should be converted from and to the view.
  579. *
  580. * @typedef {Object} module:engine/conversion/conversion~ConverterDefinition
  581. *
  582. * @property {*} [model] The model conversion definition. Describes the model element or model attribute to convert. This parameter differs
  583. * for different functions that accept `ConverterDefinition`. See the description of the function to learn how to set it.
  584. * @property {module:engine/view/elementdefinition~ElementDefinition|Object} view The definition of the view element to convert from and
  585. * to. If `model` describes multiple values, `view` is an object that assigns these values (`view` object keys) to view element definitions
  586. * (`view` object values).
  587. * @property {module:engine/view/matcher~MatcherPattern|Array.<module:engine/view/matcher~MatcherPattern>} [upcastAlso]
  588. * Any view element matching `upcastAlso` will also be converted to the model. If `model` describes multiple values, `upcastAlso`
  589. * is an object that assigns these values (`upcastAlso` object keys) to {@link module:engine/view/matcher~MatcherPattern}s
  590. * (`upcastAlso` object values).
  591. * @property {module:utils/priorities~PriorityString} [converterPriority] The converter priority.
  592. */
  593. // Helper function that creates a joint array out of an item passed in `definition.view` and items passed in
  594. // `definition.upcastAlso`.
  595. //
  596. // @param {module:engine/conversion/conversion~ConverterDefinition} definition
  597. // @returns {Array} Array containing view definitions.
  598. function* _getAllUpcastDefinitions( definition ) {
  599. if ( definition.model.values ) {
  600. for ( const value of definition.model.values ) {
  601. const model = { key: definition.model.key, value };
  602. const view = definition.view[ value ];
  603. const upcastAlso = definition.upcastAlso ? definition.upcastAlso[ value ] : undefined;
  604. yield* _getUpcastDefinition( model, view, upcastAlso );
  605. }
  606. } else {
  607. yield* _getUpcastDefinition( definition.model, definition.view, definition.upcastAlso );
  608. }
  609. }
  610. function* _getUpcastDefinition( model, view, upcastAlso ) {
  611. yield { model, view };
  612. if ( upcastAlso ) {
  613. upcastAlso = Array.isArray( upcastAlso ) ? upcastAlso : [ upcastAlso ];
  614. for ( const upcastAlsoItem of upcastAlso ) {
  615. yield { model, view: upcastAlsoItem };
  616. }
  617. }
  618. }