selection.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/view/selection
  7. */
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import Range from './range';
  10. import Position from './position';
  11. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  12. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  13. import Node from './node';
  14. import Element from './element';
  15. import count from '@ckeditor/ckeditor5-utils/src/count';
  16. import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
  17. /**
  18. * Class representing selection in tree view.
  19. *
  20. * Selection can consist of {@link module:engine/view/range~Range ranges} that can be set using
  21. * {@link module:engine/view/selection~Selection#_setTo} method.
  22. * That method create copies of provided ranges and store those copies internally. Further modifications to passed
  23. * ranges will not change selection's state.
  24. * Selection's ranges can be obtained via {@link module:engine/view/selection~Selection#getRanges getRanges},
  25. * {@link module:engine/view/selection~Selection#getFirstRange getFirstRange}
  26. * and {@link module:engine/view/selection~Selection#getLastRange getLastRange}
  27. * methods, which return copies of ranges stored inside selection. Modifications made on these copies will not change
  28. * selection's state. Similar situation occurs when getting {@link module:engine/view/selection~Selection#anchor anchor},
  29. * {@link module:engine/view/selection~Selection#focus focus}, {@link module:engine/view/selection~Selection#getFirstPosition first} and
  30. * {@link module:engine/view/selection~Selection#getLastPosition last} positions - all will return copies of requested positions.
  31. */
  32. export default class Selection {
  33. /**
  34. * Creates new selection instance.
  35. *
  36. * // Creates empty selection without ranges.
  37. * const selection = new Selection();
  38. *
  39. * // Creates selection at the given range.
  40. * const range = new Range( start, end );
  41. * const selection = new Selection( range, { backward, fake, label } );
  42. *
  43. * // Creates selection at the given ranges
  44. * const ranges = [ new Range( start1, end2 ), new Range( star2, end2 ) ];
  45. * const selection = new Selection( ranges, { backward, fake, label } );
  46. *
  47. * // Creates selection from the other selection.
  48. * const otherSelection = new Selection();
  49. * const selection = new Selection( otherSelection );
  50. *
  51. * // Creates selection at the given position.
  52. * const position = new Position( root, path );
  53. * const selection = new Selection( position, { fake, label } );
  54. *
  55. * // Sets collapsed selection at the position of given item and offset.
  56. * const paragraph = writer.createElement( 'paragraph' );
  57. * selection.setTo( paragraph, offset, { fake, label } );
  58. *
  59. * // Sets selection inside the item.
  60. * const paragraph = writer.createElement( 'paragraph' );
  61. * selection.setTo( paragraph, 'in', { backward, fake, label } );
  62. *
  63. * // Sets selection on the item.
  64. * const paragraph = writer.createElement( 'paragraph' );
  65. * selection.setTo( paragraph, 'on', { backward, fake, label } );
  66. *
  67. * @param {module:engine/view/selection~Selection|module:engine/view/position~Position|
  68. * Iterable.<module:engine/view/range~Range>|module:engine/view/range~Range|module:engine/view/item~Item|null} [selectable=null]
  69. * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Offset or place when selectable is an `Item`.
  70. * @param {Object} [options]
  71. * @param {Boolean} [options.backward] Sets this selection instance to be backward.
  72. * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
  73. * @param {String} [options.label] Label for the fake selection.
  74. */
  75. constructor( selectable = null, placeOrOffset, options ) {
  76. /**
  77. * Stores all ranges that are selected.
  78. *
  79. * @protected
  80. * @member {Array.<module:engine/view/range~Range>}
  81. */
  82. this._ranges = [];
  83. /**
  84. * Specifies whether the last added range was added as a backward or forward range.
  85. *
  86. * @protected
  87. * @member {Boolean}
  88. */
  89. this._lastRangeBackward = false;
  90. /**
  91. * Specifies whether selection instance is fake.
  92. *
  93. * @private
  94. * @member {Boolean}
  95. */
  96. this._isFake = false;
  97. /**
  98. * Fake selection's label.
  99. *
  100. * @private
  101. * @member {String}
  102. */
  103. this._fakeSelectionLabel = '';
  104. this._setTo( selectable, placeOrOffset, options );
  105. }
  106. /**
  107. * Returns true if selection instance is marked as `fake`.
  108. *
  109. * @see #_setTo
  110. * @returns {Boolean}
  111. */
  112. get isFake() {
  113. return this._isFake;
  114. }
  115. /**
  116. * Returns fake selection label.
  117. *
  118. * @see #_setTo
  119. * @returns {String}
  120. */
  121. get fakeSelectionLabel() {
  122. return this._fakeSelectionLabel;
  123. }
  124. /**
  125. * Selection anchor. Anchor may be described as a position where the selection starts. Together with
  126. * {@link #focus focus} they define the direction of selection, which is important
  127. * when expanding/shrinking selection. Anchor is always the start or end of the most recent added range.
  128. * It may be a bit unintuitive when there are multiple ranges in selection.
  129. *
  130. * @see #focus
  131. * @type {module:engine/view/position~Position}
  132. */
  133. get anchor() {
  134. if ( !this._ranges.length ) {
  135. return null;
  136. }
  137. const range = this._ranges[ this._ranges.length - 1 ];
  138. const anchor = this._lastRangeBackward ? range.end : range.start;
  139. return Position.createFromPosition( anchor );
  140. }
  141. /**
  142. * Selection focus. Focus is a position where the selection ends.
  143. *
  144. * @see #anchor
  145. * @type {module:engine/view/position~Position}
  146. */
  147. get focus() {
  148. if ( !this._ranges.length ) {
  149. return null;
  150. }
  151. const range = this._ranges[ this._ranges.length - 1 ];
  152. const focus = this._lastRangeBackward ? range.start : range.end;
  153. return Position.createFromPosition( focus );
  154. }
  155. /**
  156. * Returns whether the selection is collapsed. Selection is collapsed when there is exactly one range which is
  157. * collapsed.
  158. *
  159. * @type {Boolean}
  160. */
  161. get isCollapsed() {
  162. return this.rangeCount === 1 && this._ranges[ 0 ].isCollapsed;
  163. }
  164. /**
  165. * Returns number of ranges in selection.
  166. *
  167. * @type {Number}
  168. */
  169. get rangeCount() {
  170. return this._ranges.length;
  171. }
  172. /**
  173. * Specifies whether the {@link #focus} precedes {@link #anchor}.
  174. *
  175. * @type {Boolean}
  176. */
  177. get isBackward() {
  178. return !this.isCollapsed && this._lastRangeBackward;
  179. }
  180. /**
  181. * {@link module:engine/view/editableelement~EditableElement EditableElement} instance that contains this selection, or `null`
  182. * if the selection is not inside an editable element.
  183. *
  184. * @type {module:engine/view/editableelement~EditableElement|null}
  185. */
  186. get editableElement() {
  187. if ( this.anchor ) {
  188. return this.anchor.editableElement;
  189. }
  190. return null;
  191. }
  192. /**
  193. * Returns an iterable that contains copies of all ranges added to the selection.
  194. *
  195. * @returns {Iterable.<module:engine/view/range~Range>}
  196. */
  197. * getRanges() {
  198. for ( const range of this._ranges ) {
  199. yield Range.createFromRange( range );
  200. }
  201. }
  202. /**
  203. * Returns copy of the first range in the selection. First range is the one which
  204. * {@link module:engine/view/range~Range#start start} position {@link module:engine/view/position~Position#isBefore is before} start
  205. * position of all other ranges (not to confuse with the first range added to the selection).
  206. * Returns `null` if no ranges are added to selection.
  207. *
  208. * @returns {module:engine/view/range~Range|null}
  209. */
  210. getFirstRange() {
  211. let first = null;
  212. for ( const range of this._ranges ) {
  213. if ( !first || range.start.isBefore( first.start ) ) {
  214. first = range;
  215. }
  216. }
  217. return first ? Range.createFromRange( first ) : null;
  218. }
  219. /**
  220. * Returns copy of the last range in the selection. Last range is the one which {@link module:engine/view/range~Range#end end}
  221. * position {@link module:engine/view/position~Position#isAfter is after} end position of all other ranges (not to confuse
  222. * with the last range added to the selection). Returns `null` if no ranges are added to selection.
  223. *
  224. * @returns {module:engine/view/range~Range|null}
  225. */
  226. getLastRange() {
  227. let last = null;
  228. for ( const range of this._ranges ) {
  229. if ( !last || range.end.isAfter( last.end ) ) {
  230. last = range;
  231. }
  232. }
  233. return last ? Range.createFromRange( last ) : null;
  234. }
  235. /**
  236. * Returns copy of the first position in the selection. First position is the position that
  237. * {@link module:engine/view/position~Position#isBefore is before} any other position in the selection ranges.
  238. * Returns `null` if no ranges are added to selection.
  239. *
  240. * @returns {module:engine/view/position~Position|null}
  241. */
  242. getFirstPosition() {
  243. const firstRange = this.getFirstRange();
  244. return firstRange ? Position.createFromPosition( firstRange.start ) : null;
  245. }
  246. /**
  247. * Returns copy of the last position in the selection. Last position is the position that
  248. * {@link module:engine/view/position~Position#isAfter is after} any other position in the selection ranges.
  249. * Returns `null` if no ranges are added to selection.
  250. *
  251. * @returns {module:engine/view/position~Position|null}
  252. */
  253. getLastPosition() {
  254. const lastRange = this.getLastRange();
  255. return lastRange ? Position.createFromPosition( lastRange.end ) : null;
  256. }
  257. /**
  258. * Checks whether, this selection is equal to given selection. Selections are equal if they have same directions,
  259. * same number of ranges and all ranges from one selection equal to a range from other selection.
  260. *
  261. * @param {module:engine/view/selection~Selection} otherSelection Selection to compare with.
  262. * @returns {Boolean} `true` if selections are equal, `false` otherwise.
  263. */
  264. isEqual( otherSelection ) {
  265. if ( this.isFake != otherSelection.isFake ) {
  266. return false;
  267. }
  268. if ( this.isFake && this.fakeSelectionLabel != otherSelection.fakeSelectionLabel ) {
  269. return false;
  270. }
  271. if ( this.rangeCount != otherSelection.rangeCount ) {
  272. return false;
  273. } else if ( this.rangeCount === 0 ) {
  274. return true;
  275. }
  276. if ( !this.anchor.isEqual( otherSelection.anchor ) || !this.focus.isEqual( otherSelection.focus ) ) {
  277. return false;
  278. }
  279. for ( const thisRange of this._ranges ) {
  280. let found = false;
  281. for ( const otherRange of otherSelection._ranges ) {
  282. if ( thisRange.isEqual( otherRange ) ) {
  283. found = true;
  284. break;
  285. }
  286. }
  287. if ( !found ) {
  288. return false;
  289. }
  290. }
  291. return true;
  292. }
  293. /**
  294. * Checks whether this selection is similar to given selection. Selections are similar if they have same directions, same
  295. * number of ranges, and all {@link module:engine/view/range~Range#getTrimmed trimmed} ranges from one selection are
  296. * equal to any trimmed range from other selection.
  297. *
  298. * @param {module:engine/view/selection~Selection} otherSelection Selection to compare with.
  299. * @returns {Boolean} `true` if selections are similar, `false` otherwise.
  300. */
  301. isSimilar( otherSelection ) {
  302. if ( this.isBackward != otherSelection.isBackward ) {
  303. return false;
  304. }
  305. const numOfRangesA = count( this.getRanges() );
  306. const numOfRangesB = count( otherSelection.getRanges() );
  307. // If selections have different number of ranges, they cannot be similar.
  308. if ( numOfRangesA != numOfRangesB ) {
  309. return false;
  310. }
  311. // If both selections have no ranges, they are similar.
  312. if ( numOfRangesA == 0 ) {
  313. return true;
  314. }
  315. // Check if each range in one selection has a similar range in other selection.
  316. for ( let rangeA of this.getRanges() ) {
  317. rangeA = rangeA.getTrimmed();
  318. let found = false;
  319. for ( let rangeB of otherSelection.getRanges() ) {
  320. rangeB = rangeB.getTrimmed();
  321. if ( rangeA.start.isEqual( rangeB.start ) && rangeA.end.isEqual( rangeB.end ) ) {
  322. found = true;
  323. break;
  324. }
  325. }
  326. // For `rangeA`, neither range in `otherSelection` was similar. So selections are not similar.
  327. if ( !found ) {
  328. return false;
  329. }
  330. }
  331. // There were no ranges that weren't matched. Selections are similar.
  332. return true;
  333. }
  334. /**
  335. * Returns the selected element. {@link module:engine/view/element~Element Element} is considered as selected if there is only
  336. * one range in the selection, and that range contains exactly one element.
  337. * Returns `null` if there is no selected element.
  338. *
  339. * @returns {module:engine/view/element~Element|null}
  340. */
  341. getSelectedElement() {
  342. if ( this.rangeCount !== 1 ) {
  343. return null;
  344. }
  345. const range = this.getFirstRange();
  346. const nodeAfterStart = range.start.nodeAfter;
  347. const nodeBeforeEnd = range.end.nodeBefore;
  348. return ( nodeAfterStart instanceof Element && nodeAfterStart == nodeBeforeEnd ) ? nodeAfterStart : null;
  349. }
  350. /**
  351. * Sets this selection's ranges and direction to the specified location based on the given
  352. * {@link module:engine/view/selection~Selection selection}, {@link module:engine/view/position~Position position},
  353. * {@link module:engine/view/item~Item item}, {@link module:engine/view/range~Range range},
  354. * an iterable of {@link module:engine/view/range~Range ranges} or null.
  355. *
  356. * This method provides option to create a fake selection.
  357. * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
  358. * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
  359. * represented in other way, for example by applying proper CSS class.
  360. *
  361. * // Sets selection to the given range.
  362. * const range = new Range( start, end );
  363. * selection.setTo( range, { backward, fake, label } );
  364. *
  365. * // Sets selection to given ranges.
  366. * const ranges = [ new Range( start1, end2 ), new Range( star2, end2 ) ];
  367. * selection.setTo( range, { backward, fake, label } );
  368. *
  369. * // Sets selection to the other selection.
  370. * const otherSelection = new Selection();
  371. * selection.setTo( otherSelection );
  372. *
  373. * // Sets collapsed selection at the given position.
  374. * const position = new Position( root, path );
  375. * selection.setTo( position, { fake, label } );
  376. *
  377. * // Sets collapsed selection at the position of given item and offset.
  378. * const paragraph = writer.createElement( 'paragraph' );
  379. * selection.setTo( paragraph, offset, { fake, label } );
  380. *
  381. * // Sets selection inside the item.
  382. * const paragraph = writer.createElement( 'paragraph' );
  383. * selection.setTo( paragraph, 'in', { backward, fake, label } );
  384. *
  385. * // Sets selection on the item.
  386. * const paragraph = writer.createElement( 'paragraph' );
  387. * selection.setTo( paragraph, 'on', { backward, fake, label } );
  388. *
  389. * // Clears selection. Removes all ranges.
  390. * selection.setTo( null );
  391. *
  392. * @protected
  393. * @fires change
  394. * @param {module:engine/view/selection~Selection|module:engine/view/position~Position|
  395. * Iterable.<module:engine/view/range~Range>|module:engine/view/range~Range|module:engine/view/item~Item|null} selectable
  396. * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Sets offset or place of the selection.
  397. * @param {Object} [options]
  398. * @param {Boolean} [options.backward] Sets this selection instance to be backward.
  399. * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
  400. * @param {String} [options.label] Label for the fake selection.
  401. */
  402. _setTo( selectable, placeOrOffset, options ) {
  403. if ( selectable === null ) {
  404. this._setRanges( [] );
  405. this._setFakeOptions( placeOrOffset );
  406. } else if ( selectable instanceof Selection ) {
  407. this._setRanges( selectable.getRanges(), selectable.isBackward );
  408. this._setFakeOptions( { fake: selectable.isFake, label: selectable.fakeSelectionLabel } );
  409. } else if ( selectable instanceof Range ) {
  410. this._setRanges( [ selectable ], placeOrOffset && placeOrOffset.backward );
  411. this._setFakeOptions( placeOrOffset );
  412. } else if ( selectable instanceof Position ) {
  413. this._setRanges( [ new Range( selectable ) ] );
  414. this._setFakeOptions( placeOrOffset );
  415. } else if ( selectable instanceof Node ) {
  416. const backward = !!options && !!options.backward;
  417. let range;
  418. if ( placeOrOffset === undefined ) {
  419. /**
  420. * selection.setTo requires the second parameter when the first parameter is a node.
  421. *
  422. * @error view-selection-setTo-required-second-parameter
  423. */
  424. throw new CKEditorError(
  425. 'view-selection-setTo-required-second-parameter: ' +
  426. 'selection.setTo requires the second parameter when the first parameter is a node.'
  427. );
  428. } else if ( placeOrOffset == 'in' ) {
  429. range = Range.createIn( selectable );
  430. } else if ( placeOrOffset == 'on' ) {
  431. range = Range.createOn( selectable );
  432. } else {
  433. range = Range.createCollapsedAt( selectable, placeOrOffset );
  434. }
  435. this._setRanges( [ range ], backward );
  436. this._setFakeOptions( options );
  437. } else if ( isIterable( selectable ) ) {
  438. // We assume that the selectable is an iterable of ranges.
  439. // Array.from() is used to prevent setting ranges to the old iterable
  440. this._setRanges( selectable, placeOrOffset && placeOrOffset.backward );
  441. this._setFakeOptions( placeOrOffset );
  442. } else {
  443. /**
  444. * Cannot set selection to given place.
  445. *
  446. * @error view-selection-setTo-not-selectable
  447. */
  448. throw new CKEditorError( 'view-selection-setTo-not-selectable: Cannot set selection to given place.' );
  449. }
  450. this.fire( 'change' );
  451. }
  452. /**
  453. * Replaces all ranges that were added to the selection with given array of ranges. Last range of the array
  454. * is treated like the last added range and is used to set {@link #anchor anchor} and {@link #focus focus}.
  455. * Accepts a flag describing in which way the selection is made.
  456. *
  457. * @private
  458. * @param {Iterable.<module:engine/view/range~Range>} newRanges Iterable object of ranges to set.
  459. * @param {Boolean} [isLastBackward=false] Flag describing if last added range was selected forward - from start to end
  460. * (`false`) or backward - from end to start (`true`). Defaults to `false`.
  461. */
  462. _setRanges( newRanges, isLastBackward = false ) {
  463. // New ranges should be copied to prevent removing them by setting them to `[]` first.
  464. // Only applies to situations when selection is set to the same selection or same selection's ranges.
  465. newRanges = Array.from( newRanges );
  466. this._ranges = [];
  467. for ( const range of newRanges ) {
  468. this._addRange( range );
  469. }
  470. this._lastRangeBackward = !!isLastBackward;
  471. }
  472. /**
  473. * Moves {@link #focus} to the specified location.
  474. *
  475. * The location can be specified in the same form as {@link module:engine/view/position~Position.createAt} parameters.
  476. *
  477. * @protected
  478. * @fires change
  479. * @param {module:engine/view/item~Item|module:engine/view/position~Position} itemOrPosition
  480. * @param {Number|'end'|'before'|'after'} [offset=0] Offset or one of the flags. Used only when
  481. * first parameter is a {@link module:engine/view/item~Item view item}.
  482. */
  483. _setFocus( itemOrPosition, offset ) {
  484. if ( this.anchor === null ) {
  485. /**
  486. * Cannot set selection focus if there are no ranges in selection.
  487. *
  488. * @error view-selection-setFocus-no-ranges
  489. */
  490. throw new CKEditorError(
  491. 'view-selection-setFocus-no-ranges: Cannot set selection focus if there are no ranges in selection.'
  492. );
  493. }
  494. const newFocus = Position.createAt( itemOrPosition, offset );
  495. if ( newFocus.compareWith( this.focus ) == 'same' ) {
  496. return;
  497. }
  498. const anchor = this.anchor;
  499. this._ranges.pop();
  500. if ( newFocus.compareWith( anchor ) == 'before' ) {
  501. this._addRange( new Range( newFocus, anchor ), true );
  502. } else {
  503. this._addRange( new Range( anchor, newFocus ) );
  504. }
  505. this.fire( 'change' );
  506. }
  507. /**
  508. * Sets this selection instance to be marked as `fake`. A fake selection does not render as browser native selection
  509. * over selected elements and is hidden to the user. This way, no native selection UI artifacts are displayed to
  510. * the user and selection over elements can be represented in other way, for example by applying proper CSS class.
  511. *
  512. * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM (and be
  513. * properly handled by screen readers).
  514. *
  515. * @private
  516. * @param {Object} [options] Options.
  517. * @param {Boolean} [options.fake] If set to true selection will be marked as `fake`.
  518. * @param {String} [options.label=''] Fake selection label.
  519. */
  520. _setFakeOptions( options = {} ) {
  521. this._isFake = !!options.fake;
  522. this._fakeSelectionLabel = options.fake ? options.label || '' : '';
  523. }
  524. /**
  525. * Adds a range to the selection. Added range is copied. This means that passed range is not saved in the
  526. * selection instance and you can safely operate on it.
  527. *
  528. * Accepts a flag describing in which way the selection is made - passed range might be selected from
  529. * {@link module:engine/view/range~Range#start start} to {@link module:engine/view/range~Range#end end}
  530. * or from {@link module:engine/view/range~Range#end end} to {@link module:engine/view/range~Range#start start}.
  531. * The flag is used to set {@link #anchor anchor} and {@link #focus focus} properties.
  532. *
  533. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-selection-range-intersects` if added range intersects
  534. * with ranges already stored in Selection instance.
  535. *
  536. * @private
  537. * @fires change
  538. * @param {module:engine/view/range~Range} range
  539. * @param {Boolean} [isBackward]
  540. */
  541. _addRange( range, isBackward = false ) {
  542. if ( !( range instanceof Range ) ) {
  543. throw new CKEditorError( 'view-selection-invalid-range: Invalid Range.' );
  544. }
  545. this._pushRange( range );
  546. this._lastRangeBackward = !!isBackward;
  547. }
  548. /**
  549. * Adds range to selection - creates copy of given range so it can be safely used and modified.
  550. *
  551. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-selection-range-intersects` if added range intersects
  552. * with ranges already stored in selection instance.
  553. *
  554. * @private
  555. * @param {module:engine/view/range~Range} range
  556. */
  557. _pushRange( range ) {
  558. for ( const storedRange of this._ranges ) {
  559. if ( range.isIntersecting( storedRange ) ) {
  560. /**
  561. * Trying to add a range that intersects with another range from selection.
  562. *
  563. * @error view-selection-range-intersects
  564. * @param {module:engine/view/range~Range} addedRange Range that was added to the selection.
  565. * @param {module:engine/view/range~Range} intersectingRange Range from selection that intersects with `addedRange`.
  566. */
  567. throw new CKEditorError(
  568. 'view-selection-range-intersects: Trying to add a range that intersects with another range from selection.',
  569. { addedRange: range, intersectingRange: storedRange }
  570. );
  571. }
  572. }
  573. this._ranges.push( Range.createFromRange( range ) );
  574. }
  575. }
  576. mix( Selection, EmitterMixin );
  577. /**
  578. * Fired whenever selection ranges are changed through {@link ~Selection Selection API}.
  579. *
  580. * @event change
  581. */