downcasthelpers.js 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * Contains downcast (model-to-view) converters for {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher}.
  7. *
  8. * @module engine/conversion/downcasthelpers
  9. */
  10. import ModelRange from '../model/range';
  11. import ModelSelection from '../model/selection';
  12. import ModelElement from '../model/element';
  13. import ViewAttributeElement from '../view/attributeelement';
  14. import DocumentSelection from '../model/documentselection';
  15. import ConversionHelpers from './conversionhelpers';
  16. import { cloneDeep } from 'lodash-es';
  17. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  18. /**
  19. * Downcast conversion helper functions.
  20. *
  21. * @extends module:engine/conversion/conversionhelpers~ConversionHelpers
  22. */
  23. export default class DowncastHelpers extends ConversionHelpers {
  24. /**
  25. * Model element to view element conversion helper.
  26. *
  27. * This conversion results in creating a view element. For example, model `<paragraph>Foo</paragraph>` becomes `<p>Foo</p>` in the view.
  28. *
  29. * editor.conversion.for( 'downcast' ).elementToElement( {
  30. * model: 'paragraph',
  31. * view: 'p'
  32. * } );
  33. *
  34. * editor.conversion.for( 'downcast' ).elementToElement( {
  35. * model: 'paragraph',
  36. * view: 'div',
  37. * converterPriority: 'high'
  38. * } );
  39. *
  40. * editor.conversion.for( 'downcast' ).elementToElement( {
  41. * model: 'fancyParagraph',
  42. * view: {
  43. * name: 'p',
  44. * classes: 'fancy'
  45. * }
  46. * } );
  47. *
  48. * editor.conversion.for( 'downcast' ).elementToElement( {
  49. * model: 'heading',
  50. * view: ( modelElement, conversionApi ) => {
  51. * const { writer } = conversionApi;
  52. *
  53. * return writer.createContainerElement( 'h' + modelElement.getAttribute( 'level' ) );
  54. * }
  55. * } );
  56. *
  57. * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
  58. * to the conversion process.
  59. *
  60. * You can read more about element to element conversion in the
  61. * {@glink framework/guides/deep-dive/conversion/custom-element-conversion Custom element conversion} guide.
  62. *
  63. * @method #elementToElement
  64. * @param {Object} config Conversion configuration.
  65. * @param {String} config.model The name of the model element to convert.
  66. * @param {module:engine/view/elementdefinition~ElementDefinition|Function} config.view A view element definition or a function
  67. * that takes the model element and {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi downcast conversion API}
  68. * as parameters and returns a view container element.
  69. * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers}
  70. */
  71. elementToElement( config ) {
  72. return this.add( downcastElementToElement( config ) );
  73. }
  74. /**
  75. * Model attribute to view element conversion helper.
  76. *
  77. * This conversion results in wrapping view nodes with a view attribute element. For example, a model text node with
  78. * `"Foo"` as data and the `bold` attribute becomes `<strong>Foo</strong>` in the view.
  79. *
  80. * editor.conversion.for( 'downcast' ).attributeToElement( {
  81. * model: 'bold',
  82. * view: 'strong'
  83. * } );
  84. *
  85. * editor.conversion.for( 'downcast' ).attributeToElement( {
  86. * model: 'bold',
  87. * view: 'b',
  88. * converterPriority: 'high'
  89. * } );
  90. *
  91. * editor.conversion.for( 'downcast' ).attributeToElement( {
  92. * model: 'invert',
  93. * view: {
  94. * name: 'span',
  95. * classes: [ 'font-light', 'bg-dark' ]
  96. * }
  97. * } );
  98. *
  99. * editor.conversion.for( 'downcast' ).attributeToElement( {
  100. * model: {
  101. * key: 'fontSize',
  102. * values: [ 'big', 'small' ]
  103. * },
  104. * view: {
  105. * big: {
  106. * name: 'span',
  107. * styles: {
  108. * 'font-size': '1.2em'
  109. * }
  110. * },
  111. * small: {
  112. * name: 'span',
  113. * styles: {
  114. * 'font-size': '0.8em'
  115. * }
  116. * }
  117. * }
  118. * } );
  119. *
  120. * editor.conversion.for( 'downcast' ).attributeToElement( {
  121. * model: 'bold',
  122. * view: ( modelAttributeValue, conversionApi ) => {
  123. * const { writer } = conversionApi;
  124. *
  125. * return writer.createAttributeElement( 'span', {
  126. * style: 'font-weight:' + modelAttributeValue
  127. * } );
  128. * }
  129. * } );
  130. *
  131. * editor.conversion.for( 'downcast' ).attributeToElement( {
  132. * model: {
  133. * key: 'color',
  134. * name: '$text'
  135. * },
  136. * view: ( modelAttributeValue, conversionApi ) => {
  137. * const { writer } = conversionApi;
  138. *
  139. * return writer.createAttributeElement( 'span', {
  140. * style: 'color:' + modelAttributeValue
  141. * } );
  142. * }
  143. * } );
  144. *
  145. * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
  146. * to the conversion process.
  147. *
  148. * @method #attributeToElement
  149. * @param {Object} config Conversion configuration.
  150. * @param {String|Object} config.model The key of the attribute to convert from or a `{ key, values }` object. `values` is an array
  151. * of `String`s with possible values if the model attribute is an enumerable.
  152. * @param {module:engine/view/elementdefinition~ElementDefinition|Function|Object} config.view A view element definition or a function
  153. * that takes the model attribute value and
  154. * {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi downcast conversion API} as parameters and returns a view
  155. * attribute element. If `config.model.values` is given, `config.view` should be an object assigning values from `config.model.values`
  156. * to view element definitions or functions.
  157. * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
  158. * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers}
  159. */
  160. attributeToElement( config ) {
  161. return this.add( downcastAttributeToElement( config ) );
  162. }
  163. /**
  164. * Model attribute to view attribute conversion helper.
  165. *
  166. * This conversion results in adding an attribute to a view node, basing on an attribute from a model node. For example,
  167. * `<image src='foo.jpg'></image>` is converted to `<img src='foo.jpg'></img>`.
  168. *
  169. * editor.conversion.for( 'downcast' ).attributeToAttribute( {
  170. * model: 'source',
  171. * view: 'src'
  172. * } );
  173. *
  174. * editor.conversion.for( 'downcast' ).attributeToAttribute( {
  175. * model: 'source',
  176. * view: 'href',
  177. * converterPriority: 'high'
  178. * } );
  179. *
  180. * editor.conversion.for( 'downcast' ).attributeToAttribute( {
  181. * model: {
  182. * name: 'image',
  183. * key: 'source'
  184. * },
  185. * view: 'src'
  186. * } );
  187. *
  188. * editor.conversion.for( 'downcast' ).attributeToAttribute( {
  189. * model: {
  190. * name: 'styled',
  191. * values: [ 'dark', 'light' ]
  192. * },
  193. * view: {
  194. * dark: {
  195. * key: 'class',
  196. * value: [ 'styled', 'styled-dark' ]
  197. * },
  198. * light: {
  199. * key: 'class',
  200. * value: [ 'styled', 'styled-light' ]
  201. * }
  202. * }
  203. * } );
  204. *
  205. * editor.conversion.for( 'downcast' ).attributeToAttribute( {
  206. * model: 'styled',
  207. * view: modelAttributeValue => ( {
  208. * key: 'class',
  209. * value: 'styled-' + modelAttributeValue
  210. * } )
  211. * } );
  212. *
  213. * **Note**: Downcasting to a style property requires providing `value` as an object:
  214. *
  215. * editor.conversion.for( 'downcast' ).attributeToAttribute( {
  216. * model: 'lineHeight',
  217. * view: modelAttributeValue => ( {
  218. * key: 'style',
  219. * value: {
  220. * 'line-height': modelAttributeValue,
  221. * 'border-bottom': '1px dotted #ba2'
  222. * }
  223. * } )
  224. * } );
  225. *
  226. * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
  227. * to the conversion process.
  228. *
  229. * @method #attributeToAttribute
  230. * @param {Object} config Conversion configuration.
  231. * @param {String|Object} config.model The key of the attribute to convert from or a `{ key, values, [ name ] }` object describing
  232. * the attribute key, possible values and, optionally, an element name to convert from.
  233. * @param {String|Object|Function} config.view A view attribute key, or a `{ key, value }` object or a function that takes
  234. * the model attribute value and {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi downcast conversion API}
  235. * as parameters and returns a `{ key, value }` object. If `key` is `'class'`, `value` can be a `String` or an
  236. * array of `String`s. If `key` is `'style'`, `value` is an object with key-value pairs. In other cases, `value` is a `String`.
  237. * If `config.model.values` is set, `config.view` should be an object assigning values from `config.model.values` to
  238. * `{ key, value }` objects or a functions.
  239. * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
  240. * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers}
  241. */
  242. attributeToAttribute( config ) {
  243. return this.add( downcastAttributeToAttribute( config ) );
  244. }
  245. /**
  246. * Model marker to view element conversion helper.
  247. *
  248. * **Note**: This method should be used only for editing downcast. For data downcast, use
  249. * {@link #markerToData `#markerToData()`} that produces valid HTML data.
  250. *
  251. * This conversion results in creating a view element on the boundaries of the converted marker. If the converted marker
  252. * is collapsed, only one element is created. For example, model marker set like this: `<paragraph>F[oo b]ar</paragraph>`
  253. * becomes `<p>F<span data-marker="search"></span>oo b<span data-marker="search"></span>ar</p>` in the view.
  254. *
  255. * editor.conversion.for( 'editingDowncast' ).markerToElement( {
  256. * model: 'search',
  257. * view: 'marker-search'
  258. * } );
  259. *
  260. * editor.conversion.for( 'editingDowncast' ).markerToElement( {
  261. * model: 'search',
  262. * view: 'search-result',
  263. * converterPriority: 'high'
  264. * } );
  265. *
  266. * editor.conversion.for( 'editingDowncast' ).markerToElement( {
  267. * model: 'search',
  268. * view: {
  269. * name: 'span',
  270. * attributes: {
  271. * 'data-marker': 'search'
  272. * }
  273. * }
  274. * } );
  275. *
  276. * editor.conversion.for( 'editingDowncast' ).markerToElement( {
  277. * model: 'search',
  278. * view: ( markerData, conversionApi ) => {
  279. * const { writer } = conversionApi;
  280. *
  281. * return writer.createUIElement( 'span', {
  282. * 'data-marker': 'search',
  283. * 'data-start': markerData.isOpening
  284. * } );
  285. * }
  286. * } );
  287. *
  288. * If a function is passed as the `config.view` parameter, it will be used to generate both boundary elements. The function
  289. * receives the `data` object and {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi downcast conversion API}
  290. * as a parameters and should return an instance of the
  291. * {@link module:engine/view/uielement~UIElement view UI element}. The `data` object and
  292. * {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi `conversionApi`} are passed from
  293. * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:addMarker}. Additionally,
  294. * the `data.isOpening` parameter is passed, which is set to `true` for the marker start boundary element, and `false` to
  295. * the marker end boundary element.
  296. *
  297. * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
  298. * to the conversion process.
  299. *
  300. * @method #markerToElement
  301. * @param {Object} config Conversion configuration.
  302. * @param {String} config.model The name of the model marker (or model marker group) to convert.
  303. * @param {module:engine/view/elementdefinition~ElementDefinition|Function} config.view A view element definition or a function that
  304. * takes the model marker data and {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi downcast conversion API}
  305. * as a parameters and returns a view UI element.
  306. * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
  307. * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers}
  308. */
  309. markerToElement( config ) {
  310. return this.add( downcastMarkerToElement( config ) );
  311. }
  312. /**
  313. * Model marker to highlight conversion helper.
  314. *
  315. * This conversion results in creating a highlight on view nodes. For this kind of conversion,
  316. * {@link module:engine/conversion/downcasthelpers~HighlightDescriptor} should be provided.
  317. *
  318. * For text nodes, a `<span>` {@link module:engine/view/attributeelement~AttributeElement} is created and it wraps all text nodes
  319. * in the converted marker range. For example, a model marker set like this: `<paragraph>F[oo b]ar</paragraph>` becomes
  320. * `<p>F<span class="comment">oo b</span>ar</p>` in the view.
  321. *
  322. * {@link module:engine/view/containerelement~ContainerElement} may provide a custom way of handling highlight. Most often,
  323. * the element itself is given classes and attributes described in the highlight descriptor (instead of being wrapped in `<span>`).
  324. * For example, a model marker set like this: `[<image src="foo.jpg"></image>]` becomes `<img src="foo.jpg" class="comment"></img>`
  325. * in the view.
  326. *
  327. * For container elements, the conversion is two-step. While the converter processes the highlight descriptor and passes it
  328. * to a container element, it is the container element instance itself that applies values from the highlight descriptor.
  329. * So, in a sense, the converter takes care of stating what should be applied on what, while the element decides how to apply that.
  330. *
  331. * editor.conversion.for( 'downcast' ).markerToHighlight( { model: 'comment', view: { classes: 'comment' } } );
  332. *
  333. * editor.conversion.for( 'downcast' ).markerToHighlight( {
  334. * model: 'comment',
  335. * view: { classes: 'new-comment' },
  336. * converterPriority: 'high'
  337. * } );
  338. *
  339. * editor.conversion.for( 'downcast' ).markerToHighlight( {
  340. * model: 'comment',
  341. * view: ( data, converstionApi ) => {
  342. * // Assuming that the marker name is in a form of comment:commentType.
  343. * const commentType = data.markerName.split( ':' )[ 1 ];
  344. *
  345. * return {
  346. * classes: [ 'comment', 'comment-' + commentType ]
  347. * };
  348. * }
  349. * } );
  350. *
  351. * If a function is passed as the `config.view` parameter, it will be used to generate the highlight descriptor. The function
  352. * receives the `data` object and {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi downcast conversion API}
  353. * as a parameters and should return a
  354. * {@link module:engine/conversion/downcasthelpers~HighlightDescriptor highlight descriptor}.
  355. * The `data` object properties are passed from {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:addMarker}.
  356. *
  357. * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
  358. * to the conversion process.
  359. *
  360. * @method #markerToHighlight
  361. * @param {Object} config Conversion configuration.
  362. * @param {String} config.model The name of the model marker (or model marker group) to convert.
  363. * @param {module:engine/conversion/downcasthelpers~HighlightDescriptor|Function} config.view A highlight descriptor
  364. * that will be used for highlighting or a function that takes the model marker data and
  365. * {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi downcast conversion API} as a parameters
  366. * and returns a highlight descriptor.
  367. * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
  368. * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers}
  369. */
  370. markerToHighlight( config ) {
  371. return this.add( downcastMarkerToHighlight( config ) );
  372. }
  373. /**
  374. * Model marker converter for data downcast.
  375. *
  376. * This conversion creates a representation for model marker boundaries in the view:
  377. *
  378. * * If the marker boundary is at a position where text nodes are allowed, then a view element with the specified tag name
  379. * and `name` attribute is added at this position.
  380. * * In other cases, a specified attribute is set on a view element that is before or after the marker boundary.
  381. *
  382. * Typically, marker names use the `group:uniqueId:otherData` convention. For example: `comment:e34zfk9k2n459df53sjl34:zx32c`.
  383. * The default configuration for this conversion is that the first part is the `group` part and the rest of
  384. * the marker name becomes the `name` part.
  385. *
  386. * Tag and attribute names and values are generated from the marker name:
  387. *
  388. * * Templates for attributes are `data-[group]-start-before="[name]"`, `data-[group]-start-after="[name]"`,
  389. * `data-[group]-end-before="[name]"` and `data-[group]-end-after="[name]"`.
  390. * * Templates for view elements are `<[group]-start name="[name]">` and `<[group]-end name="[name]">`.
  391. *
  392. * Attributes mark whether the given marker's start or end boundary is before or after the given element.
  393. * Attributes `data-[group]-start-before` and `data-[group]-end-after` are favored.
  394. * The other two are used when the former two cannot be used.
  395. *
  396. * The conversion configuration can take a function that will generate different group and name parts.
  397. * If such function is set as the `config.view` parameter, it is passed a marker name and it is expected to return an object with two
  398. * properties: `group` and `name`. If the function returns a falsy value, the conversion will not take place.
  399. *
  400. * Basic usage:
  401. *
  402. * // Using the default conversion.
  403. * // In this case, all markers whose name starts with 'comment:' will be converted.
  404. * // The `group` parameter will be set to `comment`.
  405. * // The `name` parameter will be the rest of the marker name (without `:`).
  406. * editor.conversion.for( 'dataDowncast' ).markerToData( {
  407. * model: 'comment'
  408. * } );
  409. *
  410. * An example of a view that may be generated by this conversion (assuming a marker with the name `comment:commentId:uid` marked
  411. * by `[]`):
  412. *
  413. * // Model:
  414. * <paragraph>Foo[bar</paragraph>
  415. * <image src="abc.jpg"></image>]
  416. *
  417. * // View:
  418. * <p>Foo<comment-start name="commentId:uid"></comment-start>bar</p>
  419. * <figure data-comment-end-after="commentId:uid" class="image"><img src="abc.jpg" /></figure>
  420. *
  421. * In the example above, the comment starts before "bar" and ends after the image.
  422. *
  423. * If the `name` part is empty, the following view may be generated:
  424. *
  425. * <p>Foo <myMarker-start></myMarker-start>bar</p>
  426. * <figure data-myMarker-end-after="" class="image"><img src="abc.jpg" /></figure>
  427. *
  428. * **Note:** A situation where some markers have the `name` part and some do not have it is incorrect and should be avoided.
  429. *
  430. * Examples where `data-group-start-after` and `data-group-end-before` are used:
  431. *
  432. * // Model:
  433. * <blockQuote>[]<paragraph>Foo</paragraph></blockQuote>
  434. *
  435. * // View:
  436. * <blockquote><p data-group-end-before="name" data-group-start-before="name">Foo</p></blockquote>
  437. *
  438. * Similarly, when a marker is collapsed after the last element:
  439. *
  440. * // Model:
  441. * <blockQuote><paragraph>Foo</paragraph>[]</blockQuote>
  442. *
  443. * // View:
  444. * <blockquote><p data-group-end-after="name" data-group-start-after="name">Foo</p></blockquote>
  445. *
  446. * When there are multiple markers from the same group stored in the same attribute of the same element, their
  447. * name parts are put together in the attribute value, for example: `data-group-start-before="name1,name2,name3"`.
  448. *
  449. * Other examples of usage:
  450. *
  451. * // Using a custom function which is the same as the default conversion:
  452. * editor.conversion.for( 'dataDowncast' ).markerToData( {
  453. * model: 'comment'
  454. * view: markerName => ( {
  455. * group: 'comment',
  456. * name: markerName.substr( 8 ) // Removes 'comment:' part.
  457. * } )
  458. * } );
  459. *
  460. * // Using the converter priority:
  461. * editor.conversion.for( 'dataDowncast' ).markerToData( {
  462. * model: 'comment'
  463. * view: markerName => ( {
  464. * group: 'comment',
  465. * name: markerName.substr( 8 ) // Removes 'comment:' part.
  466. * } ),
  467. * converterPriority: 'high'
  468. * } );
  469. *
  470. * This kind of conversion is useful for saving data into the database, so it should be used in the data conversion pipeline.
  471. *
  472. * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
  473. * to the conversion process.
  474. *
  475. * @method #markerToData
  476. * @param {Object} config Conversion configuration.
  477. * @param {String} config.model The name of the model marker (or model marker group) to convert.
  478. * @param {Function} [config.view] A function that takes the model marker name and
  479. * {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi downcast conversion API} as a parameters
  480. * and returns an object with the `group` and `name` properties.
  481. * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
  482. * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers}
  483. */
  484. markerToData( config ) {
  485. return this.add( downcastMarkerToData( config ) );
  486. }
  487. }
  488. /**
  489. * Function factory that creates a default downcast converter for text insertion changes.
  490. *
  491. * The converter automatically consumes the corresponding value from the consumables list and stops the event (see
  492. * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher}).
  493. *
  494. * modelDispatcher.on( 'insert:$text', insertText() );
  495. *
  496. * @returns {Function} Insert text event converter.
  497. */
  498. export function insertText() {
  499. return ( evt, data, conversionApi ) => {
  500. if ( !conversionApi.consumable.consume( data.item, 'insert' ) ) {
  501. return;
  502. }
  503. const viewWriter = conversionApi.writer;
  504. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  505. const viewText = viewWriter.createText( data.item.data );
  506. viewWriter.insert( viewPosition, viewText );
  507. };
  508. }
  509. /**
  510. * Function factory that creates a default downcast converter for node remove changes.
  511. *
  512. * modelDispatcher.on( 'remove', remove() );
  513. *
  514. * @returns {Function} Remove event converter.
  515. */
  516. export function remove() {
  517. return ( evt, data, conversionApi ) => {
  518. // Find view range start position by mapping model position at which the remove happened.
  519. const viewStart = conversionApi.mapper.toViewPosition( data.position );
  520. const modelEnd = data.position.getShiftedBy( data.length );
  521. const viewEnd = conversionApi.mapper.toViewPosition( modelEnd, { isPhantom: true } );
  522. const viewRange = conversionApi.writer.createRange( viewStart, viewEnd );
  523. // Trim the range to remove in case some UI elements are on the view range boundaries.
  524. const removed = conversionApi.writer.remove( viewRange.getTrimmed() );
  525. // After the range is removed, unbind all view elements from the model.
  526. // Range inside view document fragment is used to unbind deeply.
  527. for ( const child of conversionApi.writer.createRangeIn( removed ).getItems() ) {
  528. conversionApi.mapper.unbindViewElement( child );
  529. }
  530. };
  531. }
  532. /**
  533. * Creates a `<span>` {@link module:engine/view/attributeelement~AttributeElement view attribute element} from the information
  534. * provided by the {@link module:engine/conversion/downcasthelpers~HighlightDescriptor highlight descriptor} object. If a priority
  535. * is not provided in the descriptor, the default priority will be used.
  536. *
  537. * @param {module:engine/view/downcastwriter~DowncastWriter} writer
  538. * @param {module:engine/conversion/downcasthelpers~HighlightDescriptor} descriptor
  539. * @returns {module:engine/view/attributeelement~AttributeElement}
  540. */
  541. export function createViewElementFromHighlightDescriptor( writer, descriptor ) {
  542. const viewElement = writer.createAttributeElement( 'span', descriptor.attributes );
  543. if ( descriptor.classes ) {
  544. viewElement._addClass( descriptor.classes );
  545. }
  546. if ( descriptor.priority ) {
  547. viewElement._priority = descriptor.priority;
  548. }
  549. viewElement._id = descriptor.id;
  550. return viewElement;
  551. }
  552. /**
  553. * Function factory that creates a converter which converts a non-collapsed {@link module:engine/model/selection~Selection model selection}
  554. * to a {@link module:engine/view/documentselection~DocumentSelection view selection}. The converter consumes appropriate
  555. * value from the `consumable` object and maps model positions from the selection to view positions.
  556. *
  557. * modelDispatcher.on( 'selection', convertRangeSelection() );
  558. *
  559. * @returns {Function} Selection converter.
  560. */
  561. export function convertRangeSelection() {
  562. return ( evt, data, conversionApi ) => {
  563. const selection = data.selection;
  564. if ( selection.isCollapsed ) {
  565. return;
  566. }
  567. if ( !conversionApi.consumable.consume( selection, 'selection' ) ) {
  568. return;
  569. }
  570. const viewRanges = [];
  571. for ( const range of selection.getRanges() ) {
  572. const viewRange = conversionApi.mapper.toViewRange( range );
  573. viewRanges.push( viewRange );
  574. }
  575. conversionApi.writer.setSelection( viewRanges, { backward: selection.isBackward } );
  576. };
  577. }
  578. /**
  579. * Function factory that creates a converter which converts a collapsed {@link module:engine/model/selection~Selection model selection} to
  580. * a {@link module:engine/view/documentselection~DocumentSelection view selection}. The converter consumes appropriate
  581. * value from the `consumable` object, maps the model selection position to the view position and breaks
  582. * {@link module:engine/view/attributeelement~AttributeElement attribute elements} at the selection position.
  583. *
  584. * modelDispatcher.on( 'selection', convertCollapsedSelection() );
  585. *
  586. * An example of the view state before and after converting the collapsed selection:
  587. *
  588. * <p><strong>f^oo<strong>bar</p>
  589. * -> <p><strong>f</strong>^<strong>oo</strong>bar</p>
  590. *
  591. * By breaking attribute elements like `<strong>`, the selection is in a correct element. Then, when the selection attribute is
  592. * converted, broken attributes might be merged again, or the position where the selection is may be wrapped
  593. * with different, appropriate attribute elements.
  594. *
  595. * See also {@link module:engine/conversion/downcasthelpers~clearAttributes} which does a clean-up
  596. * by merging attributes.
  597. *
  598. * @returns {Function} Selection converter.
  599. */
  600. export function convertCollapsedSelection() {
  601. return ( evt, data, conversionApi ) => {
  602. const selection = data.selection;
  603. if ( !selection.isCollapsed ) {
  604. return;
  605. }
  606. if ( !conversionApi.consumable.consume( selection, 'selection' ) ) {
  607. return;
  608. }
  609. const viewWriter = conversionApi.writer;
  610. const modelPosition = selection.getFirstPosition();
  611. const viewPosition = conversionApi.mapper.toViewPosition( modelPosition );
  612. const brokenPosition = viewWriter.breakAttributes( viewPosition );
  613. viewWriter.setSelection( brokenPosition );
  614. };
  615. }
  616. /**
  617. * Function factory that creates a converter which clears artifacts after the previous
  618. * {@link module:engine/model/selection~Selection model selection} conversion. It removes all empty
  619. * {@link module:engine/view/attributeelement~AttributeElement view attribute elements} and merges sibling attributes at all start and end
  620. * positions of all ranges.
  621. *
  622. * <p><strong>^</strong></p>
  623. * -> <p>^</p>
  624. *
  625. * <p><strong>foo</strong>^<strong>bar</strong>bar</p>
  626. * -> <p><strong>foo^bar<strong>bar</p>
  627. *
  628. * <p><strong>foo</strong><em>^</em><strong>bar</strong>bar</p>
  629. * -> <p><strong>foo^bar<strong>bar</p>
  630. *
  631. * This listener should be assigned before any converter for the new selection:
  632. *
  633. * modelDispatcher.on( 'selection', clearAttributes() );
  634. *
  635. * See {@link module:engine/conversion/downcasthelpers~convertCollapsedSelection}
  636. * which does the opposite by breaking attributes in the selection position.
  637. *
  638. * @returns {Function} Selection converter.
  639. */
  640. export function clearAttributes() {
  641. return ( evt, data, conversionApi ) => {
  642. const viewWriter = conversionApi.writer;
  643. const viewSelection = viewWriter.document.selection;
  644. for ( const range of viewSelection.getRanges() ) {
  645. // Not collapsed selection should not have artifacts.
  646. if ( range.isCollapsed ) {
  647. // Position might be in the node removed by the view writer.
  648. if ( range.end.parent.isAttached() ) {
  649. conversionApi.writer.mergeAttributes( range.start );
  650. }
  651. }
  652. }
  653. viewWriter.setSelection( null );
  654. };
  655. }
  656. /**
  657. * Function factory that creates a converter which converts set/change/remove attribute changes from the model to the view.
  658. * It can also be used to convert selection attributes. In that case, an empty attribute element will be created and the
  659. * selection will be put inside it.
  660. *
  661. * Attributes from the model are converted to a view element that will be wrapping these view nodes that are bound to
  662. * model elements having the given attribute. This is useful for attributes like `bold` that may be set on text nodes in the model
  663. * but are represented as an element in the view:
  664. *
  665. * [paragraph] MODEL ====> VIEW <p>
  666. * |- a {bold: true} |- <b>
  667. * |- b {bold: true} | |- ab
  668. * |- c |- c
  669. *
  670. * Passed `Function` will be provided with the attribute value and then all the parameters of the
  671. * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute `attribute` event}.
  672. * It is expected that the function returns an {@link module:engine/view/element~Element}.
  673. * The result of the function will be the wrapping element.
  674. * When the provided `Function` does not return any element, no conversion will take place.
  675. *
  676. * The converter automatically consumes the corresponding value from the consumables list and stops the event (see
  677. * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher}).
  678. *
  679. * modelDispatcher.on( 'attribute:bold', wrap( ( modelAttributeValue, { writer } ) => {
  680. * return writer.createAttributeElement( 'strong' );
  681. * } );
  682. *
  683. * @protected
  684. * @param {Function} elementCreator Function returning a view element that will be used for wrapping.
  685. * @returns {Function} Set/change attribute converter.
  686. */
  687. export function wrap( elementCreator ) {
  688. return ( evt, data, conversionApi ) => {
  689. // Recreate current wrapping node. It will be used to unwrap view range if the attribute value has changed
  690. // or the attribute was removed.
  691. const oldViewElement = elementCreator( data.attributeOldValue, conversionApi );
  692. // Create node to wrap with.
  693. const newViewElement = elementCreator( data.attributeNewValue, conversionApi );
  694. if ( !oldViewElement && !newViewElement ) {
  695. return;
  696. }
  697. if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
  698. return;
  699. }
  700. const viewWriter = conversionApi.writer;
  701. const viewSelection = viewWriter.document.selection;
  702. if ( data.item instanceof ModelSelection || data.item instanceof DocumentSelection ) {
  703. // Selection attribute conversion.
  704. viewWriter.wrap( viewSelection.getFirstRange(), newViewElement );
  705. } else {
  706. // Node attribute conversion.
  707. let viewRange = conversionApi.mapper.toViewRange( data.range );
  708. // First, unwrap the range from current wrapper.
  709. if ( data.attributeOldValue !== null && oldViewElement ) {
  710. viewRange = viewWriter.unwrap( viewRange, oldViewElement );
  711. }
  712. if ( data.attributeNewValue !== null && newViewElement ) {
  713. viewWriter.wrap( viewRange, newViewElement );
  714. }
  715. }
  716. };
  717. }
  718. /**
  719. * Function factory that creates a converter which converts node insertion changes from the model to the view.
  720. * The function passed will be provided with all the parameters of the dispatcher's
  721. * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:insert `insert` event}.
  722. * It is expected that the function returns an {@link module:engine/view/element~Element}.
  723. * The result of the function will be inserted into the view.
  724. *
  725. * The converter automatically consumes the corresponding value from the consumables list, stops the event (see
  726. * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher}) and binds the model and view elements.
  727. *
  728. * downcastDispatcher.on(
  729. * 'insert:myElem',
  730. * insertElement( ( modelItem, { writer } ) => {
  731. * const text = writer.createText( 'myText' );
  732. * const myElem = writer.createElement( 'myElem', { myAttr: 'my-' + modelItem.getAttribute( 'myAttr' ) }, text );
  733. *
  734. * // Do something fancy with `myElem` using `modelItem` or other parameters.
  735. *
  736. * return myElem;
  737. * }
  738. * ) );
  739. *
  740. * @protected
  741. * @param {Function} elementCreator Function returning a view element, which will be inserted.
  742. * @returns {Function} Insert element event converter.
  743. */
  744. export function insertElement( elementCreator ) {
  745. return ( evt, data, conversionApi ) => {
  746. const viewElement = elementCreator( data.item, conversionApi );
  747. if ( !viewElement ) {
  748. return;
  749. }
  750. if ( !conversionApi.consumable.consume( data.item, 'insert' ) ) {
  751. return;
  752. }
  753. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  754. conversionApi.mapper.bindElements( data.item, viewElement );
  755. conversionApi.writer.insert( viewPosition, viewElement );
  756. };
  757. }
  758. /**
  759. * Function factory that creates a converter which converts marker adding change to the
  760. * {@link module:engine/view/uielement~UIElement view UI element}.
  761. *
  762. * The view UI element that will be added to the view depends on the passed parameter. See {@link ~insertElement}.
  763. * In case of a non-collapsed range, the UI element will not wrap nodes but separate elements will be placed at the beginning
  764. * and at the end of the range.
  765. *
  766. * This converter binds created UI elements with the marker name using {@link module:engine/conversion/mapper~Mapper#bindElementToMarker}.
  767. *
  768. * @protected
  769. * @param {module:engine/view/uielement~UIElement|Function} elementCreator A view UI element or a function returning the view element
  770. * that will be inserted.
  771. * @returns {Function} Insert element event converter.
  772. */
  773. export function insertUIElement( elementCreator ) {
  774. return ( evt, data, conversionApi ) => {
  775. // Create two view elements. One will be inserted at the beginning of marker, one at the end.
  776. // If marker is collapsed, only "opening" element will be inserted.
  777. data.isOpening = true;
  778. const viewStartElement = elementCreator( data, conversionApi );
  779. data.isOpening = false;
  780. const viewEndElement = elementCreator( data, conversionApi );
  781. if ( !viewStartElement || !viewEndElement ) {
  782. return;
  783. }
  784. const markerRange = data.markerRange;
  785. // Marker that is collapsed has consumable build differently that non-collapsed one.
  786. // For more information see `addMarker` event description.
  787. // If marker's range is collapsed - check if it can be consumed.
  788. if ( markerRange.isCollapsed && !conversionApi.consumable.consume( markerRange, evt.name ) ) {
  789. return;
  790. }
  791. // If marker's range is not collapsed - consume all items inside.
  792. for ( const value of markerRange ) {
  793. if ( !conversionApi.consumable.consume( value.item, evt.name ) ) {
  794. return;
  795. }
  796. }
  797. const mapper = conversionApi.mapper;
  798. const viewWriter = conversionApi.writer;
  799. // Add "opening" element.
  800. viewWriter.insert( mapper.toViewPosition( markerRange.start ), viewStartElement );
  801. conversionApi.mapper.bindElementToMarker( viewStartElement, data.markerName );
  802. // Add "closing" element only if range is not collapsed.
  803. if ( !markerRange.isCollapsed ) {
  804. viewWriter.insert( mapper.toViewPosition( markerRange.end ), viewEndElement );
  805. conversionApi.mapper.bindElementToMarker( viewEndElement, data.markerName );
  806. }
  807. evt.stop();
  808. };
  809. }
  810. // Function factory that returns a default downcast converter for removing a {@link module:engine/view/uielement~UIElement UI element}
  811. // based on marker remove change.
  812. //
  813. // This converter unbinds elements from the marker name.
  814. //
  815. // @returns {Function} Removed UI element converter.
  816. function removeUIElement() {
  817. return ( evt, data, conversionApi ) => {
  818. const elements = conversionApi.mapper.markerNameToElements( data.markerName );
  819. if ( !elements ) {
  820. return;
  821. }
  822. for ( const element of elements ) {
  823. conversionApi.mapper.unbindElementFromMarkerName( element, data.markerName );
  824. conversionApi.writer.clear( conversionApi.writer.createRangeOn( element ), element );
  825. }
  826. conversionApi.writer.clearClonedElementsGroup( data.markerName );
  827. evt.stop();
  828. };
  829. }
  830. // Function factory that creates a default converter for model markers.
  831. //
  832. // See {@link DowncastHelpers#markerToData} for more information what type of view is generated.
  833. //
  834. // This converter binds created UI elements and affected view elements with the marker name
  835. // using {@link module:engine/conversion/mapper~Mapper#bindElementToMarker}.
  836. //
  837. // @returns {Function} Add marker converter.
  838. function insertMarkerData( viewCreator ) {
  839. return ( evt, data, conversionApi ) => {
  840. const viewMarkerData = viewCreator( data.markerName, conversionApi );
  841. if ( !viewMarkerData ) {
  842. return;
  843. }
  844. const markerRange = data.markerRange;
  845. if ( !conversionApi.consumable.consume( markerRange, evt.name ) ) {
  846. return;
  847. }
  848. // Adding closing data first to keep the proper order in the view.
  849. handleMarkerBoundary( markerRange, false, conversionApi, data, viewMarkerData );
  850. handleMarkerBoundary( markerRange, true, conversionApi, data, viewMarkerData );
  851. evt.stop();
  852. };
  853. }
  854. // Helper function for `insertMarkerData()` that marks a marker boundary at the beginning or end of given `range`.
  855. function handleMarkerBoundary( range, isStart, conversionApi, data, viewMarkerData ) {
  856. const modelPosition = isStart ? range.start : range.end;
  857. const canInsertElement = conversionApi.schema.checkChild( modelPosition, '$text' );
  858. if ( canInsertElement ) {
  859. const viewPosition = conversionApi.mapper.toViewPosition( modelPosition );
  860. insertMarkerAsElement( viewPosition, isStart, conversionApi, data, viewMarkerData );
  861. } else {
  862. let modelElement;
  863. let isBefore;
  864. // If possible, we want to add `data-group-start-before` and `data-group-end-after` attributes.
  865. // Below `if` is constructed in a way that will favor adding these attributes.
  866. //
  867. // Also, I assume that there will be always an element either after or before the position.
  868. // If not, then it is a case when we are not in a position where text is allowed and also there are no elements around...
  869. if ( isStart && modelPosition.nodeAfter || !isStart && !modelPosition.nodeBefore ) {
  870. modelElement = modelPosition.nodeAfter;
  871. isBefore = true;
  872. } else {
  873. modelElement = modelPosition.nodeBefore;
  874. isBefore = false;
  875. }
  876. const viewElement = conversionApi.mapper.toViewElement( modelElement );
  877. insertMarkerAsAttribute( viewElement, isStart, isBefore, conversionApi, data, viewMarkerData );
  878. }
  879. }
  880. // Helper function for `insertMarkerData()` that marks a marker boundary in the view as an attribute on a view element.
  881. function insertMarkerAsAttribute( viewElement, isStart, isBefore, conversionApi, data, viewMarkerData ) {
  882. const attributeName = `data-${ viewMarkerData.group }-${ isStart ? 'start' : 'end' }-${ isBefore ? 'before' : 'after' }`;
  883. const markerNames = viewElement.hasAttribute( attributeName ) ? viewElement.getAttribute( attributeName ).split( ',' ) : [];
  884. // Adding marker name at the beginning to have the same order in the attribute as there is with marker elements.
  885. markerNames.unshift( viewMarkerData.name );
  886. conversionApi.writer.setAttribute( attributeName, markerNames.join( ',' ), viewElement );
  887. conversionApi.mapper.bindElementToMarker( viewElement, data.markerName );
  888. }
  889. // Helper function for `insertMarkerData()` that marks a marker boundary in the view as a separate view ui element.
  890. function insertMarkerAsElement( position, isStart, conversionApi, data, viewMarkerData ) {
  891. const viewElementName = `${ viewMarkerData.group }-${ isStart ? 'start' : 'end' }`;
  892. const attrs = viewMarkerData.name ? { 'name': viewMarkerData.name } : null;
  893. const viewElement = conversionApi.writer.createUIElement( viewElementName, attrs );
  894. conversionApi.writer.insert( position, viewElement );
  895. conversionApi.mapper.bindElementToMarker( viewElement, data.markerName );
  896. }
  897. // Function factory that creates a converter for removing a model marker data added by the {@link #insertMarkerData} converter.
  898. //
  899. // @returns {Function} Remove marker converter.
  900. function removeMarkerData( viewCreator ) {
  901. return ( evt, data, conversionApi ) => {
  902. const viewData = viewCreator( data.markerName, conversionApi );
  903. if ( !viewData ) {
  904. return;
  905. }
  906. const elements = conversionApi.mapper.markerNameToElements( data.markerName );
  907. if ( !elements ) {
  908. return;
  909. }
  910. for ( const element of elements ) {
  911. conversionApi.mapper.unbindElementFromMarkerName( element, data.markerName );
  912. if ( element.is( 'containerElement' ) ) {
  913. removeMarkerFromAttribute( `data-${ viewData.group }-start-before`, element );
  914. removeMarkerFromAttribute( `data-${ viewData.group }-start-after`, element );
  915. removeMarkerFromAttribute( `data-${ viewData.group }-end-before`, element );
  916. removeMarkerFromAttribute( `data-${ viewData.group }-end-after`, element );
  917. } else {
  918. conversionApi.writer.clear( conversionApi.writer.createRangeOn( element ), element );
  919. }
  920. }
  921. conversionApi.writer.clearClonedElementsGroup( data.markerName );
  922. evt.stop();
  923. function removeMarkerFromAttribute( attributeName, element ) {
  924. if ( element.hasAttribute( attributeName ) ) {
  925. const markerNames = new Set( element.getAttribute( attributeName ).split( ',' ) );
  926. markerNames.delete( viewData.name );
  927. if ( markerNames.size == 0 ) {
  928. conversionApi.writer.removeAttribute( attributeName, element );
  929. } else {
  930. conversionApi.writer.setAttribute( attributeName, Array.from( markerNames ).join( ',' ), element );
  931. }
  932. }
  933. }
  934. };
  935. }
  936. // Function factory that creates a converter which converts set/change/remove attribute changes from the model to the view.
  937. //
  938. // Attributes from the model are converted to the view element attributes in the view. You may provide a custom function to generate
  939. // a key-value attribute pair to add/change/remove. If not provided, model attributes will be converted to view element
  940. // attributes on a one-to-one basis.
  941. //
  942. // *Note:** The provided attribute creator should always return the same `key` for a given attribute from the model.
  943. //
  944. // The converter automatically consumes the corresponding value from the consumables list and stops the event (see
  945. // {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher}).
  946. //
  947. // modelDispatcher.on( 'attribute:customAttr:myElem', changeAttribute( ( value, data ) => {
  948. // // Change attribute key from `customAttr` to `class` in the view.
  949. // const key = 'class';
  950. // let value = data.attributeNewValue;
  951. //
  952. // // Force attribute value to 'empty' if the model element is empty.
  953. // if ( data.item.childCount === 0 ) {
  954. // value = 'empty';
  955. // }
  956. //
  957. // // Return the key-value pair.
  958. // return { key, value };
  959. // } ) );
  960. //
  961. // @param {Function} [attributeCreator] Function returning an object with two properties: `key` and `value`, which
  962. // represent the attribute key and attribute value to be set on a {@link module:engine/view/element~Element view element}.
  963. // The function is passed the model attribute value as the first parameter and additional data about the change as the second parameter.
  964. // @returns {Function} Set/change attribute converter.
  965. function changeAttribute( attributeCreator ) {
  966. return ( evt, data, conversionApi ) => {
  967. const oldAttribute = attributeCreator( data.attributeOldValue, conversionApi );
  968. const newAttribute = attributeCreator( data.attributeNewValue, conversionApi );
  969. if ( !oldAttribute && !newAttribute ) {
  970. return;
  971. }
  972. if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
  973. return;
  974. }
  975. const viewElement = conversionApi.mapper.toViewElement( data.item );
  976. const viewWriter = conversionApi.writer;
  977. // If model item cannot be mapped to a view element, it means item is not an `Element` instance but a `TextProxy` node.
  978. // Only elements can have attributes in a view so do not proceed for anything else (#1587).
  979. if ( !viewElement ) {
  980. /**
  981. * This error occurs when a {@link module:engine/model/textproxy~TextProxy text node's} attribute is to be downcasted
  982. * by {@link module:engine/conversion/conversion~Conversion#attributeToAttribute `Attribute to Attribute converter`}.
  983. * In most cases it is caused by converters misconfiguration when only "generic" converter is defined:
  984. *
  985. * editor.conversion.for( 'downcast' ).attributeToAttribute( {
  986. * model: 'attribute-name',
  987. * view: 'attribute-name'
  988. * } ) );
  989. *
  990. * and given attribute is used on text node, for example:
  991. *
  992. * model.change( writer => {
  993. * writer.insertText( 'Foo', { 'attribute-name': 'bar' }, parent, 0 );
  994. * } );
  995. *
  996. * In such cases, to convert the same attribute for both {@link module:engine/model/element~Element}
  997. * and {@link module:engine/model/textproxy~TextProxy `Text`} nodes, text specific
  998. * {@link module:engine/conversion/conversion~Conversion#attributeToElement `Attribute to Element converter`}
  999. * with higher {@link module:utils/priorities~PriorityString priority} must also be defined:
  1000. *
  1001. * editor.conversion.for( 'downcast' ).attributeToElement( {
  1002. * model: {
  1003. * key: 'attribute-name',
  1004. * name: '$text'
  1005. * },
  1006. * view: ( value, { writer } ) => {
  1007. * return writer.createAttributeElement( 'span', { 'attribute-name': value } );
  1008. * },
  1009. * converterPriority: 'high'
  1010. * } ) );
  1011. *
  1012. * @error conversion-attribute-to-attribute-on-text
  1013. */
  1014. throw new CKEditorError(
  1015. 'conversion-attribute-to-attribute-on-text: ' +
  1016. 'Trying to convert text node\'s attribute with attribute-to-attribute converter.',
  1017. [ data, conversionApi ]
  1018. );
  1019. }
  1020. // First remove the old attribute if there was one.
  1021. if ( data.attributeOldValue !== null && oldAttribute ) {
  1022. if ( oldAttribute.key == 'class' ) {
  1023. const classes = Array.isArray( oldAttribute.value ) ? oldAttribute.value : [ oldAttribute.value ];
  1024. for ( const className of classes ) {
  1025. viewWriter.removeClass( className, viewElement );
  1026. }
  1027. } else if ( oldAttribute.key == 'style' ) {
  1028. const keys = Object.keys( oldAttribute.value );
  1029. for ( const key of keys ) {
  1030. viewWriter.removeStyle( key, viewElement );
  1031. }
  1032. } else {
  1033. viewWriter.removeAttribute( oldAttribute.key, viewElement );
  1034. }
  1035. }
  1036. // Then set the new attribute.
  1037. if ( data.attributeNewValue !== null && newAttribute ) {
  1038. if ( newAttribute.key == 'class' ) {
  1039. const classes = Array.isArray( newAttribute.value ) ? newAttribute.value : [ newAttribute.value ];
  1040. for ( const className of classes ) {
  1041. viewWriter.addClass( className, viewElement );
  1042. }
  1043. } else if ( newAttribute.key == 'style' ) {
  1044. const keys = Object.keys( newAttribute.value );
  1045. for ( const key of keys ) {
  1046. viewWriter.setStyle( key, newAttribute.value[ key ], viewElement );
  1047. }
  1048. } else {
  1049. viewWriter.setAttribute( newAttribute.key, newAttribute.value, viewElement );
  1050. }
  1051. }
  1052. };
  1053. }
  1054. // Function factory that creates a converter which converts the text inside marker's range. The converter wraps the text with
  1055. // {@link module:engine/view/attributeelement~AttributeElement} created from the provided descriptor.
  1056. // See {link module:engine/conversion/downcasthelpers~createViewElementFromHighlightDescriptor}.
  1057. //
  1058. // It can also be used to convert the selection that is inside a marker. In that case, an empty attribute element will be
  1059. // created and the selection will be put inside it.
  1060. //
  1061. // If the highlight descriptor does not provide the `priority` property, `10` will be used.
  1062. //
  1063. // If the highlight descriptor does not provide the `id` property, the name of the marker will be used.
  1064. //
  1065. // This converter binds the created {@link module:engine/view/attributeelement~AttributeElement attribute elemens} with the marker name
  1066. // using the {@link module:engine/conversion/mapper~Mapper#bindElementToMarker} method.
  1067. //
  1068. // @param {module:engine/conversion/downcasthelpers~HighlightDescriptor|Function} highlightDescriptor
  1069. // @returns {Function}
  1070. function highlightText( highlightDescriptor ) {
  1071. return ( evt, data, conversionApi ) => {
  1072. if ( !data.item ) {
  1073. return;
  1074. }
  1075. if ( !( data.item instanceof ModelSelection || data.item instanceof DocumentSelection ) && !data.item.is( '$textProxy' ) ) {
  1076. return;
  1077. }
  1078. const descriptor = prepareDescriptor( highlightDescriptor, data, conversionApi );
  1079. if ( !descriptor ) {
  1080. return;
  1081. }
  1082. if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
  1083. return;
  1084. }
  1085. const viewWriter = conversionApi.writer;
  1086. const viewElement = createViewElementFromHighlightDescriptor( viewWriter, descriptor );
  1087. const viewSelection = viewWriter.document.selection;
  1088. if ( data.item instanceof ModelSelection || data.item instanceof DocumentSelection ) {
  1089. viewWriter.wrap( viewSelection.getFirstRange(), viewElement, viewSelection );
  1090. } else {
  1091. const viewRange = conversionApi.mapper.toViewRange( data.range );
  1092. const rangeAfterWrap = viewWriter.wrap( viewRange, viewElement );
  1093. for ( const element of rangeAfterWrap.getItems() ) {
  1094. if ( element.is( 'attributeElement' ) && element.isSimilar( viewElement ) ) {
  1095. conversionApi.mapper.bindElementToMarker( element, data.markerName );
  1096. // One attribute element is enough, because all of them are bound together by the view writer.
  1097. // Mapper uses this binding to get all the elements no matter how many of them are registered in the mapper.
  1098. break;
  1099. }
  1100. }
  1101. }
  1102. };
  1103. }
  1104. // Converter function factory. It creates a function which applies the marker's highlight to an element inside the marker's range.
  1105. //
  1106. // The converter checks if an element has the `addHighlight` function stored as a
  1107. // {@link module:engine/view/element~Element#_setCustomProperty custom property} and, if so, uses it to apply the highlight.
  1108. // In such case the converter will consume all element's children, assuming that they were handled by the element itself.
  1109. //
  1110. // When the `addHighlight` custom property is not present, the element is not converted in any special way.
  1111. // This means that converters will proceed to convert the element's child nodes.
  1112. //
  1113. // If the highlight descriptor does not provide the `priority` property, `10` will be used.
  1114. //
  1115. // If the highlight descriptor does not provide the `id` property, the name of the marker will be used.
  1116. //
  1117. // This converter binds altered {@link module:engine/view/containerelement~ContainerElement container elements} with the marker name using
  1118. // the {@link module:engine/conversion/mapper~Mapper#bindElementToMarker} method.
  1119. //
  1120. // @param {module:engine/conversion/downcasthelpers~HighlightDescriptor|Function} highlightDescriptor
  1121. // @returns {Function}
  1122. function highlightElement( highlightDescriptor ) {
  1123. return ( evt, data, conversionApi ) => {
  1124. if ( !data.item ) {
  1125. return;
  1126. }
  1127. if ( !( data.item instanceof ModelElement ) ) {
  1128. return;
  1129. }
  1130. const descriptor = prepareDescriptor( highlightDescriptor, data, conversionApi );
  1131. if ( !descriptor ) {
  1132. return;
  1133. }
  1134. if ( !conversionApi.consumable.test( data.item, evt.name ) ) {
  1135. return;
  1136. }
  1137. const viewElement = conversionApi.mapper.toViewElement( data.item );
  1138. if ( viewElement && viewElement.getCustomProperty( 'addHighlight' ) ) {
  1139. // Consume element itself.
  1140. conversionApi.consumable.consume( data.item, evt.name );
  1141. // Consume all children nodes.
  1142. for ( const value of ModelRange._createIn( data.item ) ) {
  1143. conversionApi.consumable.consume( value.item, evt.name );
  1144. }
  1145. viewElement.getCustomProperty( 'addHighlight' )( viewElement, descriptor, conversionApi.writer );
  1146. conversionApi.mapper.bindElementToMarker( viewElement, data.markerName );
  1147. }
  1148. };
  1149. }
  1150. // Function factory that creates a converter which converts the removing model marker to the view.
  1151. //
  1152. // Both text nodes and elements are handled by this converter but they are handled a bit differently.
  1153. //
  1154. // Text nodes are unwrapped using the {@link module:engine/view/attributeelement~AttributeElement attribute element} created from the
  1155. // provided highlight descriptor. See {link module:engine/conversion/downcasthelpers~HighlightDescriptor}.
  1156. //
  1157. // For elements, the converter checks if an element has the `removeHighlight` function stored as a
  1158. // {@link module:engine/view/element~Element#_setCustomProperty custom property}. If so, it uses it to remove the highlight.
  1159. // In such case, the children of that element will not be converted.
  1160. //
  1161. // When `removeHighlight` is not present, the element is not converted in any special way.
  1162. // The converter will proceed to convert the element's child nodes instead.
  1163. //
  1164. // If the highlight descriptor does not provide the `priority` property, `10` will be used.
  1165. //
  1166. // If the highlight descriptor does not provide the `id` property, the name of the marker will be used.
  1167. //
  1168. // This converter unbinds elements from the marker name.
  1169. //
  1170. // @param {module:engine/conversion/downcasthelpers~HighlightDescriptor|Function} highlightDescriptor
  1171. // @returns {Function}
  1172. function removeHighlight( highlightDescriptor ) {
  1173. return ( evt, data, conversionApi ) => {
  1174. // This conversion makes sense only for non-collapsed range.
  1175. if ( data.markerRange.isCollapsed ) {
  1176. return;
  1177. }
  1178. const descriptor = prepareDescriptor( highlightDescriptor, data, conversionApi );
  1179. if ( !descriptor ) {
  1180. return;
  1181. }
  1182. // View element that will be used to unwrap `AttributeElement`s.
  1183. const viewHighlightElement = createViewElementFromHighlightDescriptor( conversionApi.writer, descriptor );
  1184. // Get all elements bound with given marker name.
  1185. const elements = conversionApi.mapper.markerNameToElements( data.markerName );
  1186. if ( !elements ) {
  1187. return;
  1188. }
  1189. for ( const element of elements ) {
  1190. conversionApi.mapper.unbindElementFromMarkerName( element, data.markerName );
  1191. if ( element.is( 'attributeElement' ) ) {
  1192. conversionApi.writer.unwrap( conversionApi.writer.createRangeOn( element ), viewHighlightElement );
  1193. } else {
  1194. // if element.is( 'containerElement' ).
  1195. element.getCustomProperty( 'removeHighlight' )( element, descriptor.id, conversionApi.writer );
  1196. }
  1197. }
  1198. conversionApi.writer.clearClonedElementsGroup( data.markerName );
  1199. evt.stop();
  1200. };
  1201. }
  1202. // Model element to view element conversion helper.
  1203. //
  1204. // See {@link ~DowncastHelpers#elementToElement `.elementToElement()` downcast helper} for examples.
  1205. //
  1206. // @param {Object} config Conversion configuration.
  1207. // @param {String} config.model The name of the model element to convert.
  1208. // @param {module:engine/view/elementdefinition~ElementDefinition|Function} config.view A view element definition or a function
  1209. // that takes the model element and {@link module:engine/view/downcastwriter~DowncastWriter view downcast writer}
  1210. // as parameters and returns a view container element.
  1211. // @returns {Function} Conversion helper.
  1212. function downcastElementToElement( config ) {
  1213. config = cloneDeep( config );
  1214. config.view = normalizeToElementConfig( config.view, 'container' );
  1215. return dispatcher => {
  1216. dispatcher.on( 'insert:' + config.model, insertElement( config.view ), { priority: config.converterPriority || 'normal' } );
  1217. };
  1218. }
  1219. // Model attribute to view element conversion helper.
  1220. //
  1221. // See {@link ~DowncastHelpers#attributeToElement `.attributeToElement()` downcast helper} for examples.
  1222. //
  1223. // @param {Object} config Conversion configuration.
  1224. // @param {String|Object} config.model The key of the attribute to convert from or a `{ key, values }` object. `values` is an array
  1225. // of `String`s with possible values if the model attribute is an enumerable.
  1226. // @param {module:engine/view/elementdefinition~ElementDefinition|Function|Object} config.view A view element definition or a function
  1227. // that takes the model attribute value and {@link module:engine/view/downcastwriter~DowncastWriter view downcast writer}
  1228. // as parameters and returns a view attribute element. If `config.model.values` is
  1229. // given, `config.view` should be an object assigning values from `config.model.values` to view element definitions or functions.
  1230. // @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
  1231. // @returns {Function} Conversion helper.
  1232. function downcastAttributeToElement( config ) {
  1233. config = cloneDeep( config );
  1234. const modelKey = config.model.key ? config.model.key : config.model;
  1235. let eventName = 'attribute:' + modelKey;
  1236. if ( config.model.name ) {
  1237. eventName += ':' + config.model.name;
  1238. }
  1239. if ( config.model.values ) {
  1240. for ( const modelValue of config.model.values ) {
  1241. config.view[ modelValue ] = normalizeToElementConfig( config.view[ modelValue ], 'attribute' );
  1242. }
  1243. } else {
  1244. config.view = normalizeToElementConfig( config.view, 'attribute' );
  1245. }
  1246. const elementCreator = getFromAttributeCreator( config );
  1247. return dispatcher => {
  1248. dispatcher.on( eventName, wrap( elementCreator ), { priority: config.converterPriority || 'normal' } );
  1249. };
  1250. }
  1251. // Model attribute to view attribute conversion helper.
  1252. //
  1253. // See {@link ~DowncastHelpers#attributeToAttribute `.attributeToAttribute()` downcast helper} for examples.
  1254. //
  1255. // @param {Object} config Conversion configuration.
  1256. // @param {String|Object} config.model The key of the attribute to convert from or a `{ key, values, [ name ] }` object describing
  1257. // the attribute key, possible values and, optionally, an element name to convert from.
  1258. // @param {String|Object|Function} config.view A view attribute key, or a `{ key, value }` object or a function that takes
  1259. // the model attribute value and returns a `{ key, value }` object. If `key` is `'class'`, `value` can be a `String` or an
  1260. // array of `String`s. If `key` is `'style'`, `value` is an object with key-value pairs. In other cases, `value` is a `String`.
  1261. // If `config.model.values` is set, `config.view` should be an object assigning values from `config.model.values` to
  1262. // `{ key, value }` objects or a functions.
  1263. // @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
  1264. // @returns {Function} Conversion helper.
  1265. function downcastAttributeToAttribute( config ) {
  1266. config = cloneDeep( config );
  1267. const modelKey = config.model.key ? config.model.key : config.model;
  1268. let eventName = 'attribute:' + modelKey;
  1269. if ( config.model.name ) {
  1270. eventName += ':' + config.model.name;
  1271. }
  1272. if ( config.model.values ) {
  1273. for ( const modelValue of config.model.values ) {
  1274. config.view[ modelValue ] = normalizeToAttributeConfig( config.view[ modelValue ] );
  1275. }
  1276. } else {
  1277. config.view = normalizeToAttributeConfig( config.view );
  1278. }
  1279. const elementCreator = getFromAttributeCreator( config );
  1280. return dispatcher => {
  1281. dispatcher.on( eventName, changeAttribute( elementCreator ), { priority: config.converterPriority || 'normal' } );
  1282. };
  1283. }
  1284. // Model marker to view element conversion helper.
  1285. //
  1286. // See {@link ~DowncastHelpers#markerToElement `.markerToElement()` downcast helper} for examples.
  1287. //
  1288. // @param {Object} config Conversion configuration.
  1289. // @param {String} config.model The name of the model marker (or model marker group) to convert.
  1290. // @param {module:engine/view/elementdefinition~ElementDefinition|Function} config.view A view element definition or a function
  1291. // that takes the model marker data as a parameter and returns a view UI element.
  1292. // @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
  1293. // @returns {Function} Conversion helper.
  1294. function downcastMarkerToElement( config ) {
  1295. config = cloneDeep( config );
  1296. config.view = normalizeToElementConfig( config.view, 'ui' );
  1297. return dispatcher => {
  1298. dispatcher.on( 'addMarker:' + config.model, insertUIElement( config.view ), { priority: config.converterPriority || 'normal' } );
  1299. dispatcher.on( 'removeMarker:' + config.model, removeUIElement( config.view ), { priority: config.converterPriority || 'normal' } );
  1300. };
  1301. }
  1302. // Model marker to view data conversion helper.
  1303. //
  1304. // See {@link ~DowncastHelpers#markerToData `markerToData()` downcast helper} to learn more.
  1305. //
  1306. // @param {Object} config
  1307. // @param {String} config.model
  1308. // @param {Function} [config.view]
  1309. // @param {module:utils/priorities~PriorityString} [config.converterPriority='normal']
  1310. // @returns {Function} Conversion helper.
  1311. function downcastMarkerToData( config ) {
  1312. config = cloneDeep( config );
  1313. const group = config.model;
  1314. // Default conversion.
  1315. if ( !config.view ) {
  1316. config.view = markerName => ( {
  1317. group,
  1318. name: markerName.substr( config.model.length + 1 )
  1319. } );
  1320. }
  1321. return dispatcher => {
  1322. dispatcher.on( 'addMarker:' + group, insertMarkerData( config.view ), { priority: config.converterPriority || 'normal' } );
  1323. dispatcher.on( 'removeMarker:' + group, removeMarkerData( config.view ), { priority: config.converterPriority || 'normal' } );
  1324. };
  1325. }
  1326. // Model marker to highlight conversion helper.
  1327. //
  1328. // See {@link ~DowncastHelpers#markerToElement `.markerToElement()` downcast helper} for examples.
  1329. //
  1330. // @param {Object} config Conversion configuration.
  1331. // @param {String} config.model The name of the model marker (or model marker group) to convert.
  1332. // @param {module:engine/conversion/downcasthelpers~HighlightDescriptor|Function} config.view A highlight descriptor
  1333. // that will be used for highlighting or a function that takes the model marker data as a parameter and returns a highlight descriptor.
  1334. // @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
  1335. // @returns {Function} Conversion helper.
  1336. function downcastMarkerToHighlight( config ) {
  1337. return dispatcher => {
  1338. dispatcher.on( 'addMarker:' + config.model, highlightText( config.view ), { priority: config.converterPriority || 'normal' } );
  1339. dispatcher.on( 'addMarker:' + config.model, highlightElement( config.view ), { priority: config.converterPriority || 'normal' } );
  1340. dispatcher.on( 'removeMarker:' + config.model, removeHighlight( config.view ), { priority: config.converterPriority || 'normal' } );
  1341. };
  1342. }
  1343. // Takes `config.view`, and if it is an {@link module:engine/view/elementdefinition~ElementDefinition}, converts it
  1344. // to a function (because lower level converters accept only element creator functions).
  1345. //
  1346. // @param {module:engine/view/elementdefinition~ElementDefinition|Function} view View configuration.
  1347. // @param {'container'|'attribute'|'ui'} viewElementType View element type to create.
  1348. // @returns {Function} Element creator function to use in lower level converters.
  1349. function normalizeToElementConfig( view, viewElementType ) {
  1350. if ( typeof view == 'function' ) {
  1351. // If `view` is already a function, don't do anything.
  1352. return view;
  1353. }
  1354. return ( modelData, conversionApi ) => createViewElementFromDefinition( view, conversionApi, viewElementType );
  1355. }
  1356. // Creates a view element instance from the provided {@link module:engine/view/elementdefinition~ElementDefinition} and class.
  1357. //
  1358. // @param {module:engine/view/elementdefinition~ElementDefinition} viewElementDefinition
  1359. // @param {module:engine/view/downcastwriter~DowncastWriter} viewWriter
  1360. // @param {'container'|'attribute'|'ui'} viewElementType
  1361. // @returns {module:engine/view/element~Element}
  1362. function createViewElementFromDefinition( viewElementDefinition, conversionApi, viewElementType ) {
  1363. if ( typeof viewElementDefinition == 'string' ) {
  1364. // If `viewElementDefinition` is given as a `String`, normalize it to an object with `name` property.
  1365. viewElementDefinition = { name: viewElementDefinition };
  1366. }
  1367. let element;
  1368. const viewWriter = conversionApi.writer;
  1369. const attributes = Object.assign( {}, viewElementDefinition.attributes );
  1370. if ( viewElementType == 'container' ) {
  1371. element = viewWriter.createContainerElement( viewElementDefinition.name, attributes );
  1372. } else if ( viewElementType == 'attribute' ) {
  1373. const options = {
  1374. priority: viewElementDefinition.priority || ViewAttributeElement.DEFAULT_PRIORITY
  1375. };
  1376. element = viewWriter.createAttributeElement( viewElementDefinition.name, attributes, options );
  1377. } else {
  1378. // 'ui'.
  1379. element = viewWriter.createUIElement( viewElementDefinition.name, attributes );
  1380. }
  1381. if ( viewElementDefinition.styles ) {
  1382. const keys = Object.keys( viewElementDefinition.styles );
  1383. for ( const key of keys ) {
  1384. viewWriter.setStyle( key, viewElementDefinition.styles[ key ], element );
  1385. }
  1386. }
  1387. if ( viewElementDefinition.classes ) {
  1388. const classes = viewElementDefinition.classes;
  1389. if ( typeof classes == 'string' ) {
  1390. viewWriter.addClass( classes, element );
  1391. } else {
  1392. for ( const className of classes ) {
  1393. viewWriter.addClass( className, element );
  1394. }
  1395. }
  1396. }
  1397. return element;
  1398. }
  1399. function getFromAttributeCreator( config ) {
  1400. if ( config.model.values ) {
  1401. return ( modelAttributeValue, conversionApi ) => {
  1402. const view = config.view[ modelAttributeValue ];
  1403. if ( view ) {
  1404. return view( modelAttributeValue, conversionApi );
  1405. }
  1406. return null;
  1407. };
  1408. } else {
  1409. return config.view;
  1410. }
  1411. }
  1412. // Takes the configuration, adds default parameters if they do not exist and normalizes other parameters to be used in downcast converters
  1413. // for generating a view attribute.
  1414. //
  1415. // @param {Object} view View configuration.
  1416. function normalizeToAttributeConfig( view ) {
  1417. if ( typeof view == 'string' ) {
  1418. return modelAttributeValue => ( { key: view, value: modelAttributeValue } );
  1419. } else if ( typeof view == 'object' ) {
  1420. // { key, value, ... }
  1421. if ( view.value ) {
  1422. return () => view;
  1423. }
  1424. // { key, ... }
  1425. else {
  1426. return modelAttributeValue => ( { key: view.key, value: modelAttributeValue } );
  1427. }
  1428. } else {
  1429. // function.
  1430. return view;
  1431. }
  1432. }
  1433. // Helper function for `highlight`. Prepares the actual descriptor object using value passed to the converter.
  1434. function prepareDescriptor( highlightDescriptor, data, conversionApi ) {
  1435. // If passed descriptor is a creator function, call it. If not, just use passed value.
  1436. const descriptor = typeof highlightDescriptor == 'function' ?
  1437. highlightDescriptor( data, conversionApi ) :
  1438. highlightDescriptor;
  1439. if ( !descriptor ) {
  1440. return null;
  1441. }
  1442. // Apply default descriptor priority.
  1443. if ( !descriptor.priority ) {
  1444. descriptor.priority = 10;
  1445. }
  1446. // Default descriptor id is marker name.
  1447. if ( !descriptor.id ) {
  1448. descriptor.id = data.markerName;
  1449. }
  1450. return descriptor;
  1451. }
  1452. /**
  1453. * An object describing how the marker highlight should be represented in the view.
  1454. *
  1455. * Each text node contained in a highlighted range will be wrapped in a `<span>`
  1456. * {@link module:engine/view/attributeelement~AttributeElement view attribute element} with CSS class(es), attributes and a priority
  1457. * described by this object.
  1458. *
  1459. * Additionally, each {@link module:engine/view/containerelement~ContainerElement container element} can handle displaying the highlight
  1460. * separately by providing the `addHighlight` and `removeHighlight` custom properties. In this case:
  1461. *
  1462. * * The `HighlightDescriptor` object is passed to the `addHighlight` function upon conversion and should be used to apply the highlight to
  1463. * the element.
  1464. * * The descriptor `id` is passed to the `removeHighlight` function upon conversion and should be used to remove the highlight with the
  1465. * given ID from the element.
  1466. *
  1467. * @typedef {Object} module:engine/conversion/downcasthelpers~HighlightDescriptor
  1468. *
  1469. * @property {String|Array.<String>} classes A CSS class or an array of classes to set. If the descriptor is used to
  1470. * create an {@link module:engine/view/attributeelement~AttributeElement attribute element} over text nodes, these classes will be set
  1471. * on that attribute element. If the descriptor is applied to an element, usually these classes will be set on that element, however,
  1472. * this depends on how the element converts the descriptor.
  1473. *
  1474. * @property {String} [id] Descriptor identifier. If not provided, it defaults to the converted marker's name.
  1475. *
  1476. * @property {Number} [priority] Descriptor priority. If not provided, it defaults to `10`. If the descriptor is used to create
  1477. * an {@link module:engine/view/attributeelement~AttributeElement attribute element}, it will be that element's
  1478. * {@link module:engine/view/attributeelement~AttributeElement#priority priority}. If the descriptor is applied to an element,
  1479. * the priority will be used to determine which descriptor is more important.
  1480. *
  1481. * @property {Object} [attributes] Attributes to set. If the descriptor is used to create
  1482. * an {@link module:engine/view/attributeelement~AttributeElement attribute element} over text nodes, these attributes will be set on that
  1483. * attribute element. If the descriptor is applied to an element, usually these attributes will be set on that element, however,
  1484. * this depends on how the element converts the descriptor.
  1485. */