downcasthelpers.js 65 KB

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