8
0

position.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Position from '../../src/view/position';
  6. import Node from '../../src/view/node';
  7. import Element from '../../src/view/element';
  8. import DocumentFragment from '../../src/view/documentfragment';
  9. import EditableElement from '../../src/view/editableelement';
  10. import Document from '../../src/view/document';
  11. import Text from '../../src/view/text';
  12. import TextProxy from '../../src/view/textproxy';
  13. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  14. import { parse } from '../../src/dev-utils/view';
  15. describe( 'Position', () => {
  16. const parentMock = {};
  17. describe( 'constructor()', () => {
  18. it( 'should create element without attributes', () => {
  19. const elem = new Position( parentMock, 5 );
  20. expect( elem ).to.have.property( 'parent' ).that.equals( parentMock );
  21. expect( elem ).to.have.property( 'offset' ).that.equals( 5 );
  22. } );
  23. } );
  24. describe( 'nodeBefore', () => {
  25. it( 'should equal to node that is before position', () => {
  26. const b1 = new Element( 'b' );
  27. const el = new Element( 'p', null, [ b1 ] );
  28. const position = new Position( el, 1 );
  29. expect( position.nodeBefore ).to.equal( b1 );
  30. } );
  31. it( 'should equal null if there is no node before', () => {
  32. const b1 = new Element( 'b' );
  33. const el = new Element( 'p', null, [ b1 ] );
  34. const position = new Position( el, 0 );
  35. expect( position.nodeBefore ).to.be.null;
  36. } );
  37. it( 'should equal null if position is located inside text node', () => {
  38. const text = new Text( 'foobar' );
  39. const position = new Position( text, 3 );
  40. expect( position.nodeBefore ).to.be.null;
  41. } );
  42. } );
  43. describe( 'nodeAfter', () => {
  44. it( 'should equal to node that is after position', () => {
  45. const b1 = new Element( 'b' );
  46. const el = new Element( 'p', null, [ b1 ] );
  47. const position = new Position( el, 0 );
  48. expect( position.nodeAfter ).to.equal( b1 );
  49. } );
  50. it( 'should equal null if there is no node before', () => {
  51. const b1 = new Element( 'b' );
  52. const el = new Element( 'p', null, [ b1 ] );
  53. const position = new Position( el, 1 );
  54. expect( position.nodeAfter ).to.be.null;
  55. } );
  56. it( 'should equal null if position is located inside text node', () => {
  57. const text = new Text( 'foobar' );
  58. const position = new Position( text, 3 );
  59. expect( position.nodeAfter ).to.be.null;
  60. } );
  61. } );
  62. describe( 'getShiftedBy', () => {
  63. it( 'returns new instance with shifted offset', () => {
  64. const position = new Position( parentMock, 10 );
  65. const shifted = position.getShiftedBy( 12 );
  66. expect( shifted.offset ).to.equal( 22 );
  67. } );
  68. it( 'accepts negative values', () => {
  69. const position = new Position( parentMock, 10 );
  70. const shifted = position.getShiftedBy( -5 );
  71. expect( shifted.offset ).to.equal( 5 );
  72. } );
  73. it( 'prevents offset to be a negative value', () => {
  74. const position = new Position( parentMock, 10 );
  75. const shifted = position.getShiftedBy( -20 );
  76. expect( shifted.offset ).to.equal( 0 );
  77. } );
  78. } );
  79. describe( 'getRoot', () => {
  80. it( 'should return it\'s parent root', () => {
  81. const foo = new Text( 'foo' );
  82. const docFrag = new DocumentFragment( foo );
  83. expect( new Position( foo, 1 ).root ).to.equal( docFrag );
  84. const bar = new Text( 'bar' );
  85. const p = new Element( 'p', null, bar );
  86. expect( new Position( bar, 2 ).root ).to.equal( p );
  87. expect( new Position( p, 0 ).root ).to.equal( p );
  88. } );
  89. } );
  90. describe( 'getAncestors', () => {
  91. it( 'should return it\'s parent and all it\'s ancestors', () => {
  92. const foo = new Text( 'foo' );
  93. const p = new Element( 'p', null, foo );
  94. const div = new Element( 'div', null, p );
  95. const docFrag = new DocumentFragment( div );
  96. expect( new Position( foo, 1 ).getAncestors() ).to.deep.equal( [ foo, p, div, docFrag ] );
  97. } );
  98. it( 'should return DocumentFragment if position is directly in document fragment', () => {
  99. const docFrag = new DocumentFragment();
  100. expect( new Position( docFrag, 0 ).getAncestors() ).to.deep.equal( [ docFrag ] );
  101. } );
  102. } );
  103. describe( 'createAt', () => {
  104. it( 'should create positions from positions', () => {
  105. const spy = sinon.spy( Position, 'createFromPosition' );
  106. const p = new Element( 'p' );
  107. const position = new Position( p, 0 );
  108. const created = Position.createAt( position );
  109. expect( created.isEqual( position ) ).to.be.true;
  110. expect( spy.calledOnce ).to.be.true;
  111. } );
  112. it( 'should create positions from node and offset', () => {
  113. const foo = new Text( 'foo' );
  114. const p = new Element( 'p', null, foo );
  115. expect( Position.createAt( foo ).parent ).to.equal( foo );
  116. expect( Position.createAt( foo ).offset ).to.equal( 0 );
  117. expect( Position.createAt( foo, 2 ).parent ).to.equal( foo );
  118. expect( Position.createAt( foo, 2 ).offset ).to.equal( 2 );
  119. expect( Position.createAt( p, 1 ).parent ).to.equal( p );
  120. expect( Position.createAt( p, 1 ).offset ).to.equal( 1 );
  121. } );
  122. it( 'should create positions from node and flag', () => {
  123. const foo = new Text( 'foo' );
  124. const p = new Element( 'p', null, foo );
  125. const fooEnd = Position.createAt( foo, 'end' );
  126. const fooBefore = Position.createAt( foo, 'before' );
  127. const fooAfter = Position.createAt( foo, 'after' );
  128. const pEnd = Position.createAt( p, 'end' );
  129. // pBefore and pAfter would throw.
  130. expect( fooEnd.parent ).to.equal( foo );
  131. expect( fooEnd.offset ).to.equal( 3 );
  132. expect( fooBefore.parent ).to.equal( p );
  133. expect( fooBefore.offset ).to.equal( 0 );
  134. expect( fooAfter.parent ).to.equal( p );
  135. expect( fooAfter.offset ).to.equal( 1 );
  136. expect( pEnd.parent ).to.equal( p );
  137. expect( pEnd.offset ).to.equal( 1 );
  138. } );
  139. it( 'should create positions in document fragment', () => {
  140. const foo = new Text( 'foo' );
  141. const docFrag = new DocumentFragment( [ foo ] );
  142. const pStart = Position.createAt( docFrag, 0 );
  143. const pEnd = Position.createAt( docFrag, 'end' );
  144. expect( pStart.parent ).to.equal( docFrag );
  145. expect( pStart.offset ).to.equal( 0 );
  146. expect( pEnd.parent ).to.equal( docFrag );
  147. expect( pEnd.offset ).to.equal( 1 );
  148. } );
  149. } );
  150. describe( 'createFromPosition', () => {
  151. it( 'creates new Position with same parent and offset', () => {
  152. const offset = 50;
  153. const position = new Position( parentMock, offset );
  154. const newPosition = Position.createFromPosition( position );
  155. expect( position ).to.not.equal( newPosition );
  156. expect( position.offset ).to.equal( offset );
  157. expect( position.parent ).to.equal( parentMock );
  158. } );
  159. } );
  160. describe( 'isEqual', () => {
  161. it( 'should return true for same object', () => {
  162. const position = new Position( {}, 12 );
  163. expect( position.isEqual( position ) ).to.be.true;
  164. } );
  165. it( 'should return true for positions with same parent and offset', () => {
  166. const parentMock = {};
  167. const position1 = new Position( parentMock, 12 );
  168. const position2 = new Position( parentMock, 12 );
  169. expect( position1.isEqual( position2 ) ).to.be.true;
  170. } );
  171. it( 'should return false for positions with different parents', () => {
  172. const position1 = new Position( {}, 12 );
  173. const position2 = new Position( {}, 12 );
  174. expect( position1.isEqual( position2 ) ).to.be.false;
  175. } );
  176. it( 'should return false for positions with different positions', () => {
  177. const parentMock = {};
  178. const position1 = new Position( parentMock, 12 );
  179. const position2 = new Position( parentMock, 2 );
  180. expect( position1.isEqual( position2 ) ).to.be.false;
  181. } );
  182. } );
  183. describe( 'isBefore', () => {
  184. it( 'should return false for same positions', () => {
  185. const node = new Node();
  186. const position1 = new Position( node, 10 );
  187. const position2 = new Position( node, 10 );
  188. expect( position1.isBefore( position1 ) ).to.be.false;
  189. expect( position1.isBefore( position2 ) ).to.be.false;
  190. expect( position2.isBefore( position1 ) ).to.be.false;
  191. } );
  192. it( 'should return false if no common ancestor is found', () => {
  193. const t1 = new Text( 'foo' );
  194. const t2 = new Text( 'bar' );
  195. const e1 = new Element( 'p', null, [ t1 ] );
  196. const e2 = new Element( 'p', null, [ t2 ] );
  197. const position1 = new Position( e1, 0 );
  198. const position2 = new Position( e2, 1 );
  199. expect( position1.isBefore( position2 ) );
  200. expect( position2.isBefore( position1 ) );
  201. } );
  202. it( 'should return true if position is before in same node', () => {
  203. const node = new Node();
  204. const p1 = new Position( node, 10 );
  205. const p2 = new Position( node, 5 );
  206. expect( p2.isBefore( p1 ) ).to.be.true;
  207. expect( p1.isBefore( p2 ) ).to.be.false;
  208. } );
  209. it( 'should compare positions that have common parent', () => {
  210. const t1 = new Text( 'foo' );
  211. const t2 = new Text( 'bar' );
  212. const root = new Element( 'p', null, [ t1, t2 ] );
  213. const position1 = new Position( t1, 2 );
  214. const position2 = new Position( t2, 0 );
  215. const position3 = new Position( root, 0 );
  216. const position4 = new Position( root, 2 );
  217. const position5 = new Position( t1, 0 );
  218. const position6 = new Position( root, 1 );
  219. expect( position1.isBefore( position2 ) ).to.be.true;
  220. expect( position2.isBefore( position1 ) ).to.be.false;
  221. expect( position3.isBefore( position1 ) ).to.be.true;
  222. expect( position3.isBefore( position2 ) ).to.be.true;
  223. expect( position1.isBefore( position3 ) ).to.be.false;
  224. expect( position2.isBefore( position3 ) ).to.be.false;
  225. expect( position4.isBefore( position1 ) ).to.be.false;
  226. expect( position4.isBefore( position3 ) ).to.be.false;
  227. expect( position3.isBefore( position4 ) ).to.be.true;
  228. expect( position3.isBefore( position5 ) ).to.be.true;
  229. expect( position6.isBefore( position2 ) ).to.be.true;
  230. expect( position1.isBefore( position6 ) ).to.be.true;
  231. } );
  232. } );
  233. describe( 'isAfter', () => {
  234. it( 'should return false for same positions', () => {
  235. const node = new Node();
  236. const position1 = new Position( node, 10 );
  237. const position2 = new Position( node, 10 );
  238. expect( position1.isAfter( position1 ) ).to.be.false;
  239. expect( position1.isAfter( position2 ) ).to.be.false;
  240. expect( position2.isAfter( position1 ) ).to.be.false;
  241. } );
  242. it( 'should return false if no common ancestor is found', () => {
  243. const t1 = new Text( 'foo' );
  244. const t2 = new Text( 'bar' );
  245. const e1 = new Element( 'p', null, [ t1 ] );
  246. const e2 = new Element( 'p', null, [ t2 ] );
  247. const position1 = new Position( e1, 0 );
  248. const position2 = new Position( e2, 1 );
  249. expect( position1.isAfter( position2 ) );
  250. expect( position2.isAfter( position1 ) );
  251. } );
  252. it( 'should return true if position is after in same node', () => {
  253. const node = new Node();
  254. const p1 = new Position( node, 10 );
  255. const p2 = new Position( node, 5 );
  256. expect( p2.isAfter( p1 ) ).to.be.false;
  257. expect( p1.isAfter( p2 ) ).to.be.true;
  258. } );
  259. it( 'should compare positions that have common parent', () => {
  260. const t1 = new Text( 'foo' );
  261. const t2 = new Text( 'bar' );
  262. const root = new Element( 'p', null, [ t1, t2 ] );
  263. const position1 = new Position( t1, 2 );
  264. const position2 = new Position( t2, 0 );
  265. const position3 = new Position( root, 0 );
  266. const position4 = new Position( root, 2 );
  267. const position5 = new Position( t1, 0 );
  268. const position6 = new Position( root, 1 );
  269. expect( position1.isAfter( position2 ) ).to.be.false;
  270. expect( position2.isAfter( position1 ) ).to.be.true;
  271. expect( position3.isAfter( position1 ) ).to.be.false;
  272. expect( position3.isAfter( position2 ) ).to.be.false;
  273. expect( position1.isAfter( position3 ) ).to.be.true;
  274. expect( position2.isAfter( position3 ) ).to.be.true;
  275. expect( position4.isAfter( position1 ) ).to.be.true;
  276. expect( position4.isAfter( position3 ) ).to.be.true;
  277. expect( position3.isAfter( position4 ) ).to.be.false;
  278. expect( position5.isAfter( position3 ) ).to.be.true;
  279. expect( position2.isAfter( position6 ) ).to.be.true;
  280. } );
  281. } );
  282. describe( 'isAtStart', () => {
  283. it( 'should return true if it is at the start of it\'s parent', () => {
  284. const foo = new Text( 'foo' );
  285. const position = new Position( foo, 0 );
  286. expect( position.isAtStart ).to.be.true;
  287. } );
  288. it( 'should return false if it is not at the start of it\'s parent', () => {
  289. const foo = new Text( 'foo' );
  290. const position = new Position( foo, 1 );
  291. expect( position.isAtStart ).to.be.false;
  292. } );
  293. } );
  294. describe( 'isAtEnd', () => {
  295. it( 'should return true if it is at the end of it\'s parent', () => {
  296. const foo = new Text( 'foo' );
  297. const p = new Element( 'p', null, foo );
  298. expect( new Position( foo, 3 ).isAtEnd ).to.be.true;
  299. expect( new Position( p, 1 ).isAtEnd ).to.be.true;
  300. } );
  301. it( 'should return false if it is not at the end of it\'s parent', () => {
  302. const foo = new Text( 'foo' );
  303. const p = new Element( 'p', null, foo );
  304. expect( new Position( foo, 2 ).isAtEnd ).to.be.false;
  305. expect( new Position( p, 0 ).isAtEnd ).to.be.false;
  306. } );
  307. } );
  308. describe( 'compareWith', () => {
  309. it( 'should return same if positions are same', () => {
  310. const root = new Node();
  311. const position = new Position( root, 0 );
  312. const compared = new Position( root, 0 );
  313. expect( position.compareWith( compared ) ).to.equal( 'same' );
  314. } );
  315. it( 'should return before if the position is before compared one', () => {
  316. const root = new Node();
  317. const position = new Position( root, 0 );
  318. const compared = new Position( root, 1 );
  319. expect( position.compareWith( compared ) ).to.equal( 'before' );
  320. } );
  321. it( 'should return after if the position is after compared one', () => {
  322. const root = new Node();
  323. const position = new Position( root, 4 );
  324. const compared = new Position( root, 1 );
  325. expect( position.compareWith( compared ) ).to.equal( 'after' );
  326. } );
  327. it( 'should return different if positions are in different roots', () => {
  328. const root1 = new Node();
  329. const root2 = new Node();
  330. const position = new Position( root1, 4 );
  331. const compared = new Position( root2, 1 );
  332. expect( position.compareWith( compared ) ).to.equal( 'different' );
  333. } );
  334. it( 'should return correct results if position is in document fragment', () => {
  335. const node = new Node( 'name' );
  336. const docFrag = new DocumentFragment( [ node ] );
  337. const position = new Position( docFrag, 0 );
  338. const compared = new Position( docFrag, 1 );
  339. const posInNode = new Position( node, 0 );
  340. expect( position.compareWith( compared ) ).to.equal( 'before' );
  341. expect( compared.compareWith( position ) ).to.equal( 'after' );
  342. expect( compared.compareWith( compared ) ).to.equal( 'same' );
  343. expect( position.compareWith( posInNode ) ).to.equal( 'before' );
  344. expect( compared.compareWith( posInNode ) ).to.equal( 'after' );
  345. } );
  346. } );
  347. describe( 'createBefore', () => {
  348. it( 'should throw error if one try to create positions before root', () => {
  349. expect( () => {
  350. Position.createBefore( parse( '<p></p>' ) );
  351. } ).to.throw( CKEditorError, /view-position-before-root/ );
  352. } );
  353. it( 'should create positions before `Node`', () => {
  354. const { selection } = parse( '<p>[]<b></b></p>' );
  355. const position = selection.getFirstPosition();
  356. const nodeAfter = position.nodeAfter;
  357. expect( Position.createBefore( nodeAfter ).isEqual( position ) ).to.be.true;
  358. } );
  359. it( 'should create positions before `TextProxy`', () => {
  360. const text = new Text( 'abc' );
  361. const textProxy = new TextProxy( text, 1, 1 );
  362. const position = new Position( text, 1 );
  363. expect( Position.createBefore( textProxy ) ).deep.equal( position );
  364. } );
  365. } );
  366. describe( 'createAfter', () => {
  367. it( 'should throw error if one try to create positions after root', () => {
  368. expect( () => {
  369. Position.createAfter( parse( '<p></p>' ) );
  370. } ).to.throw( CKEditorError, /view-position-after-root/ );
  371. } );
  372. it( 'should create positions after `Node`', () => {
  373. const { selection } = parse( '<p><b></b>[]</p>' );
  374. const position = selection.getFirstPosition();
  375. const nodeBefore = position.nodeBefore;
  376. expect( Position.createAfter( nodeBefore ).isEqual( position ) ).to.be.true;
  377. } );
  378. it( 'should create positions after `TextProxy`', () => {
  379. const text = new Text( 'abcd' );
  380. const textProxy = new TextProxy( text, 1, 2 );
  381. const position = new Position( text, 3 );
  382. expect( Position.createAfter( textProxy ) ).deep.equal( position );
  383. } );
  384. } );
  385. describe( 'getEditableElement', () => {
  386. it( 'should return null if position is not inside EditableElement', () => {
  387. const position = new Position( new Element( 'p' ), 0 );
  388. expect( position.editableElement ).to.be.null;
  389. } );
  390. it( 'should return EditableElement when position is placed inside', () => {
  391. const document = new Document();
  392. const p = new Element( 'p' );
  393. const editable = new EditableElement( 'div', null, p );
  394. editable.document = document;
  395. const position = new Position( p, 0 );
  396. expect( position.editableElement ).to.equal( editable );
  397. document.destroy();
  398. } );
  399. } );
  400. } );