position.js 7.4 KB

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