selection.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/selection
  7. */
  8. import Position from './position';
  9. import Element from './element';
  10. import Range from './range';
  11. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  12. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  13. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  14. import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
  15. import mapsEqual from '@ckeditor/ckeditor5-utils/src/mapsequal';
  16. /**
  17. * `Selection` is a group of {@link module:engine/model/range~Range ranges} which has a direction specified by
  18. * {@link module:engine/model/selection~Selection#anchor anchor} and {@link module:engine/model/selection~Selection#focus focus}.
  19. * Additionally, `Selection` may have it's own attributes.
  20. */
  21. export default class Selection {
  22. /**
  23. * Creates an empty selection.
  24. */
  25. constructor() {
  26. /**
  27. * Specifies whether the last added range was added as a backward or forward range.
  28. *
  29. * @private
  30. * @member {Boolean}
  31. */
  32. this._lastRangeBackward = false;
  33. /**
  34. * Stores selection ranges.
  35. *
  36. * @protected
  37. * @member {Array.<module:engine/model/range~Range>}
  38. */
  39. this._ranges = [];
  40. /**
  41. * List of attributes set on current selection.
  42. *
  43. * @protected
  44. * @member {Map} module:engine/model/selection~Selection#_attrs
  45. */
  46. this._attrs = new Map();
  47. }
  48. /**
  49. * Selection anchor. Anchor may be described as a position where the most recent part of the selection starts.
  50. * Together with {@link #focus} they define the direction of selection, which is important
  51. * when expanding/shrinking selection. Anchor is always {@link module:engine/model/range~Range#start start} or
  52. * {@link module:engine/model/range~Range#end end} position of the most recently added range.
  53. *
  54. * Is set to `null` if there are no ranges in selection.
  55. *
  56. * @see #focus
  57. * @readonly
  58. * @type {module:engine/model/position~Position|null}
  59. */
  60. get anchor() {
  61. if ( this._ranges.length > 0 ) {
  62. const range = this._ranges[ this._ranges.length - 1 ];
  63. return this._lastRangeBackward ? range.end : range.start;
  64. }
  65. return null;
  66. }
  67. /**
  68. * Selection focus. Focus is a position where the selection ends.
  69. *
  70. * Is set to `null` if there are no ranges in selection.
  71. *
  72. * @see #anchor
  73. * @readonly
  74. * @type {module:engine/model/position~Position|null}
  75. */
  76. get focus() {
  77. if ( this._ranges.length > 0 ) {
  78. const range = this._ranges[ this._ranges.length - 1 ];
  79. return this._lastRangeBackward ? range.start : range.end;
  80. }
  81. return null;
  82. }
  83. /**
  84. * Returns whether the selection is collapsed. Selection is collapsed when there is exactly one range which is
  85. * collapsed.
  86. *
  87. * @readonly
  88. * @type {Boolean}
  89. */
  90. get isCollapsed() {
  91. const length = this._ranges.length;
  92. if ( length === 1 ) {
  93. return this._ranges[ 0 ].isCollapsed;
  94. } else {
  95. return false;
  96. }
  97. }
  98. /**
  99. * Returns number of ranges in selection.
  100. *
  101. * @type {Number}
  102. */
  103. get rangeCount() {
  104. return this._ranges.length;
  105. }
  106. /**
  107. * Specifies whether the {@link #focus}
  108. * precedes {@link #anchor}.
  109. *
  110. * @type {Boolean}
  111. */
  112. get isBackward() {
  113. return !this.isCollapsed && this._lastRangeBackward;
  114. }
  115. /**
  116. * Checks whether this selection is equal to given selection. Selections are equal if they have same directions,
  117. * same number of ranges and all ranges from one selection equal to a range from other selection.
  118. *
  119. * @param {module:engine/model/selection~Selection} otherSelection Selection to compare with.
  120. * @returns {Boolean} `true` if selections are equal, `false` otherwise.
  121. */
  122. isEqual( otherSelection ) {
  123. if ( this.rangeCount != otherSelection.rangeCount ) {
  124. return false;
  125. } else if ( this.rangeCount === 0 ) {
  126. return true;
  127. }
  128. if ( !this.anchor.isEqual( otherSelection.anchor ) || !this.focus.isEqual( otherSelection.focus ) ) {
  129. return false;
  130. }
  131. for ( let thisRange of this._ranges ) {
  132. let found = false;
  133. for ( let otherRange of otherSelection._ranges ) {
  134. if ( thisRange.isEqual( otherRange ) ) {
  135. found = true;
  136. break;
  137. }
  138. }
  139. if ( !found ) {
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. /**
  146. * Returns an iterator that iterates over copies of selection ranges.
  147. *
  148. * @returns {Iterator.<module:engine/model/range~Range>}
  149. */
  150. *getRanges() {
  151. for ( let range of this._ranges ) {
  152. yield Range.createFromRange( range );
  153. }
  154. }
  155. /**
  156. * Returns a copy of the first range in the selection.
  157. * First range is the one which {@link module:engine/model/range~Range#start start} position
  158. * {@link module:engine/model/position~Position#isBefore is before} start position of all other ranges
  159. * (not to confuse with the first range added to the selection).
  160. *
  161. * Returns `null` if there are no ranges in selection.
  162. *
  163. * @returns {module:engine/model/range~Range|null}
  164. */
  165. getFirstRange() {
  166. let first = null;
  167. for ( let range of this._ranges ) {
  168. if ( !first || range.start.isBefore( first.start ) ) {
  169. first = range;
  170. }
  171. }
  172. return first ? Range.createFromRange( first ) : null;
  173. }
  174. /**
  175. * Returns a copy of the last range in the selection.
  176. * Last range is the one which {@link module:engine/model/range~Range#end end} position
  177. * {@link module:engine/model/position~Position#isAfter is after} end position of all other ranges (not to confuse with the range most
  178. * recently added to the selection).
  179. *
  180. * Returns `null` if there are no ranges in selection.
  181. *
  182. * @returns {module:engine/model/range~Range|null}
  183. */
  184. getLastRange() {
  185. let last = null;
  186. for ( let range of this._ranges ) {
  187. if ( !last || range.end.isAfter( last.end ) ) {
  188. last = range;
  189. }
  190. }
  191. return last ? Range.createFromRange( last ) : null;
  192. }
  193. /**
  194. * Returns the first position in the selection.
  195. * First position is the position that {@link module:engine/model/position~Position#isBefore is before}
  196. * any other position in the selection.
  197. *
  198. * Returns `null` if there are no ranges in selection.
  199. *
  200. * @returns {module:engine/model/position~Position|null}
  201. */
  202. getFirstPosition() {
  203. const first = this.getFirstRange();
  204. return first ? Position.createFromPosition( first.start ) : null;
  205. }
  206. /**
  207. * Returns the last position in the selection.
  208. * Last position is the position that {@link module:engine/model/position~Position#isAfter is after}
  209. * any other position in the selection.
  210. *
  211. * Returns `null` if there are no ranges in selection.
  212. *
  213. * @returns {module:engine/model/position~Position|null}
  214. */
  215. getLastPosition() {
  216. const lastRange = this.getLastRange();
  217. return lastRange ? Position.createFromPosition( lastRange.end ) : null;
  218. }
  219. /**
  220. * Adds a range to this selection. Added range is copied. This means that passed range is not saved in `Selection`
  221. * instance and operating on it will not change `Selection` state.
  222. *
  223. * Accepts a flag describing in which way the selection is made - passed range might be selected from
  224. * {@link module:engine/model/range~Range#start start} to {@link module:engine/model/range~Range#end end}
  225. * or from {@link module:engine/model/range~Range#end end}
  226. * to {@link module:engine/model/range~Range#start start}.
  227. * The flag is used to set {@link #anchor} and
  228. * {@link #focus} properties.
  229. *
  230. * @fires change:range
  231. * @param {module:engine/model/range~Range} range Range to add.
  232. * @param {Boolean} [isBackward=false] Flag describing if added range was selected forward - from start to end (`false`)
  233. * or backward - from end to start (`true`).
  234. */
  235. addRange( range, isBackward = false ) {
  236. this._pushRange( range );
  237. this._lastRangeBackward = !!isBackward;
  238. this.fire( 'change:range', { directChange: true } );
  239. }
  240. /**
  241. * Removes all ranges that were added to the selection.
  242. *
  243. * @fires change:range
  244. */
  245. removeAllRanges() {
  246. if ( this._ranges.length > 0 ) {
  247. this._removeAllRanges();
  248. this.fire( 'change:range', { directChange: true } );
  249. }
  250. }
  251. /**
  252. * Replaces all ranges that were added to the selection with given array of ranges. Last range of the array
  253. * is treated like the last added range and is used to set {@link module:engine/model/selection~Selection#anchor} and
  254. * {@link module:engine/model/selection~Selection#focus}. Accepts a flag describing in which direction the selection is made
  255. * (see {@link module:engine/model/selection~Selection#addRange}).
  256. *
  257. * @fires change:range
  258. * @param {Iterable.<module:engine/model/range~Range>} newRanges Ranges to set.
  259. * @param {Boolean} [isLastBackward=false] Flag describing if last added range was selected forward - from start to end (`false`)
  260. * or backward - from end to start (`true`).
  261. */
  262. setRanges( newRanges, isLastBackward = false ) {
  263. newRanges = Array.from( newRanges );
  264. // Check whether there is any range in new ranges set that is different than all already added ranges.
  265. const anyNewRange = newRanges.some( ( newRange ) => {
  266. if ( !( newRange instanceof Range ) ) {
  267. throw new CKEditorError( 'model-selection-added-not-range: Trying to add an object that is not an instance of Range.' );
  268. }
  269. return this._ranges.every( ( oldRange ) => {
  270. return !oldRange.isEqual( newRange );
  271. } );
  272. } );
  273. // Don't do anything if nothing changed.
  274. if ( newRanges.length === this._ranges.length && !anyNewRange ) {
  275. return;
  276. }
  277. this._removeAllRanges();
  278. for ( let range of newRanges ) {
  279. this._pushRange( range );
  280. }
  281. this._lastRangeBackward = !!isLastBackward;
  282. this.fire( 'change:range', { directChange: true } );
  283. }
  284. /**
  285. * Sets this selection's ranges and direction to the ranges and direction of the given selection.
  286. *
  287. * @param {module:engine/model/selection~Selection} otherSelection
  288. */
  289. setTo( otherSelection ) {
  290. this.setRanges( otherSelection.getRanges(), otherSelection.isBackward );
  291. }
  292. /**
  293. * Sets collapsed selection in the specified location.
  294. *
  295. * The location can be specified in the same form as {@link module:engine/model/position~Position.createAt} parameters.
  296. *
  297. * @fires change:range
  298. * @param {module:engine/model/item~Item|module:engine/model/position~Position} itemOrPosition
  299. * @param {Number|'end'|'before'|'after'} [offset=0] Offset or one of the flags. Used only when
  300. * first parameter is a {@link module:engine/model/item~Item model item}.
  301. */
  302. collapse( itemOrPosition, offset ) {
  303. const pos = Position.createAt( itemOrPosition, offset );
  304. const range = new Range( pos, pos );
  305. this.setRanges( [ range ] );
  306. }
  307. /**
  308. * Collapses selection to the selection's {@link module:engine/model/selection~Selection#getFirstPosition first position}.
  309. * All ranges, besides the collapsed one, will be removed. Nothing will change if there are no ranges stored
  310. * inside selection.
  311. *
  312. * @fires change
  313. */
  314. collapseToStart() {
  315. const startPosition = this.getFirstPosition();
  316. if ( startPosition !== null ) {
  317. this.setRanges( [ new Range( startPosition, startPosition ) ] );
  318. }
  319. }
  320. /**
  321. * Collapses selection to the selection's {@link module:engine/model/selection~Selection#getLastPosition last position}.
  322. * All ranges, besides the collapsed one, will be removed. Nothing will change if there are no ranges stored
  323. * inside selection.
  324. *
  325. * @fires change
  326. */
  327. collapseToEnd() {
  328. const endPosition = this.getLastPosition();
  329. if ( endPosition !== null ) {
  330. this.setRanges( [ new Range( endPosition, endPosition ) ] );
  331. }
  332. }
  333. /**
  334. * Sets {@link module:engine/model/selection~Selection#focus} to the specified location.
  335. *
  336. * The location can be specified in the same form as {@link module:engine/model/position~Position.createAt} parameters.
  337. *
  338. * @fires change:range
  339. * @param {module:engine/model/item~Item|module:engine/model/position~Position} itemOrPosition
  340. * @param {Number|'end'|'before'|'after'} [offset=0] Offset or one of the flags. Used only when
  341. * first parameter is a {@link module:engine/model/item~Item model item}.
  342. */
  343. setFocus( itemOrPosition, offset ) {
  344. if ( this.anchor === null ) {
  345. /**
  346. * Cannot set selection focus if there are no ranges in selection.
  347. *
  348. * @error model-selection-setFocus-no-ranges
  349. */
  350. throw new CKEditorError( 'model-selection-setFocus-no-ranges: Cannot set selection focus if there are no ranges in selection.' );
  351. }
  352. const newFocus = Position.createAt( itemOrPosition, offset );
  353. if ( newFocus.compareWith( this.focus ) == 'same' ) {
  354. return;
  355. }
  356. const anchor = this.anchor;
  357. if ( this._ranges.length ) {
  358. this._popRange();
  359. }
  360. if ( newFocus.compareWith( anchor ) == 'before' ) {
  361. this.addRange( new Range( newFocus, anchor ), true );
  362. } else {
  363. this.addRange( new Range( anchor, newFocus ) );
  364. }
  365. }
  366. /**
  367. * Gets an attribute value for given key or `undefined` if that attribute is not set on the selection.
  368. *
  369. * @param {String} key Key of attribute to look for.
  370. * @returns {*} Attribute value or `undefined`.
  371. */
  372. getAttribute( key ) {
  373. return this._attrs.get( key );
  374. }
  375. /**
  376. * Returns iterator that iterates over this selection's attributes.
  377. *
  378. * Attributes are returned as arrays containing two items. First one is attribute key and second is attribute value.
  379. * This format is accepted by native `Map` object and also can be passed in `Node` constructor.
  380. *
  381. * @returns {Iterable.<*>}
  382. */
  383. getAttributes() {
  384. return this._attrs.entries();
  385. }
  386. /**
  387. * Returns iterator that iterates over this selection's attribute keys.
  388. *
  389. * @returns {Iterator.<String>}
  390. */
  391. getAttributeKeys() {
  392. return this._attrs.keys();
  393. }
  394. /**
  395. * Checks if the selection has an attribute for given key.
  396. *
  397. * @param {String} key Key of attribute to check.
  398. * @returns {Boolean} `true` if attribute with given key is set on selection, `false` otherwise.
  399. */
  400. hasAttribute( key ) {
  401. return this._attrs.has( key );
  402. }
  403. /**
  404. * Removes all attributes from the selection.
  405. *
  406. * If there were any attributes in selection, fires the {@link #event:change} event with
  407. * removed attributes' keys.
  408. *
  409. * @fires change:attribute
  410. */
  411. clearAttributes() {
  412. if ( this._attrs.size > 0 ) {
  413. const attributeKeys = Array.from( this._attrs.keys() );
  414. this._attrs.clear();
  415. this.fire( 'change:attribute', { attributeKeys, directChange: true } );
  416. }
  417. }
  418. /**
  419. * Removes an attribute with given key from the selection.
  420. *
  421. * If given attribute was set on the selection, fires the {@link #event:change} event with
  422. * removed attribute key.
  423. *
  424. * @fires change:attribute
  425. * @param {String} key Key of attribute to remove.
  426. */
  427. removeAttribute( key ) {
  428. if ( this.hasAttribute( key ) ) {
  429. this._attrs.delete( key );
  430. this.fire( 'change:attribute', { attributeKeys: [ key ], directChange: true } );
  431. }
  432. }
  433. /**
  434. * Sets attribute on the selection. If attribute with the same key already is set, it's value is overwritten.
  435. *
  436. * If the attribute value has changed, fires the {@link #event:change} event with
  437. * the attribute key.
  438. *
  439. * @fires change:attribute
  440. * @param {String} key Key of attribute to set.
  441. * @param {*} value Attribute value.
  442. */
  443. setAttribute( key, value ) {
  444. if ( this.getAttribute( key ) !== value ) {
  445. this._attrs.set( key, value );
  446. this.fire( 'change:attribute', { attributeKeys: [ key ], directChange: true } );
  447. }
  448. }
  449. /**
  450. * Removes all attributes from the selection and sets given attributes.
  451. *
  452. * If given set of attributes is different than set of attributes already added to selection, fires
  453. * {@link #event:change change event} with keys of attributes that changed.
  454. *
  455. * @fires event:change:attribute
  456. * @param {Iterable|Object} attrs Iterable object containing attributes to be set.
  457. */
  458. setAttributesTo( attrs ) {
  459. attrs = toMap( attrs );
  460. if ( !mapsEqual( attrs, this._attrs ) ) {
  461. // Create a set from keys of old and new attributes.
  462. const changed = new Set( Array.from( attrs.keys() ).concat( Array.from( this._attrs.keys() ) ) );
  463. for ( let [ key, value ] of attrs ) {
  464. // If the attribute remains unchanged, remove it from changed set.
  465. if ( this._attrs.get( key ) === value ) {
  466. changed.delete( key );
  467. }
  468. }
  469. this._attrs = attrs;
  470. this.fire( 'change:attribute', { attributeKeys: Array.from( changed ), directChange: true } );
  471. }
  472. }
  473. /**
  474. * Returns the selected element. {@link module:engine/model/element~Element Element} is considered as selected if there is only
  475. * one range in the selection, and that range contains exactly one element.
  476. * Returns `null` if there is no selected element.
  477. *
  478. * @returns {module:engine/model/element~Element|null}
  479. */
  480. getSelectedElement() {
  481. if ( this.rangeCount !== 1 ) {
  482. return null;
  483. }
  484. const range = this.getFirstRange();
  485. const nodeAfterStart = range.start.nodeAfter;
  486. const nodeBeforeEnd = range.end.nodeBefore;
  487. return ( nodeAfterStart instanceof Element && nodeAfterStart == nodeBeforeEnd ) ? nodeAfterStart : null;
  488. }
  489. /**
  490. * Gets elements of type "block" touched by the selection.
  491. *
  492. * This method's result can be used for example to apply block styling to all blocks covered by this selection.
  493. *
  494. * *Note:* `getSelectedBlocks` always returns the deepest block.
  495. *
  496. * In this case the function will return exactly all 3 paragraphs:
  497. *
  498. * <paragraph>[a</paragraph>
  499. * <quote>
  500. * <paragraph>b</paragraph>
  501. * </quote>
  502. * <paragraph>c]d</paragraph>
  503. *
  504. * In this case the paragraph will also be returned, despite the collapsed selection:
  505. *
  506. * <paragraph>[]a</paragraph>
  507. *
  508. * @returns {Iterator.<module:engine/model/element~Element>}
  509. */
  510. *getSelectedBlocks() {
  511. const visited = new WeakSet();
  512. for ( const range of this.getRanges() ) {
  513. const startBlock = getParentBlock( range.start, visited );
  514. if ( startBlock ) {
  515. yield startBlock;
  516. }
  517. for ( const value of range.getWalker() ) {
  518. if ( value.type == 'elementEnd' && isUnvisitedBlockContainer( value.item, visited ) ) {
  519. yield value.item;
  520. }
  521. }
  522. const endBlock = getParentBlock( range.end, visited );
  523. if ( endBlock ) {
  524. yield endBlock;
  525. }
  526. }
  527. }
  528. /**
  529. * Creates and returns an instance of `Selection` that is a clone of given selection, meaning that it has same
  530. * ranges and same direction as this selection.
  531. *
  532. * @params {module:engine/model/selection~Selection} otherSelection Selection to be cloned.
  533. * @returns {module:engine/model/selection~Selection} `Selection` instance that is a clone of given selection.
  534. */
  535. static createFromSelection( otherSelection ) {
  536. const selection = new this();
  537. selection.setTo( otherSelection );
  538. return selection;
  539. }
  540. /**
  541. * Adds given range to internal {@link #_ranges ranges array}. Throws an error
  542. * if given range is intersecting with any range that is already stored in this selection.
  543. *
  544. * @protected
  545. * @param {module:engine/model/range~Range} range Range to add.
  546. */
  547. _pushRange( range ) {
  548. if ( !( range instanceof Range ) ) {
  549. throw new CKEditorError( 'model-selection-added-not-range: Trying to add an object that is not an instance of Range.' );
  550. }
  551. this._checkRange( range );
  552. this._ranges.push( Range.createFromRange( range ) );
  553. }
  554. /**
  555. * Checks if given range intersects with ranges that are already in the selection. Throws an error if it does.
  556. *
  557. * @protected
  558. * @param {module:engine/model/range~Range} range Range to check.
  559. */
  560. _checkRange( range ) {
  561. for ( let i = 0; i < this._ranges.length; i++ ) {
  562. if ( range.isIntersecting( this._ranges[ i ] ) ) {
  563. /**
  564. * Trying to add a range that intersects with another range from selection.
  565. *
  566. * @error selection-range-intersects
  567. * @param {module:engine/model/range~Range} addedRange Range that was added to the selection.
  568. * @param {module:engine/model/range~Range} intersectingRange Range from selection that intersects with `addedRange`.
  569. */
  570. throw new CKEditorError(
  571. 'model-selection-range-intersects: Trying to add a range that intersects with another range from selection.',
  572. { addedRange: range, intersectingRange: this._ranges[ i ] }
  573. );
  574. }
  575. }
  576. }
  577. /**
  578. * Removes most recently added range from the selection.
  579. *
  580. * @protected
  581. */
  582. _popRange() {
  583. this._ranges.pop();
  584. }
  585. /**
  586. * Deletes ranges from internal range array. Uses {@link #_popRange _popRange} to
  587. * ensure proper ranges removal.
  588. *
  589. * @private
  590. */
  591. _removeAllRanges() {
  592. while ( this._ranges.length > 0 ) {
  593. this._popRange();
  594. }
  595. }
  596. /**
  597. * @event change
  598. */
  599. /**
  600. * Fired whenever selection ranges are changed.
  601. *
  602. * @event change:range
  603. * @param {Boolean} directChange Specifies whether the range change was caused by direct usage of `Selection` API (`true`)
  604. * or by changes done to {@link module:engine/model/document~Document model document}
  605. * using {@link module:engine/model/batch~Batch Batch} API (`false`).
  606. */
  607. /**
  608. * Fired whenever selection attributes are changed.
  609. *
  610. * @event change:attribute
  611. * @param {Boolean} directChange Specifies whether the attributes changed by direct usage of the Selection API (`true`)
  612. * or by changes done to the {@link module:engine/model/document~Document model document}
  613. * using the {@link module:engine/model/batch~Batch Batch} API (`false`).
  614. * @param {Array.<String>} attributeKeys Array containing keys of attributes that changed.
  615. */
  616. }
  617. mix( Selection, EmitterMixin );
  618. // Checks whether the given element extends $block in the schema and has a parent (is not a root).
  619. // Marks it as already visited.
  620. function isUnvisitedBlockContainer( element, visited ) {
  621. if ( visited.has( element ) ) {
  622. return false;
  623. }
  624. visited.add( element );
  625. // TODO https://github.com/ckeditor/ckeditor5-engine/issues/532#issuecomment-278900072.
  626. // This should not be a `$block` check.
  627. return element.document.schema.itemExtends( element.name, '$block' ) && element.parent;
  628. }
  629. // Finds the lowest element in position's ancestors which is a block.
  630. // Marks all ancestors as already visited to not include any of them later on.
  631. function getParentBlock( position, visited ) {
  632. const ancestors = position.parent.getAncestors( { parentFirst: true, includeNode: true } );
  633. const block = ancestors.find( ( element ) => isUnvisitedBlockContainer( element, visited ) );
  634. // Mark all ancestors of this position's parent, because find() might've stopped early and
  635. // the found block may be a child of another block.
  636. ancestors.forEach( element => visited.add( element ) );
  637. return block;
  638. }