position.js 10 KB

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