model-to-view-converters.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelRange from '../model/range';
  6. import ViewElement from '../view/element';
  7. import ViewAttributeElement from '../view/attributeelement';
  8. import ViewText from '../view/text';
  9. import ViewRange from '../view/range';
  10. /**
  11. * Contains model to view converters for
  12. * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher}.
  13. *
  14. * @module engine/conversion/model-to-view-converters
  15. */
  16. /**
  17. * Function factory, creates a converter that converts node insertion changes from the model to the view.
  18. * The view element that will be added to the view depends on passed parameter. If {@link module:engine/view/element~Element} was passed,
  19. * it will be cloned and the copy will be inserted. If `Function` is provided, it is passed all the parameters of the
  20. * dispatcher's {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher#event:insert insert event}.
  21. * It's expected that the function returns a {@link module:engine/view/element~Element}.
  22. * The result of the function will be inserted to the view.
  23. *
  24. * The converter automatically consumes corresponding value from consumables list, stops the event (see
  25. * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher}) and bind model and view elements.
  26. *
  27. * modelDispatcher.on( 'insert:paragraph', insertElement( new ViewElement( 'p' ) ) );
  28. *
  29. * modelDispatcher.on(
  30. * 'insert:myElem',
  31. * insertElement( ( data, consumable, conversionApi ) => {
  32. * let myElem = new ViewElement( 'myElem', { myAttr: true }, new ViewText( 'myText' ) );
  33. *
  34. * // Do something fancy with myElem using data/consumable/conversionApi ...
  35. *
  36. * return myElem;
  37. * }
  38. * ) );
  39. *
  40. * @param {module:engine/view/element~Element|Function} elementCreator View element, or function returning a view element, which
  41. * will be inserted.
  42. * @returns {Function} Insert element event converter.
  43. */
  44. export function insertElement( elementCreator ) {
  45. return ( evt, data, consumable, conversionApi ) => {
  46. const viewElement = ( elementCreator instanceof ViewElement ) ?
  47. elementCreator.clone( true ) :
  48. elementCreator( data, consumable, conversionApi );
  49. if ( !viewElement ) {
  50. return;
  51. }
  52. if ( !consumable.consume( data.item, 'insert' ) ) {
  53. return;
  54. }
  55. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  56. conversionApi.mapper.bindElements( data.item, viewElement );
  57. conversionApi.writer.insert( viewPosition, viewElement );
  58. };
  59. }
  60. /**
  61. * Function factory, creates a default model-to-view converter for text insertion changes.
  62. *
  63. * The converter automatically consumes corresponding value from consumables list and stops the event (see
  64. * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher}).
  65. *
  66. * modelDispatcher.on( 'insert:$text', insertText() );
  67. *
  68. * @returns {Function} Insert text event converter.
  69. */
  70. export function insertText() {
  71. return ( evt, data, consumable, conversionApi ) => {
  72. if ( !consumable.consume( data.item, 'insert' ) ) {
  73. return;
  74. }
  75. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  76. const viewText = new ViewText( data.item.data );
  77. conversionApi.writer.insert( viewPosition, viewText );
  78. };
  79. }
  80. /**
  81. * Function factory, creates a default model-to-view converter for node remove changes.
  82. *
  83. * modelDispatcher.on( 'remove', remove() );
  84. *
  85. * @returns {Function} Remove event converter.
  86. */
  87. export function remove() {
  88. return ( evt, data, conversionApi ) => {
  89. // Find view range start position by mapping model position at which the remove happened.
  90. const viewStart = conversionApi.mapper.toViewPosition( data.position );
  91. const modelEnd = data.position.getShiftedBy( data.length );
  92. const viewEnd = conversionApi.mapper.toViewPosition( modelEnd, { isPhantom: true } );
  93. const viewRange = new ViewRange( viewStart, viewEnd );
  94. // Trim the range to remove in case some UI elements are on the view range boundaries.
  95. const removed = conversionApi.writer.remove( viewRange.getTrimmed() );
  96. // After the range is removed, unbind all view elements from the model.
  97. // Range inside view document fragment is used to unbind deeply.
  98. for ( const child of ViewRange.createIn( removed ).getItems() ) {
  99. conversionApi.mapper.unbindViewElement( child );
  100. }
  101. };
  102. }
  103. /**
  104. * Function factory, creates a converter that converts marker adding change to the view ui element.
  105. * The view ui element that will be added to the view depends on passed parameter. See {@link ~insertElement}.
  106. * In a case of collapsed range element will not wrap range but separate elements will be placed at the beginning
  107. * and at the end of the range.
  108. *
  109. * **Note:** unlike {@link ~insertElement}, the converter does not bind view element to model, because this converter
  110. * uses marker as "model source of data". This means that view ui element does not have corresponding model element.
  111. *
  112. * @param {module:engine/view/uielement~UIElement|Function} elementCreator View ui element, or function returning a view element, which
  113. * will be inserted.
  114. * @returns {Function} Insert element event converter.
  115. */
  116. export function insertUIElement( elementCreator ) {
  117. return ( evt, data, consumable, conversionApi ) => {
  118. let viewStartElement, viewEndElement;
  119. // Create two view elements. One will be inserted at the beginning of marker, one at the end.
  120. // If marker is collapsed, only "opening" element will be inserted.
  121. if ( elementCreator instanceof ViewElement ) {
  122. viewStartElement = elementCreator.clone( true );
  123. viewEndElement = elementCreator.clone( true );
  124. } else {
  125. data.isOpening = true;
  126. viewStartElement = elementCreator( data, conversionApi );
  127. data.isOpening = false;
  128. viewEndElement = elementCreator( data, conversionApi );
  129. }
  130. if ( !viewStartElement || !viewEndElement ) {
  131. return;
  132. }
  133. const markerRange = data.markerRange;
  134. const eventName = evt.name;
  135. // Marker that is collapsed has consumable build differently that non-collapsed one.
  136. // For more information see `addMarker` event description.
  137. // If marker's range is collapsed - check if it can be consumed.
  138. if ( markerRange.isCollapsed && !consumable.consume( markerRange, eventName ) ) {
  139. return;
  140. }
  141. // If marker's range is not collapsed - consume all items inside.
  142. for ( const value of markerRange ) {
  143. if ( !consumable.consume( value.item, eventName ) ) {
  144. return;
  145. }
  146. }
  147. const mapper = conversionApi.mapper;
  148. const writer = conversionApi.writer;
  149. // Add "opening" element.
  150. writer.insert( mapper.toViewPosition( markerRange.start ), viewStartElement );
  151. // Add "closing" element only if range is not collapsed.
  152. if ( !markerRange.isCollapsed ) {
  153. writer.insert( mapper.toViewPosition( markerRange.end ), viewEndElement );
  154. }
  155. evt.stop();
  156. };
  157. }
  158. /**
  159. * Function factory, creates a default model-to-view converter for removing {@link module:engine/view/uielement~UIElement ui element}
  160. * basing on marker remove change.
  161. *
  162. * @param {module:engine/view/uielement~UIElement|Function} elementCreator View ui element, or function returning
  163. * a view ui element, which will be used as a pattern when look for element to remove at the marker start position.
  164. * @returns {Function} Remove ui element converter.
  165. */
  166. export function removeUIElement( elementCreator ) {
  167. return ( evt, data, conversionApi ) => {
  168. let viewStartElement, viewEndElement;
  169. // Create two view elements. One will be used to remove "opening element", the other for "closing element".
  170. // If marker was collapsed, only "opening" element will be removed.
  171. if ( elementCreator instanceof ViewElement ) {
  172. viewStartElement = elementCreator.clone( true );
  173. viewEndElement = elementCreator.clone( true );
  174. } else {
  175. data.isOpening = true;
  176. viewStartElement = elementCreator( data, conversionApi );
  177. data.isOpening = false;
  178. viewEndElement = elementCreator( data, conversionApi );
  179. }
  180. if ( !viewStartElement || !viewEndElement ) {
  181. return;
  182. }
  183. const markerRange = data.markerRange;
  184. const writer = conversionApi.writer;
  185. // When removing the ui elements, we map the model range to view twice, because that view range
  186. // may change after the first clearing.
  187. if ( !markerRange.isCollapsed ) {
  188. writer.clear( conversionApi.mapper.toViewRange( markerRange ).getEnlarged(), viewEndElement );
  189. }
  190. // Remove "opening" element.
  191. writer.clear( conversionApi.mapper.toViewRange( markerRange ).getEnlarged(), viewStartElement );
  192. evt.stop();
  193. };
  194. }
  195. /**
  196. * Function factory, creates a converter that converts set/change/remove attribute changes from the model to the view.
  197. *
  198. * Attributes from model are converted to the view element attributes in the view. You may provide a custom function to generate
  199. * a key-value attribute pair to add/change/remove. If not provided, model attributes will be converted to view elements
  200. * attributes on 1-to-1 basis.
  201. *
  202. * **Note:** Provided attribute creator should always return the same `key` for given attribute from the model.
  203. *
  204. * The converter automatically consumes corresponding value from consumables list and stops the event (see
  205. * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher}).
  206. *
  207. * modelDispatcher.on( 'attribute:customAttr:myElem', changeAttribute( ( data ) => {
  208. * // Change attribute key from `customAttr` to `class` in view.
  209. * const key = 'class';
  210. * let value = data.attributeNewValue;
  211. *
  212. * // Force attribute value to 'empty' if the model element is empty.
  213. * if ( data.item.childCount === 0 ) {
  214. * value = 'empty';
  215. * }
  216. *
  217. * // Return key-value pair.
  218. * return { key, value };
  219. * } ) );
  220. *
  221. * @param {Function} [attributeCreator] Function returning an object with two properties: `key` and `value`, which
  222. * represents attribute key and attribute value to be set on a {@link module:engine/view/element~Element view element}.
  223. * The function is passed all the parameters of the
  224. * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher#event:attribute} event.
  225. * @returns {Function} Set/change attribute converter.
  226. */
  227. export function changeAttribute( attributeCreator ) {
  228. attributeCreator = attributeCreator || ( ( value, key ) => ( { value, key } ) );
  229. return ( evt, data, consumable, conversionApi ) => {
  230. if ( !consumable.consume( data.item, eventNameToConsumableType( evt.name ) ) ) {
  231. return;
  232. }
  233. const { key, value } = attributeCreator( data.attributeNewValue, data.attributeKey, data, consumable, conversionApi );
  234. if ( data.attributeNewValue !== null ) {
  235. conversionApi.mapper.toViewElement( data.item ).setAttribute( key, value );
  236. } else {
  237. conversionApi.mapper.toViewElement( data.item ).removeAttribute( key );
  238. }
  239. };
  240. }
  241. /**
  242. * Function factory, creates a converter that converts set/change/remove attribute changes from the model to the view.
  243. *
  244. * Attributes from model are converted to a view element that will be wrapping those view nodes that are bound to
  245. * model elements having given attribute. This is useful for attributes like `bold`, which may be set on text nodes in model
  246. * but are represented as an element in the view:
  247. *
  248. * [paragraph] MODEL ====> VIEW <p>
  249. * |- a {bold: true} |- <b>
  250. * |- b {bold: true} | |- ab
  251. * |- c |- c
  252. *
  253. * The wrapping node depends on passed parameter. If {@link module:engine/view/element~Element} was passed, it will be cloned and
  254. * the copy will become the wrapping element. If `Function` is provided, it is passed attribute value and then all the parameters of the
  255. * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher#event:attribute attribute event}.
  256. * It's expected that the function returns a {@link module:engine/view/element~Element}.
  257. * The result of the function will be the wrapping element.
  258. * When provided `Function` does not return element, then will be no conversion.
  259. *
  260. * The converter automatically consumes corresponding value from consumables list, stops the event (see
  261. * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher}).
  262. *
  263. * modelDispatcher.on( 'attribute:bold', wrapItem( new ViewAttributeElement( 'strong' ) ) );
  264. *
  265. * @param {module:engine/view/element~Element|Function} elementCreator View element, or function returning a view element, which will
  266. * be used for wrapping.
  267. * @returns {Function} Set/change attribute converter.
  268. */
  269. export function wrap( elementCreator ) {
  270. return ( evt, data, consumable, conversionApi ) => {
  271. // Recreate current wrapping node. It will be used to unwrap view range if the attribute value has changed
  272. // or the attribute was removed.
  273. const oldViewElement = ( elementCreator instanceof ViewElement ) ?
  274. elementCreator.clone( true ) :
  275. elementCreator( data.attributeOldValue, data, consumable, conversionApi );
  276. // Create node to wrap with.
  277. const newViewElement = ( elementCreator instanceof ViewElement ) ?
  278. elementCreator.clone( true ) :
  279. elementCreator( data.attributeNewValue, data, consumable, conversionApi );
  280. if ( !oldViewElement && !newViewElement ) {
  281. return;
  282. }
  283. if ( !consumable.consume( data.item, eventNameToConsumableType( evt.name ) ) ) {
  284. return;
  285. }
  286. let viewRange = conversionApi.mapper.toViewRange( data.range );
  287. const writer = conversionApi.writer;
  288. // First, unwrap the range from current wrapper.
  289. if ( data.attributeOldValue !== null ) {
  290. viewRange = writer.unwrap( viewRange, oldViewElement );
  291. }
  292. // Then wrap with the new wrapper.
  293. if ( data.attributeNewValue !== null ) {
  294. writer.wrap( viewRange, newViewElement );
  295. }
  296. };
  297. }
  298. /**
  299. * Function factory, creates converter that converts text inside marker's range. Converter wraps the text with
  300. * {@link module:engine/view/attributeelement~AttributeElement} created from provided descriptor.
  301. * See {link module:engine/conversion/model-to-view-converters~createViewElementFromHighlightDescriptor}.
  302. *
  303. * If the highlight descriptor will not provide `priority` property, `10` will be used.
  304. *
  305. * If the highlight descriptor will not provide `id` property, name of the marker will be used.
  306. *
  307. * @param {module:engine/conversion/model-to-view-converters~HighlightDescriptor|Function} highlightDescriptor
  308. * @return {Function}
  309. */
  310. export function highlightText( highlightDescriptor ) {
  311. return ( evt, data, consumable, conversionApi ) => {
  312. if ( data.markerRange.isCollapsed ) {
  313. return;
  314. }
  315. const modelItem = data.item;
  316. if ( !modelItem.is( 'textProxy' ) ) {
  317. return;
  318. }
  319. const descriptor = _prepareDescriptor( highlightDescriptor, data, conversionApi );
  320. if ( !descriptor ) {
  321. return;
  322. }
  323. if ( !consumable.consume( modelItem, evt.name ) ) {
  324. return;
  325. }
  326. const viewElement = createViewElementFromHighlightDescriptor( descriptor );
  327. const viewRange = conversionApi.mapper.toViewRange( data.range );
  328. conversionApi.writer.wrap( viewRange, viewElement );
  329. };
  330. }
  331. /**
  332. * Converter function factory. Creates a function which applies the marker's highlight to an element inside the marker's range.
  333. *
  334. * The converter checks if an element has `addHighlight` function stored as
  335. * {@link module:engine/view/element~Element#setCustomProperty custom property} and, if so, uses it to apply the highlight.
  336. * In such case converter will consume all element's children, assuming that they were handled by element itself.
  337. *
  338. * When `addHighlight` custom property is not present, element is not converted in any special way.
  339. * This means that converters will proceed to convert element's child nodes.
  340. *
  341. * If the highlight descriptor will not provide `priority` property, `10` will be used.
  342. *
  343. * If the highlight descriptor will not provide `id` property, name of the marker will be used.
  344. *
  345. * @param {module:engine/conversion/model-to-view-converters~HighlightDescriptor|Function} highlightDescriptor
  346. * @return {Function}
  347. */
  348. export function highlightElement( highlightDescriptor ) {
  349. return ( evt, data, consumable, conversionApi ) => {
  350. if ( data.markerRange.isCollapsed ) {
  351. return;
  352. }
  353. const modelItem = data.item;
  354. if ( !modelItem.is( 'element' ) ) {
  355. return;
  356. }
  357. const descriptor = _prepareDescriptor( highlightDescriptor, data, conversionApi );
  358. if ( !descriptor ) {
  359. return;
  360. }
  361. if ( !consumable.test( modelItem, evt.name ) ) {
  362. return;
  363. }
  364. const viewElement = conversionApi.mapper.toViewElement( modelItem );
  365. if ( viewElement && viewElement.getCustomProperty( 'addHighlight' ) ) {
  366. // Consume element itself.
  367. consumable.consume( data.item, evt.name );
  368. // Consume all children nodes.
  369. for ( const value of ModelRange.createIn( modelItem ) ) {
  370. consumable.consume( value.item, evt.name );
  371. }
  372. viewElement.getCustomProperty( 'addHighlight' )( viewElement, descriptor );
  373. }
  374. };
  375. }
  376. /**
  377. * Function factory, creates a converter that converts model marker remove to the view.
  378. *
  379. * Both text nodes and elements are handled by this converter by they are handled a bit differently.
  380. *
  381. * Text nodes are unwrapped using {@link module:engine/view/attributeelement~AttributeElement} created from provided
  382. * highlight descriptor. See {link module:engine/conversion/model-to-view-converters~highlightDescriptorToAttributeElement}.
  383. *
  384. * For elements, the converter checks if an element has `removeHighlight` function stored as
  385. * {@link module:engine/view/element~Element#setCustomProperty custom property}. If so, it uses it to remove the highlight.
  386. * In such case, children of that element will not be converted.
  387. *
  388. * When `removeHighlight` is not present, element is not converted in any special way.
  389. * Instead converter will proceed to convert element's child nodes.
  390. *
  391. * If the highlight descriptor will not provide `priority` property, `10` will be used.
  392. *
  393. * If the highlight descriptor will not provide `id` property, name of the marker will be used.
  394. *
  395. * @param {module:engine/conversion/model-to-view-converters~HighlightDescriptor|Function} highlightDescriptor
  396. * @return {Function}
  397. */
  398. export function removeHighlight( highlightDescriptor ) {
  399. return ( evt, data, conversionApi ) => {
  400. // This conversion makes sense only for non-collapsed range.
  401. if ( data.markerRange.isCollapsed ) {
  402. return;
  403. }
  404. const descriptor = _prepareDescriptor( highlightDescriptor, data, conversionApi );
  405. if ( !descriptor ) {
  406. return;
  407. }
  408. const viewRange = conversionApi.mapper.toViewRange( data.markerRange );
  409. // Retrieve all items in the affected range. We will process them and remove highlight from them appropriately.
  410. const items = new Set( viewRange.getItems() );
  411. // First, iterate through all items and remove highlight from those container elements that have custom highlight handling.
  412. for ( const item of items ) {
  413. if ( item.is( 'containerElement' ) && item.getCustomProperty( 'removeHighlight' ) ) {
  414. item.getCustomProperty( 'removeHighlight' )( item, descriptor.id );
  415. // If container element had custom handling, remove all it's children from further processing.
  416. for ( const descendant of ViewRange.createIn( item ) ) {
  417. items.delete( descendant );
  418. }
  419. }
  420. }
  421. // Then, iterate through all other items. Look for text nodes and unwrap them. Start from the end
  422. // to prevent errors when view structure changes when unwrapping (and, for example, some attributes are merged).
  423. const viewHighlightElement = createViewElementFromHighlightDescriptor( descriptor );
  424. for ( const item of Array.from( items ).reverse() ) {
  425. if ( item.is( 'textProxy' ) ) {
  426. conversionApi.writer.unwrap( ViewRange.createOn( item ), viewHighlightElement );
  427. }
  428. }
  429. };
  430. }
  431. // Helper function for `highlight`. Prepares the actual descriptor object using value passed to the converter.
  432. function _prepareDescriptor( highlightDescriptor, data, conversionApi ) {
  433. // If passed descriptor is a creator function, call it. If not, just use passed value.
  434. const descriptor = typeof highlightDescriptor == 'function' ?
  435. highlightDescriptor( data, conversionApi ) :
  436. highlightDescriptor;
  437. if ( !descriptor ) {
  438. return null;
  439. }
  440. // Apply default descriptor priority.
  441. if ( !descriptor.priority ) {
  442. descriptor.priority = 10;
  443. }
  444. // Default descriptor id is marker name.
  445. if ( !descriptor.id ) {
  446. descriptor.id = data.markerName;
  447. }
  448. return descriptor;
  449. }
  450. /**
  451. * Returns the consumable type that is to be consumed in an event, basing on that event name.
  452. *
  453. * @param {String} evtName Event name.
  454. * @returns {String} Consumable type.
  455. */
  456. export function eventNameToConsumableType( evtName ) {
  457. const parts = evtName.split( ':' );
  458. return parts[ 0 ] + ':' + parts[ 1 ];
  459. }
  460. /**
  461. * Creates `span` {@link module:engine/view/attributeelement~AttributeElement view attribute element} from information
  462. * provided by {@link module:engine/conversion/model-to-view-converters~HighlightDescriptor} object. If priority
  463. * is not provided in descriptor - default priority will be used.
  464. *
  465. * @param {module:engine/conversion/model-to-view-converters~HighlightDescriptor} descriptor
  466. * @return {module:engine/conversion/model-to-view-converters~HighlightAttributeElement}
  467. */
  468. export function createViewElementFromHighlightDescriptor( descriptor ) {
  469. const viewElement = new HighlightAttributeElement( 'span', descriptor.attributes );
  470. if ( descriptor.class ) {
  471. const cssClasses = Array.isArray( descriptor.class ) ? descriptor.class : [ descriptor.class ];
  472. viewElement.addClass( ...cssClasses );
  473. }
  474. if ( descriptor.priority ) {
  475. viewElement.priority = descriptor.priority;
  476. }
  477. viewElement.setCustomProperty( 'highlightDescriptorId', descriptor.id );
  478. return viewElement;
  479. }
  480. /**
  481. * Special kind of {@link module:engine/view/attributeelement~AttributeElement} that is created and used in
  482. * marker-to-highlight conversion.
  483. *
  484. * The difference between `HighlightAttributeElement` and {@link module:engine/view/attributeelement~AttributeElement}
  485. * is {@link module:engine/view/attributeelement~AttributeElement#isSimilar} method.
  486. *
  487. * For `HighlightAttributeElement` it checks just `highlightDescriptorId` custom property, that is set during marker-to-highlight
  488. * conversion basing on {@link module:engine/conversion/model-to-view-converters~HighlightDescriptor} object.
  489. * `HighlightAttributeElement`s with same `highlightDescriptorId` property are considered similar.
  490. */
  491. class HighlightAttributeElement extends ViewAttributeElement {
  492. isSimilar( otherElement ) {
  493. if ( otherElement.is( 'attributeElement' ) ) {
  494. return this.getCustomProperty( 'highlightDescriptorId' ) === otherElement.getCustomProperty( 'highlightDescriptorId' );
  495. }
  496. return false;
  497. }
  498. }
  499. /**
  500. * Object describing how the content highlight should be created in the view.
  501. *
  502. * Each text node contained in the highlight will be wrapped with `span` element with CSS class(es), attributes and priority
  503. * described by this object.
  504. *
  505. * Each element can handle displaying the highlight separately by providing `addHighlight` and `removeHighlight` custom
  506. * properties:
  507. * * `HighlightDescriptor` is passed to the `addHighlight` function upon conversion and should be used to apply the highlight to
  508. * the element,
  509. * * descriptor id is passed to the `removeHighlight` function upon conversion and should be used to remove the highlight of given
  510. * id from the element.
  511. *
  512. * @typedef {Object} module:engine/conversion/model-to-view-converters~HighlightDescriptor
  513. *
  514. * @property {String|Array.<String>} class CSS class or array of classes to set. If descriptor is used to
  515. * create {@link module:engine/view/attributeelement~AttributeElement} over text nodes, those classes will be set
  516. * on that {@link module:engine/view/attributeelement~AttributeElement}. If descriptor is applied to an element,
  517. * usually those class will be set on that element, however this depends on how the element converts the descriptor.
  518. *
  519. * @property {String} [id] Descriptor identifier. If not provided, defaults to converted marker's name.
  520. *
  521. * @property {Number} [priority] Descriptor priority. If not provided, defaults to `10`. If descriptor is used to create
  522. * {@link module:engine/view/attributeelement~AttributeElement}, it will be that element's
  523. * {@link module:engine/view/attributeelement~AttributeElement#priority}. If descriptor is applied to an element,
  524. * the priority will be used to determine which descriptor is more important.
  525. *
  526. * @property {Object} [attributes] Attributes to set. If descriptor is used to create
  527. * {@link module:engine/view/attributeelement~AttributeElement} over text nodes, those attributes will be set on that
  528. * {@link module:engine/view/attributeelement~AttributeElement}. If descriptor is applied to an element, usually those
  529. * attributes will be set on that element, however this depends on how the element converts the descriptor.
  530. */