mapper.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/conversion/mapper
  7. */
  8. import ModelPosition from '../model/position';
  9. import ModelRange from '../model/range';
  10. import ViewPosition from '../view/position';
  11. import ViewRange from '../view/range';
  12. import ViewText from '../view/text';
  13. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  14. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  15. /**
  16. * Maps elements, positions and markers between {@link module:engine/view/document~Document the view} and
  17. * {@link module:engine/model/model the model}.
  18. *
  19. * Mapper use bound elements to find corresponding elements and positions, so, to get proper results,
  20. * all model elements should be {@link module:engine/conversion/mapper~Mapper#bindElements bound}.
  21. *
  22. * To map complex model to/from view relations, you may provide custom callbacks for
  23. * {@link module:engine/conversion/mapper~Mapper#event:modelToViewPosition modelToViewPosition event} and
  24. * {@link module:engine/conversion/mapper~Mapper#event:viewToModelPosition viewToModelPosition event} that are fired whenever
  25. * a position mapping request occurs.
  26. * Those events are fired by {@link module:engine/conversion/mapper~Mapper#toViewPosition toViewPosition}
  27. * and {@link module:engine/conversion/mapper~Mapper#toModelPosition toModelPosition} methods. `Mapper` adds it's own default callbacks
  28. * with `'lowest'` priority. To override default `Mapper` mapping, add custom callback with higher priority and
  29. * stop the event.
  30. */
  31. export default class Mapper {
  32. /**
  33. * Creates an instance of the mapper.
  34. */
  35. constructor() {
  36. /**
  37. * Model element to view element mapping.
  38. *
  39. * @private
  40. * @member {WeakMap}
  41. */
  42. this._modelToViewMapping = new WeakMap();
  43. /**
  44. * View element to model element mapping.
  45. *
  46. * @private
  47. * @member {WeakMap}
  48. */
  49. this._viewToModelMapping = new WeakMap();
  50. /**
  51. * A map containing callbacks between view element names and functions evaluating length of view elements
  52. * in model.
  53. *
  54. * @private
  55. * @member {Map}
  56. */
  57. this._viewToModelLengthCallbacks = new Map();
  58. /**
  59. * Model marker name to view elements mapping.
  60. *
  61. * Keys are `String`s while values are `Set`s with {@link module:engine/view/element~Element view elements}.
  62. * One marker (name) can be mapped to multiple elements.
  63. *
  64. * @private
  65. * @member {Map}
  66. */
  67. this._markerNameToElements = new Map();
  68. // Default mapper algorithm for mapping model position to view position.
  69. this.on( 'modelToViewPosition', ( evt, data ) => {
  70. if ( data.viewPosition ) {
  71. return;
  72. }
  73. const viewContainer = this._modelToViewMapping.get( data.modelPosition.parent );
  74. data.viewPosition = this._findPositionIn( viewContainer, data.modelPosition.offset );
  75. }, { priority: 'low' } );
  76. // Default mapper algorithm for mapping view position to model position.
  77. this.on( 'viewToModelPosition', ( evt, data ) => {
  78. if ( data.modelPosition ) {
  79. return;
  80. }
  81. let viewBlock = data.viewPosition.parent;
  82. let modelParent = this._viewToModelMapping.get( viewBlock );
  83. while ( !modelParent ) {
  84. viewBlock = viewBlock.parent;
  85. modelParent = this._viewToModelMapping.get( viewBlock );
  86. }
  87. const modelOffset = this._toModelOffset( data.viewPosition.parent, data.viewPosition.offset, viewBlock );
  88. data.modelPosition = ModelPosition._createAt( modelParent, modelOffset );
  89. }, { priority: 'low' } );
  90. }
  91. /**
  92. * Marks model and view elements as corresponding. Corresponding elements can be retrieved by using
  93. * the {@link module:engine/conversion/mapper~Mapper#toModelElement toModelElement} and
  94. * {@link module:engine/conversion/mapper~Mapper#toViewElement toViewElement} methods.
  95. * The information that elements are bound is also used to translate positions.
  96. *
  97. * @param {module:engine/model/element~Element} modelElement Model element.
  98. * @param {module:engine/view/element~Element} viewElement View element.
  99. */
  100. bindElements( modelElement, viewElement ) {
  101. this._modelToViewMapping.set( modelElement, viewElement );
  102. this._viewToModelMapping.set( viewElement, modelElement );
  103. }
  104. /**
  105. * Unbinds given {@link module:engine/view/element~Element view element} from the map.
  106. *
  107. * **Note:** view-to-model binding will be removed, if it existed. However, corresponding model-to-view binding
  108. * will be removed only if model element is still bound to passed `viewElement`.
  109. *
  110. * This behavior lets for re-binding model element to another view element without fear of losing the new binding
  111. * when the previously bound view element is unbound.
  112. *
  113. * @param {module:engine/view/element~Element} viewElement View element to unbind.
  114. */
  115. unbindViewElement( viewElement ) {
  116. const modelElement = this.toModelElement( viewElement );
  117. this._viewToModelMapping.delete( viewElement );
  118. if ( this._modelToViewMapping.get( modelElement ) == viewElement ) {
  119. this._modelToViewMapping.delete( modelElement );
  120. }
  121. }
  122. /**
  123. * Unbinds given {@link module:engine/model/element~Element model element} from the map.
  124. *
  125. * **Note:** model-to-view binding will be removed, if it existed. However, corresponding view-to-model binding
  126. * will be removed only if view element is still bound to passed `modelElement`.
  127. *
  128. * This behavior lets for re-binding view element to another model element without fear of losing the new binding
  129. * when the previously bound model element is unbound.
  130. *
  131. * @param {module:engine/model/element~Element} modelElement Model element to unbind.
  132. */
  133. unbindModelElement( modelElement ) {
  134. const viewElement = this.toViewElement( modelElement );
  135. this._modelToViewMapping.delete( modelElement );
  136. if ( this._viewToModelMapping.get( viewElement ) == modelElement ) {
  137. this._viewToModelMapping.delete( viewElement );
  138. }
  139. }
  140. /**
  141. * Binds given marker name with given {@link module:engine/view/element~Element view element}. The element
  142. * will be added to the current set of elements bound with given marker name.
  143. *
  144. * @param {module:engine/view/element~Element} element Element to bind.
  145. * @param {String} name Marker name.
  146. */
  147. bindElementToMarker( element, name ) {
  148. const elements = this._markerNameToElements.get( name ) || new Set();
  149. elements.add( element );
  150. this._markerNameToElements.set( name, elements );
  151. }
  152. /**
  153. * Unbinds all elements from given marker name.
  154. *
  155. * @param {String} name Marker name.
  156. */
  157. unbindElementsFromMarkerName( name ) {
  158. this._markerNameToElements.delete( name );
  159. }
  160. /**
  161. * Removes all model to view and view to model bindings.
  162. */
  163. clearBindings() {
  164. this._modelToViewMapping = new WeakMap();
  165. this._viewToModelMapping = new WeakMap();
  166. this._markerNameToElements = new Map();
  167. }
  168. /**
  169. * Gets the corresponding model element.
  170. *
  171. * **Note:** {@link module:engine/view/uielement~UIElement} does not have corresponding element in model.
  172. *
  173. * @param {module:engine/view/element~Element} viewElement View element.
  174. * @returns {module:engine/model/element~Element|undefined} Corresponding model element or `undefined` if not found.
  175. */
  176. toModelElement( viewElement ) {
  177. return this._viewToModelMapping.get( viewElement );
  178. }
  179. /**
  180. * Gets the corresponding view element.
  181. *
  182. * @param {module:engine/model/element~Element} modelElement Model element.
  183. * @returns {module:engine/view/element~Element|undefined} Corresponding view element or `undefined` if not found.
  184. */
  185. toViewElement( modelElement ) {
  186. return this._modelToViewMapping.get( modelElement );
  187. }
  188. /**
  189. * Gets the corresponding model range.
  190. *
  191. * @param {module:engine/view/range~Range} viewRange View range.
  192. * @returns {module:engine/model/range~Range} Corresponding model range.
  193. */
  194. toModelRange( viewRange ) {
  195. return new ModelRange( this.toModelPosition( viewRange.start ), this.toModelPosition( viewRange.end ) );
  196. }
  197. /**
  198. * Gets the corresponding view range.
  199. *
  200. * @param {module:engine/model/range~Range} modelRange Model range.
  201. * @returns {module:engine/view/range~Range} Corresponding view range.
  202. */
  203. toViewRange( modelRange ) {
  204. return new ViewRange( this.toViewPosition( modelRange.start ), this.toViewPosition( modelRange.end ) );
  205. }
  206. /**
  207. * Gets the corresponding model position.
  208. *
  209. * @fires viewToModelPosition
  210. * @param {module:engine/view/position~Position} viewPosition View position.
  211. * @returns {module:engine/model/position~Position} Corresponding model position.
  212. */
  213. toModelPosition( viewPosition ) {
  214. const data = {
  215. viewPosition,
  216. mapper: this
  217. };
  218. this.fire( 'viewToModelPosition', data );
  219. return data.modelPosition;
  220. }
  221. /**
  222. * Gets the corresponding view position.
  223. *
  224. * @fires modelToViewPosition
  225. * @param {module:engine/model/position~Position} modelPosition Model position.
  226. * @param {Object} [options] Additional options for position mapping process.
  227. * @param {Boolean} [options.isPhantom=false] Should be set to `true` if the model position to map is pointing to a place
  228. * in model tree which no longer exists. For example, it could be an end of a removed model range.
  229. * @returns {module:engine/view/position~Position} Corresponding view position.
  230. */
  231. toViewPosition( modelPosition, options = { isPhantom: false } ) {
  232. const data = {
  233. modelPosition,
  234. mapper: this,
  235. isPhantom: options.isPhantom
  236. };
  237. this.fire( 'modelToViewPosition', data );
  238. return data.viewPosition;
  239. }
  240. /**
  241. * Gets all view elements bound to the given marker name.
  242. *
  243. * @param {String} name Marker name.
  244. * @returns {Set.<module:engine/view/element~Element>|null} View elements bound with given marker name or `null`
  245. * if no elements are bound to given marker name.
  246. */
  247. markerNameToElements( name ) {
  248. const boundElements = this._markerNameToElements.get( name );
  249. if ( !boundElements ) {
  250. return null;
  251. }
  252. const elements = new Set();
  253. for ( const element of boundElements ) {
  254. if ( element.is( 'attributeElement' ) ) {
  255. for ( const clone of element.getElementsWithSameId() ) {
  256. elements.add( clone );
  257. }
  258. } else {
  259. elements.add( element );
  260. }
  261. }
  262. return elements;
  263. }
  264. /**
  265. * Registers a callback that evaluates the length in the model of a view element with given name.
  266. *
  267. * The callback is fired with one argument, which is a view element instance. The callback is expected to return
  268. * a number representing the length of view element in model.
  269. *
  270. * // List item in view may contain nested list, which have other list items. In model though,
  271. * // the lists are represented by flat structure. Because of those differences, length of list view element
  272. * // may be greater than one. In the callback it's checked how many nested list items are in evaluated list item.
  273. *
  274. * function getViewListItemLength( element ) {
  275. * let length = 1;
  276. *
  277. * for ( let child of element.getChildren() ) {
  278. * if ( child.name == 'ul' || child.name == 'ol' ) {
  279. * for ( let item of child.getChildren() ) {
  280. * length += getViewListItemLength( item );
  281. * }
  282. * }
  283. * }
  284. *
  285. * return length;
  286. * }
  287. *
  288. * mapper.registerViewToModelLength( 'li', getViewListItemLength );
  289. *
  290. * @param {String} viewElementName Name of view element for which callback is registered.
  291. * @param {Function} lengthCallback Function return a length of view element instance in model.
  292. */
  293. registerViewToModelLength( viewElementName, lengthCallback ) {
  294. this._viewToModelLengthCallbacks.set( viewElementName, lengthCallback );
  295. }
  296. /**
  297. * Calculates model offset based on the view position and the block element.
  298. *
  299. * Example:
  300. *
  301. * <p>foo<b>ba|r</b></p> // _toModelOffset( b, 2, p ) -> 5
  302. *
  303. * Is a sum of:
  304. *
  305. * <p>foo|<b>bar</b></p> // _toModelOffset( p, 3, p ) -> 3
  306. * <p>foo<b>ba|r</b></p> // _toModelOffset( b, 2, b ) -> 2
  307. *
  308. * @private
  309. * @param {module:engine/view/element~Element} viewParent Position parent.
  310. * @param {Number} viewOffset Position offset.
  311. * @param {module:engine/view/element~Element} viewBlock Block used as a base to calculate offset.
  312. * @returns {Number} Offset in the model.
  313. */
  314. _toModelOffset( viewParent, viewOffset, viewBlock ) {
  315. if ( viewBlock != viewParent ) {
  316. // See example.
  317. const offsetToParentStart = this._toModelOffset( viewParent.parent, viewParent.index, viewBlock );
  318. const offsetInParent = this._toModelOffset( viewParent, viewOffset, viewParent );
  319. return offsetToParentStart + offsetInParent;
  320. }
  321. // viewBlock == viewParent, so we need to calculate the offset in the parent element.
  322. // If the position is a text it is simple ("ba|r" -> 2).
  323. if ( viewParent.is( 'text' ) ) {
  324. return viewOffset;
  325. }
  326. // If the position is in an element we need to sum lengths of siblings ( <b> bar </b> foo | -> 3 + 3 = 6 ).
  327. let modelOffset = 0;
  328. for ( let i = 0; i < viewOffset; i++ ) {
  329. modelOffset += this.getModelLength( viewParent.getChild( i ) );
  330. }
  331. return modelOffset;
  332. }
  333. /**
  334. * Gets the length of the view element in the model.
  335. *
  336. * The length is calculated as follows:
  337. * * if {@link #registerViewToModelLength length mapping callback} is provided for given `viewNode` it is used to
  338. * evaluate model length (`viewNode` is used as first and only parameter passed to the callback),
  339. * * length of a {@link module:engine/view/text~Text text node} is equal to the length of it's
  340. * {@link module:engine/view/text~Text#data data},
  341. * * length of a {@link module:engine/view/uielement~UIElement ui element} is equal to 0,
  342. * * length of a mapped {@link module:engine/view/element~Element element} is equal to 1,
  343. * * length of a not-mapped {@link module:engine/view/element~Element element} is equal to the length of it's children.
  344. *
  345. * Examples:
  346. *
  347. * foo -> 3 // Text length is equal to it's data length.
  348. * <p>foo</p> -> 1 // Length of an element which is mapped is by default equal to 1.
  349. * <b>foo</b> -> 3 // Length of an element which is not mapped is a length of its children.
  350. * <div><p>x</p><p>y</p></div> -> 2 // Assuming that <div> is not mapped and <p> are mapped.
  351. *
  352. * @param {module:engine/view/element~Element} viewNode View node.
  353. * @returns {Number} Length of the node in the tree model.
  354. */
  355. getModelLength( viewNode ) {
  356. if ( this._viewToModelLengthCallbacks.get( viewNode.name ) ) {
  357. const callback = this._viewToModelLengthCallbacks.get( viewNode.name );
  358. return callback( viewNode );
  359. } else if ( this._viewToModelMapping.has( viewNode ) ) {
  360. return 1;
  361. } else if ( viewNode.is( 'text' ) ) {
  362. return viewNode.data.length;
  363. } else if ( viewNode.is( 'uiElement' ) ) {
  364. return 0;
  365. } else {
  366. let len = 0;
  367. for ( const child of viewNode.getChildren() ) {
  368. len += this.getModelLength( child );
  369. }
  370. return len;
  371. }
  372. }
  373. /**
  374. * Finds the position in the view node (or its children) with the expected model offset.
  375. *
  376. * Example:
  377. *
  378. * <p>fo<b>bar</b>bom</p> -> expected offset: 4
  379. *
  380. * _findPositionIn( p, 4 ):
  381. * <p>|fo<b>bar</b>bom</p> -> expected offset: 4, actual offset: 0
  382. * <p>fo|<b>bar</b>bom</p> -> expected offset: 4, actual offset: 2
  383. * <p>fo<b>bar</b>|bom</p> -> expected offset: 4, actual offset: 5 -> we are too far
  384. *
  385. * _findPositionIn( b, 4 - ( 5 - 3 ) ):
  386. * <p>fo<b>|bar</b>bom</p> -> expected offset: 2, actual offset: 0
  387. * <p>fo<b>bar|</b>bom</p> -> expected offset: 2, actual offset: 3 -> we are too far
  388. *
  389. * _findPositionIn( bar, 2 - ( 3 - 3 ) ):
  390. * We are in the text node so we can simple find the offset.
  391. * <p>fo<b>ba|r</b>bom</p> -> expected offset: 2, actual offset: 2 -> position found
  392. *
  393. * @private
  394. * @param {module:engine/view/element~Element} viewParent Tree view element in which we are looking for the position.
  395. * @param {Number} expectedOffset Expected offset.
  396. * @returns {module:engine/view/position~Position} Found position.
  397. */
  398. _findPositionIn( viewParent, expectedOffset ) {
  399. // Last scanned view node.
  400. let viewNode;
  401. // Length of the last scanned view node.
  402. let lastLength = 0;
  403. let modelOffset = 0;
  404. let viewOffset = 0;
  405. // In the text node it is simple: offset in the model equals offset in the text.
  406. if ( viewParent.is( 'text' ) ) {
  407. return new ViewPosition( viewParent, expectedOffset );
  408. }
  409. // In other cases we add lengths of child nodes to find the proper offset.
  410. // If it is smaller we add the length.
  411. while ( modelOffset < expectedOffset ) {
  412. viewNode = viewParent.getChild( viewOffset );
  413. lastLength = this.getModelLength( viewNode );
  414. modelOffset += lastLength;
  415. viewOffset++;
  416. }
  417. // If it equals we found the position.
  418. if ( modelOffset == expectedOffset ) {
  419. return this._moveViewPositionToTextNode( new ViewPosition( viewParent, viewOffset ) );
  420. }
  421. // If it is higher we need to enter last child.
  422. else {
  423. // ( modelOffset - lastLength ) is the offset to the child we enter,
  424. // so we subtract it from the expected offset to fine the offset in the child.
  425. return this._findPositionIn( viewNode, expectedOffset - ( modelOffset - lastLength ) );
  426. }
  427. }
  428. /**
  429. * Because we prefer positions in text nodes over positions next to text node moves view position to the text node
  430. * if it was next to it.
  431. *
  432. * <p>[]<b>foo</b></p> -> <p>[]<b>foo</b></p> // do not touch if position is not directly next to text
  433. * <p>foo[]<b>foo</b></p> -> <p>foo{}<b>foo</b></p> // move to text node
  434. * <p><b>[]foo</b></p> -> <p><b>{}foo</b></p> // move to text node
  435. *
  436. * @private
  437. * @param {module:engine/view/position~Position} viewPosition Position potentially next to text node.
  438. * @returns {module:engine/view/position~Position} Position in text node if possible.
  439. */
  440. _moveViewPositionToTextNode( viewPosition ) {
  441. // If the position is just after text node, put it at the end of that text node.
  442. // If the position is just before text node, put it at the beginning of that text node.
  443. const nodeBefore = viewPosition.nodeBefore;
  444. const nodeAfter = viewPosition.nodeAfter;
  445. if ( nodeBefore instanceof ViewText ) {
  446. return new ViewPosition( nodeBefore, nodeBefore.data.length );
  447. } else if ( nodeAfter instanceof ViewText ) {
  448. return new ViewPosition( nodeAfter, 0 );
  449. }
  450. // Otherwise, just return the given position.
  451. return viewPosition;
  452. }
  453. /**
  454. * Fired for each model-to-view position mapping request. The purpose of this event is to enable custom model-to-view position
  455. * mapping. Callbacks added to this event take {@link module:engine/model/position~Position model position} and are expected to
  456. * calculate {@link module:engine/view/position~Position view position}. Calculated view position should be added as `viewPosition`
  457. * value in `data` object that is passed as one of parameters to the event callback.
  458. *
  459. * // Assume that "captionedImage" model element is converted to <img> and following <span> elements in view,
  460. * // and the model element is bound to <img> element. Force mapping model positions inside "captionedImage" to that
  461. * // <span> element.
  462. * mapper.on( 'modelToViewPosition', ( evt, data ) => {
  463. * const positionParent = modelPosition.parent;
  464. *
  465. * if ( positionParent.name == 'captionedImage' ) {
  466. * const viewImg = data.mapper.toViewElement( positionParent );
  467. * const viewCaption = viewImg.nextSibling; // The <span> element.
  468. *
  469. * data.viewPosition = new ViewPosition( viewCaption, modelPosition.offset );
  470. *
  471. * // Stop the event if other callbacks should not modify calculated value.
  472. * evt.stop();
  473. * }
  474. * } );
  475. *
  476. * **Note:** keep in mind that sometimes a "phantom" model position is being converted. "Phantom" model position is
  477. * a position that points to a non-existing place in model. Such position might still be valid for conversion, though
  478. * (it would point to a correct place in view when converted). One example of such situation is when a range is
  479. * removed from model, there may be a need to map the range's end (which is no longer valid model position). To
  480. * handle such situation, check `data.isPhantom` flag:
  481. *
  482. * // Assume that there is "customElement" model element and whenever position is before it, we want to move it
  483. * // to the inside of the view element bound to "customElement".
  484. * mapper.on( 'modelToViewPosition', ( evt, data ) => {
  485. * if ( data.isPhantom ) {
  486. * return;
  487. * }
  488. *
  489. * // Below line might crash for phantom position that does not exist in model.
  490. * const sibling = data.modelPosition.nodeBefore;
  491. *
  492. * // Check if this is the element we are interested in.
  493. * if ( !sibling.is( 'customElement' ) ) {
  494. * return;
  495. * }
  496. *
  497. * const viewElement = data.mapper.toViewElement( sibling );
  498. *
  499. * data.viewPosition = new ViewPosition( sibling, 0 );
  500. *
  501. * evt.stop();
  502. * } );
  503. *
  504. * **Note:** default mapping callback is provided with `low` priority setting and does not cancel the event, so it is possible to
  505. * attach a custom callback after default callback and also use `data.viewPosition` calculated by default callback
  506. * (for example to fix it).
  507. *
  508. * **Note:** default mapping callback will not fire if `data.viewPosition` is already set.
  509. *
  510. * **Note:** these callbacks are called **very often**. For efficiency reasons, it is advised to use them only when position
  511. * mapping between given model and view elements is unsolvable using just elements mapping and default algorithm. Also,
  512. * the condition that checks if special case scenario happened should be as simple as possible.
  513. *
  514. * @event modelToViewPosition
  515. * @param {Object} data Data pipeline object that can store and pass data between callbacks. The callback should add
  516. * `viewPosition` value to that object with calculated {@link module:engine/view/position~Position view position}.
  517. * @param {module:engine/conversion/mapper~Mapper} data.mapper Mapper instance that fired the event.
  518. */
  519. /**
  520. * Fired for each view-to-model position mapping request. See {@link module:engine/conversion/mapper~Mapper#event:modelToViewPosition}.
  521. *
  522. * // See example in `modelToViewPosition` event description.
  523. * // This custom mapping will map positions from <span> element next to <img> to the "captionedImage" element.
  524. * mapper.on( 'viewToModelPosition', ( evt, data ) => {
  525. * const positionParent = viewPosition.parent;
  526. *
  527. * if ( positionParent.hasClass( 'image-caption' ) ) {
  528. * const viewImg = positionParent.previousSibling;
  529. * const modelImg = data.mapper.toModelElement( viewImg );
  530. *
  531. * data.modelPosition = new ModelPosition( modelImg, viewPosition.offset );
  532. * evt.stop();
  533. * }
  534. * } );
  535. *
  536. * **Note:** default mapping callback is provided with `low` priority setting and does not cancel the event, so it is possible to
  537. * attach a custom callback after default callback and also use `data.modelPosition` calculated by default callback
  538. * (for example to fix it).
  539. *
  540. * **Note:** default mapping callback will not fire if `data.modelPosition` is already set.
  541. *
  542. * **Note:** these callbacks are called **very often**. For efficiency reasons, it is advised to use them only when position
  543. * mapping between given model and view elements is unsolvable using just elements mapping and default algorithm. Also,
  544. * the condition that checks if special case scenario happened should be as simple as possible.
  545. *
  546. * @event viewToModelPosition
  547. * @param {Object} data Data pipeline object that can store and pass data between callbacks. The callback should add
  548. * `modelPosition` value to that object with calculated {@link module:engine/model/position~Position model position}.
  549. * @param {module:engine/conversion/mapper~Mapper} data.mapper Mapper instance that fired the event.
  550. */
  551. }
  552. mix( Mapper, EmitterMixin );