8
0

position.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import DocumentFragment from './documentfragment.js';
  7. import Element from './element.js';
  8. import last from '../../utils/lib/lodash/last.js';
  9. import compareArrays from '../../utils/comparearrays';
  10. import CKEditorError from '../../utils/ckeditorerror.js';
  11. /**
  12. * Represents a position in the model tree.
  13. *
  14. * Since position in a model is represented by a {@link engine.model.Position#root position root} and
  15. * {@link engine.model.Position#path position path} it is possible to create positions placed in non-existing elements.
  16. * This requirement is important for {@link engine.model.operation.transfrom operational transformation}.
  17. *
  18. * Also, {@link engine.model.operation.Operation operations} kept in {@link engine.model.Document#history document history}
  19. * are storing positions (and ranges) which were correct when those operations were applied, but may not be correct
  20. * after document got changed.
  21. *
  22. * When changes are applied to model, it may also happen that {@link engine.model.Position#parent position parent} will change
  23. * even if position path has not changed. Keep in mind, that if a position leads to non-existing element,
  24. * {@link engine.model.Position#parent} and some other properties and methods will throw errors.
  25. *
  26. * In most cases, position with wrong path is caused by an error in code, but it is sometimes needed, as described above.
  27. *
  28. * @memberOf engine.model
  29. */
  30. export default class Position {
  31. /**
  32. * Creates a position.
  33. *
  34. * @param {engine.model.Element|engine.model.DocumentFragment} root Root of the position.
  35. * @param {Array.<Number>} path Position path. See {@link engine.model.Position#path}.
  36. */
  37. constructor( root, path ) {
  38. if ( !( root instanceof Element ) && !( root instanceof DocumentFragment ) ) {
  39. /**
  40. * Position root invalid.
  41. *
  42. * @error position-root-invalid.
  43. */
  44. throw new CKEditorError( 'position-root-invalid: Position root invalid.' );
  45. }
  46. if ( !( path instanceof Array ) || path.length === 0 ) {
  47. /**
  48. * Position path must be an Array with at least one item.
  49. *
  50. * @error position-path-incorrect
  51. * @param path
  52. */
  53. throw new CKEditorError( 'position-path-incorrect: Position path must be an Array with at least one item.', { path: path } );
  54. }
  55. // Normalize the root and path (if element was passed).
  56. path = root.getPath().concat( path );
  57. root = root.root;
  58. /**
  59. * Root of the position path.
  60. *
  61. * @readonly
  62. * @member {engine.model.Element|engine.model.DocumentFragment} engine.model.Position#root
  63. */
  64. this.root = root;
  65. /**
  66. * Position of the node it the tree.
  67. *
  68. * Position can be placed before, after or in a {@link engine.model.Node node} if that node has
  69. * {@link engine.model.Node#offsetSize} greater than `1`. Items in position path are
  70. * {@link engine.model.Node#startOffset starting offsets} of position ancestors, starting from direct root children,
  71. * down to the position offset in it's parent.
  72. *
  73. * ROOT
  74. * |- P before: [ 0 ] after: [ 1 ]
  75. * |- UL before: [ 1 ] after: [ 2 ]
  76. * |- LI before: [ 1, 0 ] after: [ 1, 1 ]
  77. * | |- foo before: [ 1, 0, 0 ] after: [ 1, 0, 3 ]
  78. * |- LI before: [ 1, 1 ] after: [ 1, 2 ]
  79. * |- bar before: [ 1, 1, 0 ] after: [ 1, 1, 3 ]
  80. *
  81. * `foo` and `bar` are representing {@link engine.model.Text text nodes}. Since text nodes has offset size
  82. * greater than `1` you can place position offset between their start and end:
  83. *
  84. * ROOT
  85. * |- P
  86. * |- UL
  87. * |- LI
  88. * | |- f^o|o ^ has path: [ 1, 0, 1 ] | has path: [ 1, 0, 2 ]
  89. * |- LI
  90. * |- b^a|r ^ has path: [ 1, 1, 1 ] | has path: [ 1, 1, 2 ]
  91. *
  92. * @member {Array.<Number>} engine.model.Position#path
  93. */
  94. this.path = path;
  95. }
  96. /**
  97. * Node directly after the position.
  98. *
  99. * @readonly
  100. * @type {engine.model.Node}
  101. */
  102. get nodeAfter() {
  103. return this.parent.getChild( this.offset ) || null;
  104. }
  105. /**
  106. * Node directly before the position.
  107. *
  108. * @readonly
  109. * @type {Node}
  110. */
  111. get nodeBefore() {
  112. return this.parent.getChild( this.offset - 1 ) || null;
  113. }
  114. /**
  115. * Offset at which this position is located in it's {@link engine.model.Position#parent parent}. It is equal
  116. * to the last item in position {@link engine.model.Position#path path}.
  117. *
  118. * @type {Number}
  119. */
  120. get offset() {
  121. return last( this.path );
  122. }
  123. /**
  124. * @param {Number} newOffset
  125. */
  126. set offset( newOffset ) {
  127. this.path[ this.path.length - 1 ] = newOffset;
  128. }
  129. /**
  130. * Parent element of the position. The position is located at {@link #offset} in this element.
  131. *
  132. * @readonly
  133. * @type {engine.model.Element}
  134. */
  135. get parent() {
  136. let parent = this.root;
  137. let i, len;
  138. for ( i = 0, len = this.path.length - 1; i < len; i++ ) {
  139. parent = parent.getChild( this.path[ i ] );
  140. }
  141. return parent;
  142. }
  143. /**
  144. * Checks whether this position is before or after given position.
  145. *
  146. * @param {engine.model.Position} otherPosition Position to compare with.
  147. * @returns {engine.model.PositionRelation}
  148. */
  149. compareWith( otherPosition ) {
  150. if ( this.root != otherPosition.root ) {
  151. return 'different';
  152. }
  153. const result = compareArrays( this.path, otherPosition.path );
  154. switch ( result ) {
  155. case 'same':
  156. return 'same';
  157. case 'prefix':
  158. return 'before';
  159. case 'extension':
  160. return 'after';
  161. default:
  162. if ( this.path[ result ] < otherPosition.path[ result ] ) {
  163. return 'before';
  164. } else {
  165. return 'after';
  166. }
  167. }
  168. }
  169. /**
  170. * Returns a path to this position's parent. Parent path is equal to position {@link engine.model.Position#path path}
  171. * but without the last item.
  172. *
  173. * This method returns the parent path even if the parent does not exists.
  174. *
  175. * @returns {Array.<Number>} Path to the parent.
  176. */
  177. getParentPath() {
  178. return this.path.slice( 0, -1 );
  179. }
  180. /**
  181. * Returns a new instance of `Position`, that has same {@link engine.model.Position#parent parent} but it's offset
  182. * is shifted by `shift` value (can be a negative value).
  183. *
  184. * @param {Number} shift Offset shift. Can be a negative value.
  185. * @returns {engine.model.Position} Shifted position.
  186. */
  187. getShiftedBy( shift ) {
  188. let shifted = Position.createFromPosition( this );
  189. let offset = shifted.offset + shift;
  190. shifted.offset = offset < 0 ? 0 : offset;
  191. return shifted;
  192. }
  193. /**
  194. * Returns a copy of this position that is updated by removing `howMany` nodes starting from `deletePosition`.
  195. * It may happen that this position is in a removed node. If that is the case, `null` is returned instead.
  196. *
  197. * @protected
  198. * @param {engine.model.Position} deletePosition Position before the first removed node.
  199. * @param {Number} howMany How many nodes are removed.
  200. * @returns {engine.model.Position|null} Transformed position or `null`.
  201. */
  202. getTransformedByDeletion( deletePosition, howMany ) {
  203. let transformed = Position.createFromPosition( this );
  204. // This position can't be affected if deletion was in a different root.
  205. if ( this.root != deletePosition.root ) {
  206. return transformed;
  207. }
  208. if ( compareArrays( deletePosition.getParentPath(), this.getParentPath() ) == 'same' ) {
  209. // If nodes are removed from the node that is pointed by this position...
  210. if ( deletePosition.offset < this.offset ) {
  211. // And are removed from before an offset of that position...
  212. if ( deletePosition.offset + howMany > this.offset ) {
  213. // Position is in removed range, it's no longer in the tree.
  214. return null;
  215. } else {
  216. // Decrement the offset accordingly.
  217. transformed.offset -= howMany;
  218. }
  219. }
  220. } else if ( compareArrays( deletePosition.getParentPath(), this.getParentPath() ) == 'prefix' ) {
  221. // If nodes are removed from a node that is on a path to this position...
  222. const i = deletePosition.path.length - 1;
  223. if ( deletePosition.offset <= this.path[ i ] ) {
  224. // And are removed from before next node of that path...
  225. if ( deletePosition.offset + howMany > this.path[ i ] ) {
  226. // If the next node of that path is removed return null
  227. // because the node containing this position got removed.
  228. return null;
  229. } else {
  230. // Otherwise, decrement index on that path.
  231. transformed.path[ i ] -= howMany;
  232. }
  233. }
  234. }
  235. return transformed;
  236. }
  237. /**
  238. * Returns a copy of this position that is updated by inserting `howMany` nodes at `insertPosition`.
  239. *
  240. * @protected
  241. * @param {engine.model.Position} insertPosition Position where nodes are inserted.
  242. * @param {Number} howMany How many nodes are inserted.
  243. * @param {Boolean} insertBefore Flag indicating whether nodes are inserted before or after `insertPosition`.
  244. * This is important only when `insertPosition` and this position are same. If that is the case and the flag is
  245. * set to `true`, this position will get transformed. If the flag is set to `false`, it won't.
  246. * @returns {engine.model.Position} Transformed position.
  247. */
  248. getTransformedByInsertion( insertPosition, howMany, insertBefore ) {
  249. let transformed = Position.createFromPosition( this );
  250. // This position can't be affected if insertion was in a different root.
  251. if ( this.root != insertPosition.root ) {
  252. return transformed;
  253. }
  254. if ( compareArrays( insertPosition.getParentPath(), this.getParentPath() ) == 'same' ) {
  255. // If nodes are inserted in the node that is pointed by this position...
  256. if ( insertPosition.offset < this.offset || ( insertPosition.offset == this.offset && insertBefore ) ) {
  257. // And are inserted before an offset of that position...
  258. // "Push" this positions offset.
  259. transformed.offset += howMany;
  260. }
  261. } else if ( compareArrays( insertPosition.getParentPath(), this.getParentPath() ) == 'prefix' ) {
  262. // If nodes are inserted in a node that is on a path to this position...
  263. const i = insertPosition.path.length - 1;
  264. if ( insertPosition.offset <= this.path[ i ] ) {
  265. // And are inserted before next node of that path...
  266. // "Push" the index on that path.
  267. transformed.path[ i ] += howMany;
  268. }
  269. }
  270. return transformed;
  271. }
  272. /**
  273. * Returns a copy of this position that is updated by moving `howMany` nodes from `sourcePosition` to `targetPosition`.
  274. *
  275. * @protected
  276. * @param {engine.model.Position} sourcePosition Position before the first element to move.
  277. * @param {engine.model.Position} targetPosition Position where moved elements will be inserted.
  278. * @param {Number} howMany How many consecutive nodes to move, starting from `sourcePosition`.
  279. * @param {Boolean} insertBefore Flag indicating whether moved nodes are pasted before or after `insertPosition`.
  280. * This is important only when `targetPosition` and this position are same. If that is the case and the flag is
  281. * set to `true`, this position will get transformed by range insertion. If the flag is set to `false`, it won't.
  282. * @param {Boolean} [sticky] Flag indicating whether this position "sticks" to range, that is if it should be moved
  283. * with the moved range if it is equal to one of range's boundaries.
  284. * @returns {engine.model.Position} Transformed position.
  285. */
  286. getTransformedByMove( sourcePosition, targetPosition, howMany, insertBefore, sticky ) {
  287. // Moving a range removes nodes from their original position. We acknowledge this by proper transformation.
  288. let transformed = this.getTransformedByDeletion( sourcePosition, howMany );
  289. // Then we update target position, as it could be affected by nodes removal too.
  290. targetPosition = targetPosition.getTransformedByDeletion( sourcePosition, howMany );
  291. if ( transformed === null || ( sticky && transformed.isEqual( sourcePosition ) ) ) {
  292. // This position is inside moved range (or sticks to it).
  293. // In this case, we calculate a combination of this position, move source position and target position.
  294. transformed = this._getCombined( sourcePosition, targetPosition );
  295. } else {
  296. // This position is not inside a removed range.
  297. // In next step, we simply reflect inserting `howMany` nodes, which might further affect the position.
  298. transformed = transformed.getTransformedByInsertion( targetPosition, howMany, insertBefore );
  299. }
  300. return transformed;
  301. }
  302. /**
  303. * Checks whether this position is after given position.
  304. *
  305. * @see engine.model.Position#isBefore
  306. *
  307. * @param {engine.model.Position} otherPosition Position to compare with.
  308. * @returns {Boolean} True if this position is after given position.
  309. */
  310. isAfter( otherPosition ) {
  311. return this.compareWith( otherPosition ) == 'after';
  312. }
  313. /**
  314. * Checks whether this position is before given position.
  315. *
  316. * **Note:** watch out when using negation of the value returned by this method, because the negation will also
  317. * be `true` if positions are in different roots and you might not expect this. You should probably use
  318. * `a.isAfter( b ) || a.isEqual( b )` or `!a.isBefore( p ) && a.root == b.root` in most scenarios. If your
  319. * condition uses multiple `isAfter` and `isBefore` checks, build them so they do not use negated values, i.e.:
  320. *
  321. * if ( a.isBefore( b ) && c.isAfter( d ) ) {
  322. * // do A.
  323. * } else {
  324. * // do B.
  325. * }
  326. *
  327. * or, if you have only one if-branch:
  328. *
  329. * if ( !( a.isBefore( b ) && c.isAfter( d ) ) {
  330. * // do B.
  331. * }
  332. *
  333. * rather than:
  334. *
  335. * if ( !a.isBefore( b ) || && !c.isAfter( d ) ) {
  336. * // do B.
  337. * } else {
  338. * // do A.
  339. * }
  340. *
  341. * @param {engine.model.Position} otherPosition Position to compare with.
  342. * @returns {Boolean} True if this position is before given position.
  343. */
  344. isBefore( otherPosition ) {
  345. return this.compareWith( otherPosition ) == 'before';
  346. }
  347. /**
  348. * Checks whether this position equals given position.
  349. *
  350. * @param {engine.model.Position} otherPosition Position to compare with.
  351. * @returns {Boolean} True if positions are same.
  352. */
  353. isEqual( otherPosition ) {
  354. return this.compareWith( otherPosition ) == 'same';
  355. }
  356. /**
  357. * Checks whether this position is touching given position. Positions touch when there are no characters
  358. * or empty nodes in a range between them. Technically, those positions are not equal but in many cases
  359. * they are very similar or even indistinguishable when they touch.
  360. *
  361. * @param {engine.model.Position} otherPosition Position to compare with.
  362. * @returns {Boolean} True if positions touch.
  363. */
  364. isTouching( otherPosition ) {
  365. let left = null;
  366. let right = null;
  367. let compare = this.compareWith( otherPosition );
  368. switch ( compare ) {
  369. case 'same':
  370. return true;
  371. case 'before':
  372. left = Position.createFromPosition( this );
  373. right = Position.createFromPosition( otherPosition );
  374. break;
  375. case 'after':
  376. left = Position.createFromPosition( otherPosition );
  377. right = Position.createFromPosition( this );
  378. break;
  379. default:
  380. return false;
  381. }
  382. while ( left.path.length + right.path.length ) {
  383. if ( left.isEqual( right ) ) {
  384. return true;
  385. }
  386. if ( left.path.length > right.path.length ) {
  387. if ( left.nodeAfter !== null ) {
  388. return false;
  389. }
  390. left.path = left.path.slice( 0, -1 );
  391. left.offset++;
  392. } else {
  393. if ( right.nodeBefore !== null ) {
  394. return false;
  395. }
  396. right.path = right.path.slice( 0, -1 );
  397. }
  398. }
  399. }
  400. /**
  401. * Whether position is at the beginning of its {@link engine.model.Position#parent}.
  402. *
  403. * @returns {Boolean}
  404. */
  405. isAtStart() {
  406. return this.offset === 0;
  407. }
  408. /**
  409. * Whether position is at the end of its {@link engine.model.Position#parent}.
  410. *
  411. * @returns {Boolean}
  412. */
  413. isAtEnd() {
  414. return this.offset == this.parent.getChildCount();
  415. }
  416. /**
  417. * Creates position at the given location. The location can be specified as:
  418. *
  419. * * a {@link engine.model.Position position},
  420. * * parent element and offset (offset defaults to `0`),
  421. * * parent element and `'end'` (sets selection at the end of that element),
  422. * * node and `'before'` or `'after'` (sets selection before or after the given node).
  423. *
  424. * This method is a shortcut to other constructors such as:
  425. *
  426. * * {@link engine.model.Position.createBefore},
  427. * * {@link engine.model.Position.createAfter},
  428. * * {@link engine.model.Position.createFromParentAndOffset},
  429. * * {@link engine.model.Position.createFromPosition}.
  430. *
  431. * @param {engine.model.Node|engine.model.Position} nodeOrPosition
  432. * @param {Number|'end'|'before'|'after'} [offset=0] Offset or one of the flags. Used only when
  433. * first parameter is a node.
  434. */
  435. static createAt( nodeOrPosition, offset ) {
  436. let node;
  437. if ( nodeOrPosition instanceof Position ) {
  438. return this.createFromPosition( nodeOrPosition );
  439. } else {
  440. node = nodeOrPosition;
  441. if ( offset == 'end' ) {
  442. offset = node.getChildCount();
  443. } else if ( offset == 'before' ) {
  444. return this.createBefore( node );
  445. } else if ( offset == 'after' ) {
  446. return this.createAfter( node );
  447. } else if ( !offset ) {
  448. offset = 0;
  449. }
  450. return this.createFromParentAndOffset( node, offset );
  451. }
  452. }
  453. /**
  454. * Creates a new position after given node.
  455. *
  456. * @see {@link engine.model.TreeWalkerValue}
  457. *
  458. * @param {engine.model.Node} node Node the position should be directly after.
  459. * @returns {engine.model.Position}
  460. */
  461. static createAfter( node ) {
  462. if ( !node.parent ) {
  463. /**
  464. * You can not make position after root.
  465. *
  466. * @error position-after-root
  467. * @param {engine.model.Node} root
  468. */
  469. throw new CKEditorError( 'position-after-root: You can not make position after root.', { root: node } );
  470. }
  471. return this.createFromParentAndOffset( node.parent, node.getIndex() + 1 );
  472. }
  473. /**
  474. * Creates a new position before the given node.
  475. *
  476. * @see {@link engine.model.TreeWalkerValue}
  477. *
  478. * @param {engine.model.node} node Node the position should be directly before.
  479. * @returns {engine.model.Position}
  480. */
  481. static createBefore( node ) {
  482. if ( !node.parent ) {
  483. /**
  484. * You can not make position before root.
  485. *
  486. * @error position-before-root
  487. * @param {engine.model.Node} root
  488. */
  489. throw new CKEditorError( 'position-before-root: You can not make position before root.', { root: node } );
  490. }
  491. return this.createFromParentAndOffset( node.parent, node.getIndex() );
  492. }
  493. /**
  494. * Creates a new position from the parent element and an offset in that element.
  495. *
  496. * @param {engine.model.Element|engine.model.DocumentFragment} parent Position's parent.
  497. * @param {Number} offset Position's offset.
  498. * @returns {engine.model.Position}
  499. */
  500. static createFromParentAndOffset( parent, offset ) {
  501. if ( !( parent instanceof Element || parent instanceof DocumentFragment ) ) {
  502. /**
  503. * Position parent have to be a model element or model document fragment.
  504. *
  505. * @error position-parent-incorrect
  506. */
  507. throw new CKEditorError( 'position-parent-incorrect: Position parent have to be a element or document fragment.' );
  508. }
  509. const path = parent.getPath();
  510. path.push( offset );
  511. return new this( parent.root, path );
  512. }
  513. /**
  514. * Creates a new position, which is equal to passed position.
  515. *
  516. * @param {engine.model.Position} position Position to be cloned.
  517. * @returns {engine.model.Position}
  518. */
  519. static createFromPosition( position ) {
  520. return new this( position.root, position.path.slice() );
  521. }
  522. /**
  523. * Creates a `Position` instance from given plain object (i.e. parsed JSON string).
  524. *
  525. * @param {Object} json Plain object to be converted to `Position`.
  526. * @returns {engine.model.Position} `Position` instance created using given plain object.
  527. */
  528. static fromJSON( json, doc ) {
  529. if ( json.root === '$graveyard' ) {
  530. return new Position( doc.graveyard, json.path );
  531. }
  532. if ( !doc.hasRoot( json.root ) ) {
  533. /**
  534. * Cannot create position for document. Root with specified name does not exist.
  535. *
  536. * @error position-fromjson-no-root
  537. * @param {String} rootName
  538. */
  539. throw new CKEditorError(
  540. 'position-fromjson-no-root: Cannot create position for document. Root with specified name does not exist.',
  541. { rootName: json.root }
  542. );
  543. }
  544. return new Position( doc.getRoot( json.root ), json.path );
  545. }
  546. /**
  547. * Returns a new position that is a combination of this position and given positions. The combined
  548. * position is this position transformed by moving a range starting at `from` to `to` position.
  549. * It is expected that this position is inside the moved range.
  550. *
  551. * In other words, this method in a smart way "cuts out" `source` path from this position and
  552. * injects `target` path in it's place, while doing necessary fixes in order to get a correct path.
  553. *
  554. * Example:
  555. *
  556. * let original = new Position( root, [ 2, 3, 1 ] );
  557. * let source = new Position( root, [ 2, 2 ] );
  558. * let target = new Position( otherRoot, [ 1, 1, 3 ] );
  559. * let combined = original.getCombined( source, target );
  560. * // combined.path is [ 1, 1, 4, 1 ], combined.root is otherRoot
  561. *
  562. * Explanation:
  563. *
  564. * We have a position `[ 2, 3, 1 ]` and move some nodes from `[ 2, 2 ]` to `[ 1, 1, 3 ]`. The original position
  565. * was inside moved nodes and now should point to the new place. The moved nodes will be after
  566. * positions `[ 1, 1, 3 ]`, `[ 1, 1, 4 ]`, `[ 1, 1, 5 ]`. Since our position was in the second moved node,
  567. * the transformed position will be in a sub-tree of a node at `[ 1, 1, 4 ]`. Looking at original path, we
  568. * took care of `[ 2, 3 ]` part of it. Now we have to add the rest of the original path to the transformed path.
  569. * Finally, the transformed position will point to `[ 1, 1, 4, 1 ]`.
  570. *
  571. * @protected
  572. * @param {engine.model.Position} source Beginning of the moved range.
  573. * @param {engine.model.Position} target Position where the range is moved.
  574. * @returns {engine.model.Position} Combined position.
  575. */
  576. _getCombined( source, target ) {
  577. const i = source.path.length - 1;
  578. // The first part of a path to combined position is a path to the place where nodes were moved.
  579. let combined = Position.createFromPosition( target );
  580. // Then we have to update the rest of the path.
  581. // Fix the offset because this position might be after `from` position and we have to reflect that.
  582. combined.offset = combined.offset + this.path[ i ] - source.offset;
  583. // Then, add the rest of the path.
  584. // If this position is at the same level as `from` position nothing will get added.
  585. combined.path = combined.path.concat( this.path.slice( i + 1 ) );
  586. return combined;
  587. }
  588. }
  589. /**
  590. * A flag indicating whether this position is `'before'` or `'after'` or `'same'` as given position.
  591. * If positions are in different roots `'different'` flag is returned.
  592. *
  593. * @typedef {String} engine.model.PositionRelation
  594. */