position.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import compareArrays from '../../utils/comparearrays.js';
  7. import Text from './text.js';
  8. /**
  9. * Position in the tree. Position is always located before or after a node.
  10. *
  11. * @memberOf engine.treeView
  12. */
  13. export default class Position {
  14. /**
  15. * Creates a position.
  16. *
  17. * @param {engine.treeView.Node} parent Position parent node.
  18. * @param {Number} offset Position offset.
  19. */
  20. constructor( parent, offset ) {
  21. /**
  22. * Position parent node.
  23. *
  24. * @member {engine.treeView.Node} engine.treeView.Position#parent
  25. */
  26. this.parent = parent;
  27. /**
  28. * Position offset.
  29. *
  30. * @member {Number} engine.treeView.Position#offset
  31. */
  32. this.offset = offset;
  33. }
  34. /**
  35. * Node directly after the position. Equals `null` when there is no node after position or position is located
  36. * inside text node.
  37. *
  38. * @readonly
  39. * @type {engine.treeView.Node|null}
  40. */
  41. get nodeAfter() {
  42. if ( this.parent instanceof Text ) {
  43. return null;
  44. }
  45. return this.parent.getChild( this.offset ) || null;
  46. }
  47. /**
  48. * Node directly before the position. Equals `null` when there is no node before position or position is located
  49. * inside text node.
  50. *
  51. * @readonly
  52. * @type {engine.treeView.Node|null}
  53. */
  54. get nodeBefore() {
  55. if ( this.parent instanceof Text ) {
  56. return null;
  57. }
  58. return this.parent.getChild( this.offset - 1 ) || null;
  59. }
  60. /**
  61. * Returns a new instance of Position with offset incremented by `shift` value.
  62. *
  63. * @param {Number} shift How position offset should get changed. Accepts negative values.
  64. * @returns {engine.treeView.Position} Shifted position.
  65. */
  66. getShiftedBy( shift ) {
  67. let shifted = Position.createFromPosition( this );
  68. let offset = shifted.offset + shift;
  69. shifted.offset = offset < 0 ? 0 : offset;
  70. return shifted;
  71. }
  72. /**
  73. * Checks whether this position equals given position.
  74. *
  75. * @param {engine.treeView.Position} otherPosition Position to compare with.
  76. * @returns {Boolean} True if positions are same.
  77. */
  78. isEqual( otherPosition ) {
  79. return this == otherPosition || ( this.parent == otherPosition.parent && this.offset == otherPosition.offset );
  80. }
  81. /**
  82. * Checks whether this position is located before given position. When method returns `false` it does not mean that
  83. * this position is after give one. Two positions may be located inside separate roots and in that situation this
  84. * method will still return `false`.
  85. *
  86. * @see engine.treeView.Position#isAfter
  87. * @see engine.treeView.Position#compareWith
  88. * @param {engine.treeView.Position} otherPosition Position to compare with.
  89. * @returns {Boolean} Returns `true` if this position is before given position.
  90. */
  91. isBefore( otherPosition ) {
  92. return this.compareWith( otherPosition ) == 'BEFORE';
  93. }
  94. /**
  95. * Checks whether this position is located after given position. When method returns `false` it does not mean that
  96. * this position is before give one. Two positions may be located inside separate roots and in that situation this
  97. * method will still return `false`.
  98. *
  99. * @see engine.treeView.Position#isBefore
  100. * @see engine.treeView.Position#compareWith
  101. * @param {engine.treeView.Position} otherPosition Position to compare with.
  102. * @returns {Boolean} Returns `true` if this position is after given position.
  103. */
  104. isAfter( otherPosition ) {
  105. return this.compareWith( otherPosition ) == 'AFTER';
  106. }
  107. /**
  108. * Checks whether this position is before, after or in same position that other position. Two positions may be also
  109. * different when they are located in separate roots.
  110. *
  111. * @param {engine.treeView.Position} otherPosition Position to compare with.
  112. * @returns {engine.treeView.PositionRelation}
  113. */
  114. compareWith( otherPosition ) {
  115. if ( this.isEqual( otherPosition ) ) {
  116. return 'SAME';
  117. }
  118. // If positions have same parent.
  119. if ( this.parent === otherPosition.parent ) {
  120. return this.offset - otherPosition.offset < 0 ? 'BEFORE' : 'AFTER';
  121. }
  122. // Get path from root to position's parent element.
  123. const path = this.parent.getAncestors( { includeNode: true } );
  124. const otherPath = otherPosition.parent.getAncestors( { includeNode: true } );
  125. // Compare both path arrays to find common ancestor.
  126. const result = compareArrays( path, otherPath );
  127. let commonAncestorIndex;
  128. switch ( result ) {
  129. case 0:
  130. // No common ancestors found.
  131. return 'DIFFERENT';
  132. case 'PREFIX':
  133. commonAncestorIndex = path.length - 1;
  134. break;
  135. case 'EXTENSION':
  136. commonAncestorIndex = otherPath.length - 1;
  137. break;
  138. default:
  139. commonAncestorIndex = result - 1;
  140. }
  141. // Common ancestor of two positions.
  142. const commonAncestor = path[ commonAncestorIndex ];
  143. const nextAncestor1 = path[ commonAncestorIndex + 1 ];
  144. const nextAncestor2 = otherPath[ commonAncestorIndex + 1 ];
  145. // Check if common ancestor is not one of the parents.
  146. if ( commonAncestor === this.parent ) {
  147. const index = this.offset - nextAncestor2.getIndex();
  148. return index <= 0 ? 'BEFORE' : 'AFTER';
  149. } else if ( commonAncestor === otherPosition.parent ) {
  150. const index = nextAncestor1.getIndex() - otherPosition.offset;
  151. return index < 0 ? 'BEFORE' : 'AFTER';
  152. }
  153. const index = nextAncestor1.getIndex() - nextAncestor2.getIndex();
  154. // Compare indexes of next ancestors inside common one.
  155. return index < 0 ? 'BEFORE' : 'AFTER';
  156. }
  157. /**
  158. * Creates a new position after given node.
  159. *
  160. * @see {@link engine.treeView.TreeWalkerValue}
  161. *
  162. * @param {engine.treeView.Node} node Node the position should be directly after.
  163. * @returns {engine.treeView.Position}
  164. */
  165. static createAfter( node ) {
  166. if ( !node.parent ) {
  167. /**
  168. * You can not make position after root.
  169. *
  170. * @error position-after-root
  171. * @param {engine.treeView.Node} root
  172. */
  173. throw new CKEditorError( 'position-after-root: You can not make position after root.', { root: node } );
  174. }
  175. return new Position( node.parent, node.getIndex() + 1 );
  176. }
  177. /**
  178. * Creates a new position before the given node.
  179. *
  180. * @see {@link engine.treeView.TreeWalkerValue}
  181. *
  182. * @param {engine.treeView.node} node Node the position should be directly before.
  183. * @returns {engine.treeView.Position}
  184. */
  185. static createBefore( node ) {
  186. if ( !node.parent ) {
  187. /**
  188. * You can not make position before root.
  189. *
  190. * @error position-before-root
  191. * @param {engine.treeView.Node} root
  192. */
  193. throw new CKEditorError( 'position-before-root: You can not make position before root.', { root: node } );
  194. }
  195. return new Position( node.parent, node.getIndex() );
  196. }
  197. /**
  198. * Creates and returns a new instance of Position, which is equal to passed position.
  199. *
  200. * @param {engine.treeView.Position} position Position to be cloned.
  201. * @returns {engine.treeView.Position}
  202. */
  203. static createFromPosition( position ) {
  204. return new this( position.parent, position.offset );
  205. }
  206. }
  207. /**
  208. * A flag indicating whether this position is `'BEFORE'` or `'AFTER'` or `'SAME'` as given position.
  209. * If positions are in different roots `'DIFFERENT'` flag is returned.
  210. *
  211. * @typedef {String} engine.treeView.PositionRelation
  212. */