8
0

converters.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module list/converters
  7. */
  8. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  9. import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
  10. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  11. import ModelSelection from '@ckeditor/ckeditor5-engine/src/model/selection';
  12. import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
  13. import ViewRange from '@ckeditor/ckeditor5-engine/src/view/range';
  14. import ViewTreeWalker from '@ckeditor/ckeditor5-engine/src/view/treewalker';
  15. import { createViewListItemElement } from './utils';
  16. /**
  17. * A model-to-view converter for `listItem` model element insertion.
  18. *
  19. * It creates a `<ul><li></li><ul>` (or `<ol>`) view structure out of a `listItem` model element, inserts it at the correct
  20. * position, and merges the list with surrounding lists (if available).
  21. *
  22. * @see module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:insert
  23. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  24. * @param {Object} data Additional information about the change.
  25. * @param {Object} conversionApi Conversion interface.
  26. */
  27. export function modelViewInsertion( evt, data, conversionApi ) {
  28. const consumable = conversionApi.consumable;
  29. if ( !consumable.test( data.item, 'insert' ) ||
  30. !consumable.test( data.item, 'attribute:listType' ) ||
  31. !consumable.test( data.item, 'attribute:listIndent' )
  32. ) {
  33. return;
  34. }
  35. consumable.consume( data.item, 'insert' );
  36. consumable.consume( data.item, 'attribute:listType' );
  37. consumable.consume( data.item, 'attribute:listIndent' );
  38. const modelItem = data.item;
  39. const viewItem = generateLiInUl( modelItem, conversionApi );
  40. injectViewList( modelItem, viewItem, conversionApi );
  41. }
  42. /**
  43. * A model-to-view converter for `listItem` model element removal.
  44. *
  45. * @see module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:remove
  46. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  47. * @param {Object} data Additional information about the change.
  48. * @param {Object} conversionApi Conversion interface.
  49. */
  50. export function modelViewRemove( evt, data, conversionApi ) {
  51. const viewStart = conversionApi.mapper.toViewPosition( data.position ).getLastMatchingPosition( value => !value.item.is( 'li' ) );
  52. const viewItem = viewStart.nodeAfter;
  53. const viewWriter = conversionApi.writer;
  54. // 1. Break the container after and before the list item.
  55. // This will create a view list with one view list item - the one to remove.
  56. viewWriter.breakContainer( ViewPosition.createBefore( viewItem ) );
  57. viewWriter.breakContainer( ViewPosition.createAfter( viewItem ) );
  58. // 2. Remove the list with the item to remove.
  59. const viewList = viewItem.parent;
  60. const viewListPrev = viewList.previousSibling;
  61. const removeRange = ViewRange.createOn( viewList );
  62. const removed = viewWriter.remove( removeRange );
  63. // 3. Merge the whole created by breaking and removing the list.
  64. if ( viewListPrev && viewListPrev.nextSibling ) {
  65. mergeViewLists( viewWriter, viewListPrev, viewListPrev.nextSibling );
  66. }
  67. // 4. Bring back nested list that was in the removed <li>.
  68. const modelItem = conversionApi.mapper.toModelElement( viewItem );
  69. hoistNestedLists( modelItem.getAttribute( 'listIndent' ) + 1, data.position, removeRange.start, viewItem, conversionApi );
  70. // 5. Unbind removed view item and all children.
  71. for ( const child of ViewRange.createIn( removed ).getItems() ) {
  72. conversionApi.mapper.unbindViewElement( child );
  73. }
  74. evt.stop();
  75. }
  76. /**
  77. * A model-to-view converter for `type` attribute change on `listItem` model element.
  78. *
  79. * This change means that `<li>` elements parent changes from `<ul>` to `<ol>` (or vice versa). This is accomplished
  80. * by breaking view elements, changing their name and merging them.
  81. *
  82. * @see module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute
  83. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  84. * @param {Object} data Additional information about the change.
  85. * @param {Object} conversionApi Conversion interface.
  86. */
  87. export function modelViewChangeType( evt, data, conversionApi ) {
  88. if ( !conversionApi.consumable.consume( data.item, 'attribute:listType' ) ) {
  89. return;
  90. }
  91. const viewItem = conversionApi.mapper.toViewElement( data.item );
  92. const viewWriter = conversionApi.writer;
  93. // 1. Break the container after and before the list item.
  94. // This will create a view list with one view list item -- the one that changed type.
  95. viewWriter.breakContainer( ViewPosition.createBefore( viewItem ) );
  96. viewWriter.breakContainer( ViewPosition.createAfter( viewItem ) );
  97. // 2. Change name of the view list that holds the changed view item.
  98. // We cannot just change name property, because that would not render properly.
  99. let viewList = viewItem.parent;
  100. const listName = data.attributeNewValue == 'numbered' ? 'ol' : 'ul';
  101. viewList = viewWriter.rename( listName, viewList );
  102. // 3. Merge the changed view list with other lists, if possible.
  103. mergeViewLists( viewWriter, viewList, viewList.nextSibling );
  104. mergeViewLists( viewWriter, viewList.previousSibling, viewList );
  105. // 4. Consumable insertion of children inside the item. They are already handled by re-building the item in view.
  106. for ( const child of data.item.getChildren() ) {
  107. conversionApi.consumable.consume( child, 'insert' );
  108. }
  109. }
  110. /**
  111. * A model-to-view converter for `listIndent` attribute change on `listItem` model element.
  112. *
  113. * @see module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute
  114. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  115. * @param {Object} data Additional information about the change.
  116. * @param {Object} conversionApi Conversion interface.
  117. */
  118. export function modelViewChangeIndent( evt, data, conversionApi ) {
  119. if ( !conversionApi.consumable.consume( data.item, 'attribute:listIndent' ) ) {
  120. return;
  121. }
  122. const viewItem = conversionApi.mapper.toViewElement( data.item );
  123. const viewWriter = conversionApi.writer;
  124. // 1. Break the container after and before the list item.
  125. // This will create a view list with one view list item -- the one that changed type.
  126. viewWriter.breakContainer( ViewPosition.createBefore( viewItem ) );
  127. viewWriter.breakContainer( ViewPosition.createAfter( viewItem ) );
  128. // 2. Extract view list with changed view list item and merge "hole" possibly created by breaking and removing elements.
  129. const viewList = viewItem.parent;
  130. const viewListPrev = viewList.previousSibling;
  131. const removeRange = ViewRange.createOn( viewList );
  132. viewWriter.remove( removeRange );
  133. if ( viewListPrev && viewListPrev.nextSibling ) {
  134. mergeViewLists( viewWriter, viewListPrev, viewListPrev.nextSibling );
  135. }
  136. // 3. Bring back nested list that was in the removed <li>.
  137. hoistNestedLists( data.attributeOldValue + 1, data.range.start, removeRange.start, viewItem, conversionApi );
  138. // 4. Inject view list like it is newly inserted.
  139. injectViewList( data.item, viewItem, conversionApi );
  140. // 5. Consume insertion of children inside the item. They are already handled by re-building the item in view.
  141. for ( const child of data.item.getChildren() ) {
  142. conversionApi.consumable.consume( child, 'insert' );
  143. }
  144. }
  145. /**
  146. * A special model-to-view converter introduced by the {@link module:list/list~List list feature}. This converter is fired for
  147. * insert change of every model item, and should be fired before the actual converter. The converter checks whether the inserted
  148. * model item is a non-`listItem` element. If it is, and it is inserted inside a view list, the converter breaks the
  149. * list so the model element is inserted to the view parent element corresponding to its model parent element.
  150. *
  151. * The converter prevents such situations:
  152. *
  153. * // Model: // View:
  154. * <listItem>foo</listItem> <ul>
  155. * <listItem>bar</listItem> <li>foo</li>
  156. * <li>bar</li>
  157. * </ul>
  158. *
  159. * // After change: // Correct view guaranteed by this converter:
  160. * <listItem>foo</listItem> <ul><li>foo</li></ul><p>xxx</p><ul><li>bar</li></ul>
  161. * <paragraph>xxx</paragraph> // Instead of this wrong view state:
  162. * <listItem>bar</listItem> <ul><li>foo</li><p>xxx</p><li>bar</li></ul>
  163. *
  164. * @see module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:insert
  165. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  166. * @param {Object} data Additional information about the change.
  167. * @param {Object} conversionApi Conversion interface.
  168. */
  169. export function modelViewSplitOnInsert( evt, data, conversionApi ) {
  170. if ( data.item.name != 'listItem' ) {
  171. let viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  172. const viewWriter = conversionApi.writer;
  173. const lists = [];
  174. // Break multiple ULs/OLs if there are.
  175. //
  176. // Imagine following list:
  177. //
  178. // 1 --------
  179. // 1.1 --------
  180. // 1.1.1 --------
  181. // 1.1.2 --------
  182. // 1.1.3 --------
  183. // 1.1.3.1 --------
  184. // 1.2 --------
  185. // 1.2.1 --------
  186. // 2 --------
  187. //
  188. // Insert paragraph after item 1.1.1:
  189. //
  190. // 1 --------
  191. // 1.1 --------
  192. // 1.1.1 --------
  193. //
  194. // Lorem ipsum.
  195. //
  196. // 1.1.2 --------
  197. // 1.1.3 --------
  198. // 1.1.3.1 --------
  199. // 1.2 --------
  200. // 1.2.1 --------
  201. // 2 --------
  202. //
  203. // In this case 1.1.2 has to become beginning of a new list.
  204. // We need to break list before 1.1.2 (obvious), then we need to break list also before 1.2.
  205. // Then we need to move those broken pieces one after another and merge:
  206. //
  207. // 1 --------
  208. // 1.1 --------
  209. // 1.1.1 --------
  210. //
  211. // Lorem ipsum.
  212. //
  213. // 1.1.2 --------
  214. // 1.1.3 --------
  215. // 1.1.3.1 --------
  216. // 1.2 --------
  217. // 1.2.1 --------
  218. // 2 --------
  219. //
  220. while ( viewPosition.parent.name == 'ul' || viewPosition.parent.name == 'ol' ) {
  221. viewPosition = viewWriter.breakContainer( viewPosition );
  222. if ( viewPosition.parent.name != 'li' ) {
  223. break;
  224. }
  225. // Remove lists that are after inserted element.
  226. // They will be brought back later, below the inserted element.
  227. const removeStart = viewPosition;
  228. const removeEnd = ViewPosition.createAt( viewPosition.parent, 'end' );
  229. // Don't remove if there is nothing to remove.
  230. if ( !removeStart.isEqual( removeEnd ) ) {
  231. const removed = viewWriter.remove( new ViewRange( removeStart, removeEnd ) );
  232. lists.push( removed );
  233. }
  234. viewPosition = ViewPosition.createAfter( viewPosition.parent );
  235. }
  236. // Bring back removed lists.
  237. if ( lists.length > 0 ) {
  238. for ( let i = 0; i < lists.length; i++ ) {
  239. const previousList = viewPosition.nodeBefore;
  240. const insertedRange = viewWriter.insert( viewPosition, lists[ i ] );
  241. viewPosition = insertedRange.end;
  242. // Don't merge first list! We want a split in that place (this is why this converter is introduced).
  243. if ( i > 0 ) {
  244. const mergePos = mergeViewLists( viewWriter, previousList, previousList.nextSibling );
  245. // If `mergePos` is in `previousList` it means that the lists got merged.
  246. // In this case, we need to fix insert position.
  247. if ( mergePos && mergePos.parent == previousList ) {
  248. viewPosition.offset--;
  249. }
  250. }
  251. }
  252. // Merge last inserted list with element after it.
  253. mergeViewLists( viewWriter, viewPosition.nodeBefore, viewPosition.nodeAfter );
  254. }
  255. }
  256. }
  257. /**
  258. * A special model-to-view converter introduced by the {@link module:list/list~List list feature}. This converter takes care of
  259. * merging view lists after something is removed or moved from near them.
  260. *
  261. * Example:
  262. *
  263. * // Model: // View:
  264. * <listItem>foo</listItem> <ul><li>foo</li></ul>
  265. * <paragraph>xxx</paragraph> <p>xxx</p>
  266. * <listItem>bar</listItem> <ul><li>bar</li></ul>
  267. *
  268. * // After change: // Correct view guaranteed by this converter:
  269. * <listItem>foo</listItem> <ul>
  270. * <listItem>bar</listItem> <li>foo</li>
  271. * <li>bar</li>
  272. * </ul>
  273. *
  274. * @see module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:remove
  275. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  276. * @param {Object} data Additional information about the change.
  277. * @param {Object} conversionApi Conversion interface.
  278. */
  279. export function modelViewMergeAfter( evt, data, conversionApi ) {
  280. const viewPosition = conversionApi.mapper.toViewPosition( data.position );
  281. const viewItemPrev = viewPosition.nodeBefore;
  282. const viewItemNext = viewPosition.nodeAfter;
  283. // Merge lists if something (remove, move) was done from inside of list.
  284. // Merging will be done only if both items are view lists of the same type.
  285. // The check is done inside the helper function.
  286. mergeViewLists( conversionApi.writer, viewItemPrev, viewItemNext );
  287. }
  288. /**
  289. * A view-to-model converter that converts `<li>` view elements into `listItem` model elements.
  290. *
  291. * To set correct values of the `listType` and `listIndent` attributes the converter:
  292. * * checks `<li>`'s parent,
  293. * * stores and increases the `conversionApi.store.indent` value when `<li>`'s sub-items are converted.
  294. *
  295. * @see module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:element
  296. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  297. * @param {Object} data An object containing conversion input and a placeholder for conversion output and possibly other values.
  298. * @param {Object} conversionApi Conversion interface to be used by the callback.
  299. */
  300. export function viewModelConverter( evt, data, conversionApi ) {
  301. if ( conversionApi.consumable.consume( data.viewItem, { name: true } ) ) {
  302. const writer = conversionApi.writer;
  303. const conversionStore = this.conversionApi.store;
  304. // 1. Create `listItem` model element.
  305. const listItem = writer.createElement( 'listItem' );
  306. // 2. Handle `listItem` model element attributes.
  307. conversionStore.indent = conversionStore.indent || 0;
  308. writer.setAttribute( 'listIndent', conversionStore.indent, listItem );
  309. // Set 'bulleted' as default. If this item is pasted into a context,
  310. const type = data.viewItem.parent && data.viewItem.parent.name == 'ol' ? 'numbered' : 'bulleted';
  311. writer.setAttribute( 'listType', type, listItem );
  312. // `listItem`s created recursively should have bigger indent.
  313. conversionStore.indent++;
  314. // Try to find allowed parent for list item.
  315. const splitResult = conversionApi.splitToAllowedParent( listItem, data.modelCursor );
  316. // When there is no allowed parent it means that list item cannot be converted at current model position
  317. // and in any of position ancestors.
  318. if ( !splitResult ) {
  319. return;
  320. }
  321. writer.insert( listItem, splitResult.position );
  322. const nextPosition = viewToModelListItemChildrenConverter( listItem, data.viewItem.getChildren(), conversionApi );
  323. conversionStore.indent--;
  324. // Result range starts before the first item and ends after the last.
  325. data.modelRange = new ModelRange( data.modelCursor, nextPosition );
  326. // When modelCursor parent had to be split to insert list item.
  327. if ( splitResult.cursorParent ) {
  328. // Then continue conversion in split element.
  329. data.modelCursor = ModelPosition.createAt( splitResult.cursorParent );
  330. } else {
  331. // Otherwise continue conversion after last list item.
  332. data.modelCursor = data.modelRange.end;
  333. }
  334. }
  335. }
  336. /**
  337. * A view-to-model converter for `<ul>` and `<ol>` view elements that cleans the input view of garbage.
  338. * This is mostly to clean whitespaces from between `<li>` view elements inside the view list element, however, also
  339. * incorrect data can be cleared if the view was incorrect.
  340. *
  341. * @see module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:element
  342. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  343. * @param {Object} data An object containing conversion input and a placeholder for conversion output and possibly other values.
  344. * @param {Object} conversionApi Conversion interface to be used by the callback.
  345. */
  346. export function cleanList( evt, data, conversionApi ) {
  347. if ( conversionApi.consumable.test( data.viewItem, { name: true } ) ) {
  348. // Caching children because when we start removing them iterating fails.
  349. const children = Array.from( data.viewItem.getChildren() );
  350. for ( const child of children ) {
  351. if ( !child.is( 'li' ) ) {
  352. child._remove();
  353. }
  354. }
  355. }
  356. }
  357. /**
  358. * A view-to-model converter for `<li>` elements that cleans whitespace formatting from the input view.
  359. *
  360. * @see module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:element
  361. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  362. * @param {Object} data An object containing conversion input and a placeholder for conversion output and possibly other values.
  363. * @param {Object} conversionApi Conversion interface to be used by the callback.
  364. */
  365. export function cleanListItem( evt, data, conversionApi ) {
  366. if ( conversionApi.consumable.test( data.viewItem, { name: true } ) ) {
  367. if ( data.viewItem.childCount === 0 ) {
  368. return;
  369. }
  370. const children = [ ...data.viewItem.getChildren() ];
  371. let foundList = false;
  372. let firstNode = true;
  373. for ( const child of children ) {
  374. if ( foundList && !child.is( 'ul' ) && !child.is( 'ol' ) ) {
  375. child._remove();
  376. }
  377. if ( child.is( 'text' ) ) {
  378. // If this is the first node and it's a text node, left-trim it.
  379. if ( firstNode ) {
  380. child._data = child.data.replace( /^\s+/, '' );
  381. }
  382. // If this is the last text node before <ul> or <ol>, right-trim it.
  383. if ( !child.nextSibling || ( child.nextSibling.is( 'ul' ) || child.nextSibling.is( 'ol' ) ) ) {
  384. child._data = child.data.replace( /\s+$/, '' );
  385. }
  386. } else if ( child.is( 'ul' ) || child.is( 'ol' ) ) {
  387. // If this is a <ul> or <ol>, do not process it, just mark that we already visited list element.
  388. foundList = true;
  389. }
  390. firstNode = false;
  391. }
  392. }
  393. }
  394. /**
  395. * The callback for model position to view position mapping for {@link module:engine/conversion/mapper~Mapper}. The callback fixes
  396. * positions between `listItem` elements that would be incorrectly mapped because of how list items are represented in model
  397. * and view.
  398. *
  399. * @see module:engine/conversion/mapper~Mapper#event:modelToViewPosition
  400. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  401. * @param {Object} data An object containing additional data and placeholder for mapping result.
  402. */
  403. export function modelToViewPosition( evt, data ) {
  404. if ( data.isPhantom ) {
  405. return;
  406. }
  407. const modelItem = data.modelPosition.nodeBefore;
  408. if ( modelItem && modelItem.is( 'listItem' ) ) {
  409. const viewItem = data.mapper.toViewElement( modelItem );
  410. const topmostViewList = viewItem.getAncestors().find( element => element.is( 'ul' ) || element.is( 'ol' ) );
  411. const walker = new ViewTreeWalker( {
  412. startPosition: ViewPosition.createAt( viewItem, 0 )
  413. } );
  414. for ( const value of walker ) {
  415. if ( value.type == 'elementStart' && value.item.is( 'li' ) ) {
  416. data.viewPosition = value.previousPosition;
  417. break;
  418. } else if ( value.type == 'elementEnd' && value.item == topmostViewList ) {
  419. data.viewPosition = value.nextPosition;
  420. break;
  421. }
  422. }
  423. }
  424. }
  425. /**
  426. * The callback for view position to model position mapping for {@link module:engine/conversion/mapper~Mapper}. The callback fixes
  427. * positions between `<li>` elements that would be incorrectly mapped because of how list items are represented in model
  428. * and view.
  429. *
  430. * @see module:engine/conversion/mapper~Mapper#event:viewToModelPosition
  431. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  432. * @param {Object} data An object containing additional data and placeholder for mapping result.
  433. */
  434. export function viewToModelPosition( evt, data ) {
  435. const viewPos = data.viewPosition;
  436. const viewParent = viewPos.parent;
  437. const mapper = data.mapper;
  438. if ( viewParent.name == 'ul' || viewParent.name == 'ol' ) {
  439. // Position is directly in <ul> or <ol>.
  440. if ( !viewPos.isAtEnd ) {
  441. // If position is not at the end, it must be before <li>.
  442. // Get that <li>, map it to `listItem` and set model position before that `listItem`.
  443. const modelNode = mapper.toModelElement( viewPos.nodeAfter );
  444. data.modelPosition = ModelPosition.createBefore( modelNode );
  445. } else {
  446. // Position is at the end of <ul> or <ol>, so there is no <li> after it to be mapped.
  447. // There is <li> before the position, but we cannot just map it to `listItem` and set model position after it,
  448. // because that <li> may contain nested items.
  449. // We will check "model length" of that <li>, in other words - how many `listItem`s are in that <li>.
  450. const modelNode = mapper.toModelElement( viewPos.nodeBefore );
  451. const modelLength = mapper.getModelLength( viewPos.nodeBefore );
  452. // Then we get model position before mapped `listItem` and shift it accordingly.
  453. data.modelPosition = ModelPosition.createBefore( modelNode ).getShiftedBy( modelLength );
  454. }
  455. evt.stop();
  456. } else if ( viewParent.name == 'li' && viewPos.nodeBefore && ( viewPos.nodeBefore.name == 'ul' || viewPos.nodeBefore.name == 'ol' ) ) {
  457. // In most cases when view position is in <li> it is in text and this is a correct position.
  458. // However, if position is after <ul> or <ol> we have to fix it -- because in model <ul>/<ol> are not in the `listItem`.
  459. const modelNode = mapper.toModelElement( viewParent );
  460. // Check all <ul>s and <ol>s that are in the <li> but before mapped position.
  461. // Get model length of those elements and then add it to the offset of `listItem` mapped to the original <li>.
  462. let modelLength = 1; // Starts from 1 because the original <li> has to be counted in too.
  463. let viewList = viewPos.nodeBefore;
  464. while ( viewList && ( viewList.is( 'ul' ) || viewList.is( 'ol' ) ) ) {
  465. modelLength += mapper.getModelLength( viewList );
  466. viewList = viewList.previousSibling;
  467. }
  468. data.modelPosition = ModelPosition.createBefore( modelNode ).getShiftedBy( modelLength );
  469. evt.stop();
  470. }
  471. }
  472. /**
  473. * Post-fixer that reacts to changes on document and fixes incorrect model states.
  474. *
  475. * In an example below, there is a correct list structure.
  476. * Then the middle element will be removed so the list structure will become incorrect:
  477. *
  478. * <listItem listType="bulleted" listIndent=0>Item 1</listItem>
  479. * <listItem listType="bulleted" listIndent=1>Item 2</listItem> <--- this is removed.
  480. * <listItem listType="bulleted" listIndent=2>Item 3</listItem>
  481. *
  482. * List structure after the middle element removed:
  483. *
  484. * <listItem listType="bulleted" listIndent=0>Item 1</listItem>
  485. * <listItem listType="bulleted" listIndent=2>Item 3</listItem>
  486. *
  487. * Should become:
  488. *
  489. * <listItem listType="bulleted" listIndent=0>Item 1</listItem>
  490. * <listItem listType="bulleted" listIndent=1>Item 3</listItem> <--- note that indent got post-fixed.
  491. *
  492. * @param {module:engine/model/model~Model} model The data model.
  493. * @param {module:engine/model/writer~Writer} writer The writer to do changes with.
  494. * @returns {Boolean} `true` if any change has been applied, `false` otherwise.
  495. */
  496. export function modelChangePostFixer( model, writer ) {
  497. const changes = model.document.differ.getChanges();
  498. const itemToListHead = new Map();
  499. let applied = false;
  500. for ( const entry of changes ) {
  501. if ( entry.type == 'insert' && entry.name == 'listItem' ) {
  502. _addListToFix( entry.position );
  503. } else if ( entry.type == 'insert' && entry.name != 'listItem' ) {
  504. if ( entry.name != '$text' ) {
  505. // In case of renamed element.
  506. const item = entry.position.nodeAfter;
  507. if ( item.hasAttribute( 'listIndent' ) ) {
  508. writer.removeAttribute( 'listIndent', item );
  509. applied = true;
  510. }
  511. if ( item.hasAttribute( 'listType' ) ) {
  512. writer.removeAttribute( 'listType', item );
  513. applied = true;
  514. }
  515. }
  516. const posAfter = entry.position.getShiftedBy( entry.length );
  517. _addListToFix( posAfter );
  518. } else if ( entry.type == 'remove' && entry.name == 'listItem' ) {
  519. _addListToFix( entry.position );
  520. } else if ( entry.type == 'attribute' && entry.attributeKey == 'listIndent' ) {
  521. _addListToFix( entry.range.start );
  522. } else if ( entry.type == 'attribute' && entry.attributeKey == 'listType' ) {
  523. _addListToFix( entry.range.start );
  524. }
  525. }
  526. for ( const listHead of itemToListHead.values() ) {
  527. _fixListIndents( listHead );
  528. _fixListTypes( listHead );
  529. }
  530. return applied;
  531. function _addListToFix( position ) {
  532. const prev = position.nodeBefore;
  533. if ( !prev || !prev.is( 'listItem' ) ) {
  534. const item = position.nodeAfter;
  535. if ( item && item.is( 'listItem' ) ) {
  536. itemToListHead.set( item, item );
  537. }
  538. } else {
  539. let listHead = prev;
  540. if ( itemToListHead.has( listHead ) ) {
  541. return;
  542. }
  543. while ( listHead.previousSibling && listHead.previousSibling.is( 'listItem' ) ) {
  544. listHead = listHead.previousSibling;
  545. if ( itemToListHead.has( listHead ) ) {
  546. return;
  547. }
  548. }
  549. itemToListHead.set( position.nodeBefore, listHead );
  550. }
  551. }
  552. function _fixListIndents( item ) {
  553. let maxIndent = 0;
  554. let fixBy = null;
  555. while ( item && item.is( 'listItem' ) ) {
  556. const itemIndent = item.getAttribute( 'listIndent' );
  557. if ( itemIndent > maxIndent ) {
  558. let newIndent;
  559. if ( fixBy === null ) {
  560. fixBy = itemIndent - maxIndent;
  561. newIndent = maxIndent;
  562. } else {
  563. if ( fixBy > itemIndent ) {
  564. fixBy = itemIndent;
  565. }
  566. newIndent = itemIndent - fixBy;
  567. }
  568. writer.setAttribute( 'listIndent', newIndent, item );
  569. applied = true;
  570. } else {
  571. fixBy = null;
  572. maxIndent = item.getAttribute( 'listIndent' ) + 1;
  573. }
  574. item = item.nextSibling;
  575. }
  576. }
  577. function _fixListTypes( item ) {
  578. let typesStack = [];
  579. let prev = null;
  580. while ( item && item.is( 'listItem' ) ) {
  581. const itemIndent = item.getAttribute( 'listIndent' );
  582. if ( prev && prev.getAttribute( 'listIndent' ) > itemIndent ) {
  583. typesStack = typesStack.slice( 0, itemIndent + 1 );
  584. }
  585. if ( itemIndent != 0 ) {
  586. if ( typesStack[ itemIndent ] ) {
  587. const type = typesStack[ itemIndent ];
  588. if ( item.getAttribute( 'listType' ) != type ) {
  589. writer.setAttribute( 'listType', type, item );
  590. applied = true;
  591. }
  592. } else {
  593. typesStack[ itemIndent ] = item.getAttribute( 'listType' );
  594. }
  595. }
  596. prev = item;
  597. item = item.nextSibling;
  598. }
  599. }
  600. }
  601. /**
  602. * A fixer for pasted content that includes list items.
  603. *
  604. * It fixes indentation of pasted list items so the pasted items match correctly to the context they are pasted into.
  605. *
  606. * Example:
  607. *
  608. * <listItem listType="bulleted" listIndent=0>A</listItem>
  609. * <listItem listType="bulleted" listIndent=1>B^</listItem>
  610. * // At ^ paste: <listItem listType="bulleted" listIndent=4>X</listItem>
  611. * // <listItem listType="bulleted" listIndent=5>Y</listItem>
  612. * <listItem listType="bulleted" listIndent=2>C</listItem>
  613. *
  614. * Should become:
  615. *
  616. * <listItem listType="bulleted" listIndent=0>A</listItem>
  617. * <listItem listType="bulleted" listIndent=1>BX</listItem>
  618. * <listItem listType="bulleted" listIndent=2>Y/listItem>
  619. * <listItem listType="bulleted" listIndent=2>C</listItem>
  620. *
  621. * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
  622. * @param {Array} args Arguments of {@link module:engine/model/model~Model#insertContent}.
  623. */
  624. export function modelIndentPasteFixer( evt, [ content, selectable ] ) {
  625. // Check whether inserted content starts from a `listItem`. If it does not, it means that there are some other
  626. // elements before it and there is no need to fix indents, because even if we insert that content into a list,
  627. // that list will be broken.
  628. // Note: we also need to handle singular elements because inserting item with indent 0 into 0,1,[],2
  629. // would create incorrect model.
  630. let item = content.is( 'documentFragment' ) ? content.getChild( 0 ) : content;
  631. let selection;
  632. if ( !selectable ) {
  633. selection = this.document.selection;
  634. } else {
  635. selection = new ModelSelection( selectable );
  636. }
  637. if ( item && item.is( 'listItem' ) ) {
  638. // Get a reference list item. Inserted list items will be fixed according to that item.
  639. const pos = selection.getFirstPosition();
  640. let refItem = null;
  641. if ( pos.parent.is( 'listItem' ) ) {
  642. refItem = pos.parent;
  643. } else if ( pos.nodeBefore && pos.nodeBefore.is( 'listItem' ) ) {
  644. refItem = pos.nodeBefore;
  645. }
  646. // If there is `refItem` it means that we do insert list items into an existing list.
  647. if ( refItem ) {
  648. // First list item in `data` has indent equal to 0 (it is a first list item). It should have indent equal
  649. // to the indent of reference item. We have to fix the first item and all of it's children and following siblings.
  650. // Indent of all those items has to be adjusted to reference item.
  651. const indentChange = refItem.getAttribute( 'listIndent' );
  652. // Fix only if there is anything to fix.
  653. if ( indentChange > 0 ) {
  654. // Adjust indent of all "first" list items in inserted data.
  655. while ( item && item.is( 'listItem' ) ) {
  656. item._setAttribute( 'listIndent', item.getAttribute( 'listIndent' ) + indentChange );
  657. item = item.nextSibling;
  658. }
  659. }
  660. }
  661. }
  662. }
  663. // Helper function that creates a `<ul><li></li></ul>` or (`<ol>`) structure out of given `modelItem` model `listItem` element.
  664. // Then, it binds created view list item (<li>) with model `listItem` element.
  665. // The function then returns created view list item (<li>).
  666. function generateLiInUl( modelItem, conversionApi ) {
  667. const mapper = conversionApi.mapper;
  668. const viewWriter = conversionApi.writer;
  669. const listType = modelItem.getAttribute( 'listType' ) == 'numbered' ? 'ol' : 'ul';
  670. const viewItem = createViewListItemElement( viewWriter );
  671. const viewList = viewWriter.createContainerElement( listType, null );
  672. viewWriter.insert( ViewPosition.createAt( viewList ), viewItem );
  673. mapper.bindElements( modelItem, viewItem );
  674. return viewItem;
  675. }
  676. // Helper function that converts children of a given `<li>` view element into corresponding model elements.
  677. // The function maintains proper order of elements if model `listItem` is split during the conversion
  678. // due to block children conversion.
  679. //
  680. // @param {module:engine/model/element~Element} listItemModel List item model element to which converted children will be inserted.
  681. // @param {Iterable.<module:engine/view/node~Node>} viewChildren View elements which will be converted.
  682. // @param {Object} conversionApi Conversion interface to be used by the callback.
  683. // @returns {module:engine/model/position~Position} Position on which next elements should be inserted after children conversion.
  684. function viewToModelListItemChildrenConverter( listItemModel, viewChildren, conversionApi ) {
  685. let lastListItem = listItemModel;
  686. let nextPosition = ModelPosition.createAfter( lastListItem );
  687. // Check all children of the converted `<li>`. At this point we assume there are no "whitespace" view text nodes
  688. // in view list, between view list items. This should be handled by `<ul>` and `<ol>` converters.
  689. for ( const child of viewChildren ) {
  690. // If this is a view list element, we will convert it after last `listItem` model element.
  691. if ( child.name == 'ul' || child.name == 'ol' ) {
  692. nextPosition = conversionApi.convertItem( child, nextPosition ).modelCursor;
  693. }
  694. // If it was not a list view element it was a "regular" list item content. Convert it to a `listItem`.
  695. else {
  696. const result = conversionApi.convertItem( child, ModelPosition.createAt( lastListItem, 'end' ) );
  697. const convertedChild = Array.from( result.modelRange )[ 0 ];
  698. nextPosition = ModelPosition.createAt( lastListItem, 'end' );
  699. // If there is a block element child being converted it will split the current list item, for example:
  700. //
  701. // <li><p>Foo</p></li>
  702. //
  703. // will be converted to:
  704. //
  705. // <listItem></listItem><paragraph>Foo</paragraph><listItem></listItem>
  706. //
  707. // so we need to update reference to `lastListItem` and `nextPosition`.
  708. if ( convertedChild.type === 'elementStart' && convertedChild.item.is( 'element' ) ) {
  709. nextPosition = result.modelCursor;
  710. lastListItem = nextPosition.getAncestors().pop();
  711. // Depending on the used converter for block elements, usually the position (`result.modelCursor`
  712. // marked as # below) points to the second list item after conversion:
  713. //
  714. // `<li><p>Foo</p></li>` -> `<listItem></listItem><paragraph>Foo</paragraph><listItem>#</listItem>`
  715. //
  716. // However, in some cases like autoparagraphing the position is placed on the end of the block element:
  717. //
  718. // `<li><h2>Foo</h2></li>` -> `<listItem></listItem><paragraph>Foo#</paragraph><listItem></listItem>`
  719. //
  720. // We need to check for such cases and use proper list item and position based on it.
  721. if ( !lastListItem.is( 'listItem' ) && lastListItem.nextSibling && lastListItem.nextSibling.is( 'listItem' ) ) {
  722. lastListItem = lastListItem.nextSibling;
  723. nextPosition = ModelPosition.createAt( lastListItem, 'end' );
  724. }
  725. }
  726. }
  727. }
  728. return nextPosition;
  729. }
  730. // Helper function that seeks for a list item sibling of given model item (or position) which meets given criteria.
  731. // `options` object may contain one or more of given values (by default they are `false`):
  732. // `options.sameIndent` - whether sought sibling should have same indent (default = no),
  733. // `options.smallerIndent` - whether sought sibling should have smaller indent (default = no).
  734. // `options.indent` - used as reference item when first parameter is a position
  735. // Either `options.sameIndent` or `options.biggerIndent` should be set to `true`.
  736. function getSiblingListItem( modelItemOrPosition, options ) {
  737. const sameIndent = !!options.sameIndent;
  738. const smallerIndent = !!options.smallerIndent;
  739. const indent = modelItemOrPosition instanceof ModelElement ? modelItemOrPosition.getAttribute( 'listIndent' ) : options.listIndent;
  740. let item = modelItemOrPosition instanceof ModelElement ? modelItemOrPosition.previousSibling : modelItemOrPosition.nodeBefore;
  741. while ( item && item.name == 'listItem' ) {
  742. const itemIndent = item.getAttribute( 'listIndent' );
  743. if ( ( sameIndent && indent == itemIndent ) || ( smallerIndent && indent > itemIndent ) ) {
  744. return item;
  745. }
  746. item = item.previousSibling;
  747. }
  748. return null;
  749. }
  750. // Helper function that takes two parameters, that are expected to be view list elements, and merges them.
  751. // The merge happen only if both parameters are UL or OL elements.
  752. function mergeViewLists( viewWriter, firstList, secondList ) {
  753. if ( firstList && secondList && ( firstList.name == 'ul' || firstList.name == 'ol' ) && firstList.name == secondList.name ) {
  754. return viewWriter.mergeContainers( ViewPosition.createAfter( firstList ) );
  755. }
  756. return null;
  757. }
  758. // Helper function that takes model list item element `modelItem`, corresponding view list item element `injectedItem`
  759. // that is not added to the view and is inside a view list element (`ul` or `ol`) and is that's list only child.
  760. // The list is inserted at correct position (element breaking may be needed) and then merged with it's siblings.
  761. // See comments below to better understand the algorithm.
  762. function injectViewList( modelItem, injectedItem, conversionApi ) {
  763. const injectedList = injectedItem.parent;
  764. const mapper = conversionApi.mapper;
  765. const viewWriter = conversionApi.writer;
  766. // Position where view list will be inserted.
  767. let insertPosition = mapper.toViewPosition( ModelPosition.createBefore( modelItem ) );
  768. // 1. Find previous list item that has same or smaller indent. Basically we are looking for a first model item
  769. // that is "parent" or "sibling" of injected model item.
  770. // If there is no such list item, it means that injected list item is the first item in "its list".
  771. const refItem = getSiblingListItem( modelItem, { sameIndent: true, smallerIndent: true } );
  772. const prevItem = modelItem.previousSibling;
  773. if ( refItem && refItem.getAttribute( 'listIndent' ) == modelItem.getAttribute( 'listIndent' ) ) {
  774. // There is a list item with same indent - we found same-level sibling.
  775. // Break the list after it. Inserted view item will be inserted in the broken space.
  776. const viewItem = mapper.toViewElement( refItem );
  777. insertPosition = viewWriter.breakContainer( ViewPosition.createAfter( viewItem ) );
  778. } else {
  779. // There is no list item with same indent. Check previous model item.
  780. if ( prevItem && prevItem.name == 'listItem' ) {
  781. // If it is a list item, it has to have lower indent.
  782. // It means that inserted item should be added to it as its nested item.
  783. insertPosition = mapper.toViewPosition( ModelPosition.createAt( prevItem, 'end' ) );
  784. } else {
  785. // Previous item is not a list item (or does not exist at all).
  786. // Just map the position and insert the view item at mapped position.
  787. insertPosition = mapper.toViewPosition( ModelPosition.createBefore( modelItem ) );
  788. }
  789. }
  790. insertPosition = positionAfterUiElements( insertPosition );
  791. // Insert the view item.
  792. viewWriter.insert( insertPosition, injectedList );
  793. // 2. Handle possible children of injected model item.
  794. if ( prevItem && prevItem.name == 'listItem' ) {
  795. const prevView = mapper.toViewElement( prevItem );
  796. const walker = new ViewTreeWalker( {
  797. boundaries: new ViewRange(
  798. ViewPosition.createAt( prevView, 0 ),
  799. insertPosition
  800. ),
  801. ignoreElementEnd: true
  802. } );
  803. for ( const value of walker ) {
  804. if ( value.item.is( 'li' ) ) {
  805. const breakPosition = viewWriter.breakContainer( ViewPosition.createBefore( value.item ) );
  806. const viewList = value.item.parent;
  807. const targetPosition = ViewPosition.createAt( injectedItem, 'end' );
  808. mergeViewLists( viewWriter, targetPosition.nodeBefore, targetPosition.nodeAfter );
  809. viewWriter.move( ViewRange.createOn( viewList ), targetPosition );
  810. walker.position = breakPosition;
  811. }
  812. }
  813. } else {
  814. const nextViewList = injectedList.nextSibling;
  815. if ( nextViewList && ( nextViewList.is( 'ul' ) || nextViewList.is( 'ol' ) ) ) {
  816. let lastSubChild = null;
  817. for ( const child of nextViewList.getChildren() ) {
  818. const modelChild = mapper.toModelElement( child );
  819. if ( modelChild && modelChild.getAttribute( 'listIndent' ) > modelItem.getAttribute( 'listIndent' ) ) {
  820. lastSubChild = child;
  821. } else {
  822. break;
  823. }
  824. }
  825. if ( lastSubChild ) {
  826. viewWriter.breakContainer( ViewPosition.createAfter( lastSubChild ) );
  827. viewWriter.move( ViewRange.createOn( lastSubChild.parent ), ViewPosition.createAt( injectedItem, 'end' ) );
  828. }
  829. }
  830. }
  831. // Merge inserted view list with its possible neighbour lists.
  832. mergeViewLists( viewWriter, injectedList, injectedList.nextSibling );
  833. mergeViewLists( viewWriter, injectedList.previousSibling, injectedList );
  834. }
  835. // Helper function that takes all children of given `viewRemovedItem` and moves them in a correct place, according
  836. // to other given parameters.
  837. function hoistNestedLists( nextIndent, modelRemoveStartPosition, viewRemoveStartPosition, viewRemovedItem, conversionApi ) {
  838. // Find correct previous model list item element.
  839. // The element has to have either same or smaller indent than given reference indent.
  840. // This will be the model element which will get nested items (if it has smaller indent) or sibling items (if it has same indent).
  841. // Keep in mind that such element might not be found, if removed item was the first item.
  842. const prevModelItem = getSiblingListItem( modelRemoveStartPosition, {
  843. sameIndent: true,
  844. smallerIndent: true,
  845. listIndent: nextIndent
  846. } );
  847. const mapper = conversionApi.mapper;
  848. const viewWriter = conversionApi.writer;
  849. // Indent of found element or `null` if the element has not been found.
  850. const prevIndent = prevModelItem ? prevModelItem.getAttribute( 'listIndent' ) : null;
  851. let insertPosition;
  852. if ( !prevModelItem ) {
  853. // If element has not been found, simply insert lists at the position where the removed item was:
  854. //
  855. // Lorem ipsum.
  856. // 1 -------- <--- this is removed, no previous list item, put nested items in place of removed item.
  857. // 1.1 -------- <--- this is reference indent.
  858. // 1.1.1 --------
  859. // 1.1.2 --------
  860. // 1.2 --------
  861. //
  862. // Becomes:
  863. //
  864. // Lorem ipsum.
  865. // 1.1 --------
  866. // 1.1.1 --------
  867. // 1.1.2 --------
  868. // 1.2 --------
  869. insertPosition = viewRemoveStartPosition;
  870. } else if ( prevIndent == nextIndent ) {
  871. // If element has been found and has same indent as reference indent it means that nested items should
  872. // become siblings of found element:
  873. //
  874. // 1 --------
  875. // 1.1 --------
  876. // 1.2 -------- <--- this is `prevModelItem`.
  877. // 2 -------- <--- this is removed, previous list item has indent same as reference indent.
  878. // 2.1 -------- <--- this is reference indent, this and 2.2 should become siblings of 1.2.
  879. // 2.2 --------
  880. //
  881. // Becomes:
  882. //
  883. // 1 --------
  884. // 1.1 --------
  885. // 1.2 --------
  886. // 2.1 --------
  887. // 2.2 --------
  888. const prevViewList = mapper.toViewElement( prevModelItem ).parent;
  889. insertPosition = ViewPosition.createAfter( prevViewList );
  890. } else {
  891. // If element has been found and has smaller indent as reference indent it means that nested items
  892. // should become nested items of found item:
  893. //
  894. // 1 -------- <--- this is `prevModelItem`.
  895. // 1.1 -------- <--- this is removed, previous list item has indent smaller than reference indent.
  896. // 1.1.1 -------- <--- this is reference indent, this and 1.1.1 should become nested items of 1.
  897. // 1.1.2 --------
  898. // 1.2 --------
  899. //
  900. // Becomes:
  901. //
  902. // 1 --------
  903. // 1.1.1 --------
  904. // 1.1.2 --------
  905. // 1.2 --------
  906. //
  907. // Note: in this case 1.1.1 have indent 2 while 1 have indent 0. In model that should not be possible,
  908. // because following item may have indent bigger only by one. But this is fixed by postfixer.
  909. const modelPosition = ModelPosition.createAt( prevModelItem, 'end' );
  910. insertPosition = mapper.toViewPosition( modelPosition );
  911. }
  912. insertPosition = positionAfterUiElements( insertPosition );
  913. // Handle multiple lists. This happens if list item has nested numbered and bulleted lists. Following lists
  914. // are inserted after the first list (no need to recalculate insertion position for them).
  915. for ( const child of [ ...viewRemovedItem.getChildren() ] ) {
  916. if ( child.is( 'ul' ) || child.is( 'ol' ) ) {
  917. insertPosition = viewWriter.move( ViewRange.createOn( child ), insertPosition ).end;
  918. mergeViewLists( viewWriter, child, child.nextSibling );
  919. mergeViewLists( viewWriter, child.previousSibling, child );
  920. }
  921. }
  922. }
  923. // Helper function that for given `view.Position`, returns a `view.Position` that is after all `view.UIElement`s that
  924. // are after given position.
  925. // For example:
  926. // <container:p>foo^<ui:span></ui:span><ui:span></ui:span>bar</contain:p>
  927. // For position ^, a position before "bar" will be returned.
  928. function positionAfterUiElements( viewPosition ) {
  929. return viewPosition.getLastMatchingPosition( value => value.item.is( 'uiElement' ) );
  930. }