position.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/view/position
  7. */
  8. import Text from './text';
  9. import TextProxy from './textproxy';
  10. import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
  11. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. import EditableElement from './editableelement';
  13. /**
  14. * Position in the tree. Position is always located before or after a node.
  15. */
  16. export default class Position {
  17. /**
  18. * Creates a position.
  19. *
  20. * @param {module:engine/view/node~Node} parent Position parent node.
  21. * @param {Number} offset Position offset.
  22. */
  23. constructor( parent, offset ) {
  24. /**
  25. * Position parent node.
  26. *
  27. * @member {module:engine/view/node~Node} module:engine/view/position~Position#parent
  28. */
  29. this.parent = parent;
  30. /**
  31. * Position offset.
  32. *
  33. * @member {Number} module:engine/view/position~Position#offset
  34. */
  35. this.offset = offset;
  36. }
  37. /**
  38. * Node directly after the position. Equals `null` when there is no node after position or position is located
  39. * inside text node.
  40. *
  41. * @readonly
  42. * @type {module:engine/view/node~Node|null}
  43. */
  44. get nodeAfter() {
  45. if ( this.parent instanceof Text ) {
  46. return null;
  47. }
  48. return this.parent.getChild( this.offset ) || null;
  49. }
  50. /**
  51. * Node directly before the position. Equals `null` when there is no node before position or position is located
  52. * inside text node.
  53. *
  54. * @readonly
  55. * @type {module:engine/view/node~Node|null}
  56. */
  57. get nodeBefore() {
  58. if ( this.parent instanceof Text ) {
  59. return null;
  60. }
  61. return this.parent.getChild( this.offset - 1 ) || null;
  62. }
  63. /**
  64. * Is `true` if position is at the beginning of its {@link module:engine/view/position~Position#parent parent}, `false` otherwise.
  65. *
  66. * @readonly
  67. * @type {Boolean}
  68. */
  69. get isAtStart() {
  70. return this.offset === 0;
  71. }
  72. /**
  73. * Is `true` if position is at the end of its {@link module:engine/view/position~Position#parent parent}, `false` otherwise.
  74. *
  75. * @readonly
  76. * @type {Boolean}
  77. */
  78. get isAtEnd() {
  79. const endOffset = this.parent instanceof Text ? this.parent.data.length : this.parent.childCount;
  80. return this.offset === endOffset;
  81. }
  82. /**
  83. * Position's root, that is the root of the position's parent element.
  84. *
  85. * @readonly
  86. * @type {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
  87. */
  88. get root() {
  89. return this.parent.root;
  90. }
  91. /**
  92. * {@link module:engine/view/editableelement~EditableElement EditableElement} instance that contains this position, or `null` if
  93. * position is not inside an editable element.
  94. *
  95. * @type {module:engine/view/editableelement~EditableElement|null}
  96. */
  97. get editableElement() {
  98. let editable = this.parent;
  99. while ( !( editable instanceof EditableElement ) ) {
  100. if ( editable.parent ) {
  101. editable = editable.parent;
  102. } else {
  103. return null;
  104. }
  105. }
  106. return editable;
  107. }
  108. /**
  109. * Returns a new instance of Position with offset incremented by `shift` value.
  110. *
  111. * @param {Number} shift How position offset should get changed. Accepts negative values.
  112. * @returns {module:engine/view/position~Position} Shifted position.
  113. */
  114. getShiftedBy( shift ) {
  115. let shifted = Position.createFromPosition( this );
  116. let offset = shifted.offset + shift;
  117. shifted.offset = offset < 0 ? 0 : offset;
  118. return shifted;
  119. }
  120. /**
  121. * Returns ancestors array of this position, that is this position's parent and it's ancestors.
  122. *
  123. * @returns {Array} Array with ancestors.
  124. */
  125. getAncestors() {
  126. return this.parent.getAncestors( { includeNode: true, parentFirst: true } );
  127. }
  128. /**
  129. * Checks whether this position equals given position.
  130. *
  131. * @param {module:engine/view/position~Position} otherPosition Position to compare with.
  132. * @returns {Boolean} True if positions are same.
  133. */
  134. isEqual( otherPosition ) {
  135. return this == otherPosition || ( this.parent == otherPosition.parent && this.offset == otherPosition.offset );
  136. }
  137. /**
  138. * Checks whether this position is located before given position. When method returns `false` it does not mean that
  139. * this position is after give one. Two positions may be located inside separate roots and in that situation this
  140. * method will still return `false`.
  141. *
  142. * @see module:engine/view/position~Position#isAfter
  143. * @see module:engine/view/position~Position#compareWith
  144. * @param {module:engine/view/position~Position} otherPosition Position to compare with.
  145. * @returns {Boolean} Returns `true` if this position is before given position.
  146. */
  147. isBefore( otherPosition ) {
  148. return this.compareWith( otherPosition ) == 'before';
  149. }
  150. /**
  151. * Checks whether this position is located after given position. When method returns `false` it does not mean that
  152. * this position is before give one. Two positions may be located inside separate roots and in that situation this
  153. * method will still return `false`.
  154. *
  155. * @see module:engine/view/position~Position#isBefore
  156. * @see module:engine/view/position~Position#compareWith
  157. * @param {module:engine/view/position~Position} otherPosition Position to compare with.
  158. * @returns {Boolean} Returns `true` if this position is after given position.
  159. */
  160. isAfter( otherPosition ) {
  161. return this.compareWith( otherPosition ) == 'after';
  162. }
  163. /**
  164. * Checks whether this position is before, after or in same position that other position. Two positions may be also
  165. * different when they are located in separate roots.
  166. *
  167. * @param {module:engine/view/position~Position} otherPosition Position to compare with.
  168. * @returns {module:engine/view/position~PositionRelation}
  169. */
  170. compareWith( otherPosition ) {
  171. if ( this.isEqual( otherPosition ) ) {
  172. return 'same';
  173. }
  174. // If positions have same parent.
  175. if ( this.parent === otherPosition.parent ) {
  176. return this.offset - otherPosition.offset < 0 ? 'before' : 'after';
  177. }
  178. // Get path from root to position's parent element.
  179. const path = this.parent.getAncestors( { includeNode: true } );
  180. const otherPath = otherPosition.parent.getAncestors( { includeNode: true } );
  181. // Compare both path arrays to find common ancestor.
  182. const result = compareArrays( path, otherPath );
  183. let commonAncestorIndex;
  184. switch ( result ) {
  185. case 0:
  186. // No common ancestors found.
  187. return 'different';
  188. case 'prefix':
  189. commonAncestorIndex = path.length - 1;
  190. break;
  191. case 'extension':
  192. commonAncestorIndex = otherPath.length - 1;
  193. break;
  194. default:
  195. commonAncestorIndex = result - 1;
  196. }
  197. // Common ancestor of two positions.
  198. const commonAncestor = path[ commonAncestorIndex ];
  199. const nextAncestor1 = path[ commonAncestorIndex + 1 ];
  200. const nextAncestor2 = otherPath[ commonAncestorIndex + 1 ];
  201. // Check if common ancestor is not one of the parents.
  202. if ( commonAncestor === this.parent ) {
  203. const index = this.offset - nextAncestor2.index;
  204. return index <= 0 ? 'before' : 'after';
  205. } else if ( commonAncestor === otherPosition.parent ) {
  206. const index = nextAncestor1.index - otherPosition.offset;
  207. return index < 0 ? 'before' : 'after';
  208. }
  209. const index = nextAncestor1.index - nextAncestor2.index;
  210. // Compare indexes of next ancestors inside common one.
  211. return index < 0 ? 'before' : 'after';
  212. }
  213. /**
  214. * Creates position at the given location. The location can be specified as:
  215. *
  216. * * a {@link module:engine/view/position~Position position},
  217. * * parent element and offset (offset defaults to `0`),
  218. * * parent element and `'end'` (sets position at the end of that element),
  219. * * {@link module:engine/view/item~Item view item} and `'before'` or `'after'` (sets position before or after given view item).
  220. *
  221. * This method is a shortcut to other constructors such as:
  222. *
  223. * * {@link module:engine/view/position~Position.createBefore},
  224. * * {@link module:engine/view/position~Position.createAfter},
  225. * * {@link module:engine/view/position~Position.createFromPosition}.
  226. *
  227. * @param {module:engine/view/item~Item|module:engine/model/position~Position} itemOrPosition
  228. * @param {Number|'end'|'before'|'after'} [offset=0] Offset or one of the flags. Used only when
  229. * first parameter is a {@link module:engine/view/item~Item view item}.
  230. */
  231. static createAt( itemOrPosition, offset ) {
  232. if ( itemOrPosition instanceof Position ) {
  233. return this.createFromPosition( itemOrPosition );
  234. } else {
  235. let node = itemOrPosition;
  236. if ( offset == 'end' ) {
  237. offset = node instanceof Text ? node.data.length : node.childCount;
  238. } else if ( offset == 'before' ) {
  239. return this.createBefore( node );
  240. } else if ( offset == 'after' ) {
  241. return this.createAfter( node );
  242. } else if ( !offset ) {
  243. offset = 0;
  244. }
  245. return new Position( node, offset );
  246. }
  247. }
  248. /**
  249. * Creates a new position after given view item.
  250. *
  251. * @param {module:engine/view/item~Item} item View item after which the position should be located.
  252. * @returns {module:engine/view/position~Position}
  253. */
  254. static createAfter( item ) {
  255. // TextProxy is not a instance of Node so we need do handle it in specific way.
  256. if ( item instanceof TextProxy ) {
  257. return new Position( item.textNode, item.offsetInText + item.data.length );
  258. }
  259. if ( !item.parent ) {
  260. /**
  261. * You can not make a position after a root.
  262. *
  263. * @error position-after-root
  264. * @param {module:engine/view/node~Node} root
  265. */
  266. throw new CKEditorError( 'view-position-after-root: You can not make position after root.', { root: item } );
  267. }
  268. return new Position( item.parent, item.index + 1 );
  269. }
  270. /**
  271. * Creates a new position before given view item.
  272. *
  273. * @param {module:engine/view/item~Item} item View item before which the position should be located.
  274. * @returns {module:engine/view/position~Position}
  275. */
  276. static createBefore( item ) {
  277. // TextProxy is not a instance of Node so we need do handle it in specific way.
  278. if ( item instanceof TextProxy ) {
  279. return new Position( item.textNode, item.offsetInText );
  280. }
  281. if ( !item.parent ) {
  282. /**
  283. * You cannot make a position before a root.
  284. *
  285. * @error position-before-root
  286. * @param {module:engine/view/node~Node} root
  287. */
  288. throw new CKEditorError( 'view-position-before-root: You can not make position before root.', { root: item } );
  289. }
  290. return new Position( item.parent, item.index );
  291. }
  292. /**
  293. * Creates and returns a new instance of `Position`, which is equal to the passed position.
  294. *
  295. * @param {module:engine/view/position~Position} position Position to be cloned.
  296. * @returns {module:engine/view/position~Position}
  297. */
  298. static createFromPosition( position ) {
  299. return new this( position.parent, position.offset );
  300. }
  301. }
  302. /**
  303. * A flag indicating whether this position is `'before'` or `'after'` or `'same'` as given position.
  304. * If positions are in different roots `'different'` flag is returned.
  305. *
  306. * @typedef {String} module:engine/view/position~PositionRelation
  307. */