position.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /**
  2. * @license Copyright (c) 2003-2018, 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, stringify } 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( 'getLastMatchingPosition', () => {
  80. it( 'should skip forward', () => {
  81. const { view, selection } = parse( '<p><b>{}foo</b></p>' );
  82. let position = selection.getFirstPosition();
  83. position = position.getLastMatchingPosition( value => value.type == 'text' );
  84. expect( stringify( view, position ) ).to.equal( '<p><b>foo[]</b></p>' );
  85. } );
  86. it( 'should skip backward', () => {
  87. const { view, selection } = parse( '<p><b>foo{}</b></p>' );
  88. let position = selection.getFirstPosition();
  89. position = position.getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } );
  90. expect( stringify( view, position ) ).to.equal( '<p><b>[]foo</b></p>' );
  91. } );
  92. } );
  93. describe( 'getRoot', () => {
  94. it( 'should return it\'s parent root', () => {
  95. const foo = new Text( 'foo' );
  96. const docFrag = new DocumentFragment( foo );
  97. expect( new Position( foo, 1 ).root ).to.equal( docFrag );
  98. const bar = new Text( 'bar' );
  99. const p = new Element( 'p', null, bar );
  100. expect( new Position( bar, 2 ).root ).to.equal( p );
  101. expect( new Position( p, 0 ).root ).to.equal( p );
  102. } );
  103. } );
  104. describe( 'getAncestors', () => {
  105. it( 'should return it\'s parent and all it\'s ancestors', () => {
  106. const foo = new Text( 'foo' );
  107. const p = new Element( 'p', null, foo );
  108. const div = new Element( 'div', null, p );
  109. const docFrag = new DocumentFragment( div );
  110. expect( new Position( foo, 1 ).getAncestors() ).to.deep.equal( [ docFrag, div, p, foo ] );
  111. } );
  112. it( 'should return DocumentFragment if position is directly in document fragment', () => {
  113. const docFrag = new DocumentFragment();
  114. expect( new Position( docFrag, 0 ).getAncestors() ).to.deep.equal( [ docFrag ] );
  115. } );
  116. } );
  117. describe( 'createAt', () => {
  118. it( 'should create positions from positions', () => {
  119. const spy = sinon.spy( Position, 'createFromPosition' );
  120. const p = new Element( 'p' );
  121. const position = new Position( p, 0 );
  122. const created = Position.createAt( position );
  123. expect( created.isEqual( position ) ).to.be.true;
  124. expect( spy.calledOnce ).to.be.true;
  125. } );
  126. it( 'should create positions from node and offset', () => {
  127. const foo = new Text( 'foo' );
  128. const p = new Element( 'p', null, foo );
  129. expect( Position.createAt( foo ).parent ).to.equal( foo );
  130. expect( Position.createAt( foo ).offset ).to.equal( 0 );
  131. expect( Position.createAt( foo, 2 ).parent ).to.equal( foo );
  132. expect( Position.createAt( foo, 2 ).offset ).to.equal( 2 );
  133. expect( Position.createAt( p, 1 ).parent ).to.equal( p );
  134. expect( Position.createAt( p, 1 ).offset ).to.equal( 1 );
  135. } );
  136. it( 'should create positions from node and flag', () => {
  137. const foo = new Text( 'foo' );
  138. const p = new Element( 'p', null, foo );
  139. const fooEnd = Position.createAt( foo, 'end' );
  140. const fooBefore = Position.createAt( foo, 'before' );
  141. const fooAfter = Position.createAt( foo, 'after' );
  142. const pEnd = Position.createAt( p, 'end' );
  143. // pBefore and pAfter would throw.
  144. expect( fooEnd.parent ).to.equal( foo );
  145. expect( fooEnd.offset ).to.equal( 3 );
  146. expect( fooBefore.parent ).to.equal( p );
  147. expect( fooBefore.offset ).to.equal( 0 );
  148. expect( fooAfter.parent ).to.equal( p );
  149. expect( fooAfter.offset ).to.equal( 1 );
  150. expect( pEnd.parent ).to.equal( p );
  151. expect( pEnd.offset ).to.equal( 1 );
  152. } );
  153. it( 'should create positions in document fragment', () => {
  154. const foo = new Text( 'foo' );
  155. const docFrag = new DocumentFragment( [ foo ] );
  156. const pStart = Position.createAt( docFrag, 0 );
  157. const pEnd = Position.createAt( docFrag, 'end' );
  158. expect( pStart.parent ).to.equal( docFrag );
  159. expect( pStart.offset ).to.equal( 0 );
  160. expect( pEnd.parent ).to.equal( docFrag );
  161. expect( pEnd.offset ).to.equal( 1 );
  162. } );
  163. } );
  164. describe( 'createFromPosition', () => {
  165. it( 'creates new Position with same parent and offset', () => {
  166. const offset = 50;
  167. const position = new Position( parentMock, offset );
  168. const newPosition = Position.createFromPosition( position );
  169. expect( position ).to.not.equal( newPosition );
  170. expect( position.offset ).to.equal( offset );
  171. expect( position.parent ).to.equal( parentMock );
  172. } );
  173. } );
  174. describe( 'isEqual', () => {
  175. it( 'should return true for same object', () => {
  176. const position = new Position( {}, 12 );
  177. expect( position.isEqual( position ) ).to.be.true;
  178. } );
  179. it( 'should return true for positions with same parent and offset', () => {
  180. const parentMock = {};
  181. const position1 = new Position( parentMock, 12 );
  182. const position2 = new Position( parentMock, 12 );
  183. expect( position1.isEqual( position2 ) ).to.be.true;
  184. } );
  185. it( 'should return false for positions with different parents', () => {
  186. const position1 = new Position( {}, 12 );
  187. const position2 = new Position( {}, 12 );
  188. expect( position1.isEqual( position2 ) ).to.be.false;
  189. } );
  190. it( 'should return false for positions with different positions', () => {
  191. const parentMock = {};
  192. const position1 = new Position( parentMock, 12 );
  193. const position2 = new Position( parentMock, 2 );
  194. expect( position1.isEqual( position2 ) ).to.be.false;
  195. } );
  196. } );
  197. describe( 'isBefore', () => {
  198. it( 'should return false for same positions', () => {
  199. const node = new Node();
  200. const position1 = new Position( node, 10 );
  201. const position2 = new Position( node, 10 );
  202. expect( position1.isBefore( position1 ) ).to.be.false;
  203. expect( position1.isBefore( position2 ) ).to.be.false;
  204. expect( position2.isBefore( position1 ) ).to.be.false;
  205. } );
  206. it( 'should return false if no common ancestor is found', () => {
  207. const t1 = new Text( 'foo' );
  208. const t2 = new Text( 'bar' );
  209. const e1 = new Element( 'p', null, [ t1 ] );
  210. const e2 = new Element( 'p', null, [ t2 ] );
  211. const position1 = new Position( e1, 0 );
  212. const position2 = new Position( e2, 1 );
  213. expect( position1.isBefore( position2 ) );
  214. expect( position2.isBefore( position1 ) );
  215. } );
  216. it( 'should return true if position is before in same node', () => {
  217. const node = new Node();
  218. const p1 = new Position( node, 10 );
  219. const p2 = new Position( node, 5 );
  220. expect( p2.isBefore( p1 ) ).to.be.true;
  221. expect( p1.isBefore( p2 ) ).to.be.false;
  222. } );
  223. it( 'should compare positions that have common parent', () => {
  224. const t1 = new Text( 'foo' );
  225. const t2 = new Text( 'bar' );
  226. const root = new Element( 'p', null, [ t1, t2 ] );
  227. const position1 = new Position( t1, 2 );
  228. const position2 = new Position( t2, 0 );
  229. const position3 = new Position( root, 0 );
  230. const position4 = new Position( root, 2 );
  231. const position5 = new Position( t1, 0 );
  232. const position6 = new Position( root, 1 );
  233. expect( position1.isBefore( position2 ) ).to.be.true;
  234. expect( position2.isBefore( position1 ) ).to.be.false;
  235. expect( position3.isBefore( position1 ) ).to.be.true;
  236. expect( position3.isBefore( position2 ) ).to.be.true;
  237. expect( position1.isBefore( position3 ) ).to.be.false;
  238. expect( position2.isBefore( position3 ) ).to.be.false;
  239. expect( position4.isBefore( position1 ) ).to.be.false;
  240. expect( position4.isBefore( position3 ) ).to.be.false;
  241. expect( position3.isBefore( position4 ) ).to.be.true;
  242. expect( position3.isBefore( position5 ) ).to.be.true;
  243. expect( position6.isBefore( position2 ) ).to.be.true;
  244. expect( position1.isBefore( position6 ) ).to.be.true;
  245. } );
  246. } );
  247. describe( 'isAfter', () => {
  248. it( 'should return false for same positions', () => {
  249. const node = new Node();
  250. const position1 = new Position( node, 10 );
  251. const position2 = new Position( node, 10 );
  252. expect( position1.isAfter( position1 ) ).to.be.false;
  253. expect( position1.isAfter( position2 ) ).to.be.false;
  254. expect( position2.isAfter( position1 ) ).to.be.false;
  255. } );
  256. it( 'should return false if no common ancestor is found', () => {
  257. const t1 = new Text( 'foo' );
  258. const t2 = new Text( 'bar' );
  259. const e1 = new Element( 'p', null, [ t1 ] );
  260. const e2 = new Element( 'p', null, [ t2 ] );
  261. const position1 = new Position( e1, 0 );
  262. const position2 = new Position( e2, 1 );
  263. expect( position1.isAfter( position2 ) );
  264. expect( position2.isAfter( position1 ) );
  265. } );
  266. it( 'should return true if position is after in same node', () => {
  267. const node = new Node();
  268. const p1 = new Position( node, 10 );
  269. const p2 = new Position( node, 5 );
  270. expect( p2.isAfter( p1 ) ).to.be.false;
  271. expect( p1.isAfter( p2 ) ).to.be.true;
  272. } );
  273. it( 'should compare positions that have common parent', () => {
  274. const t1 = new Text( 'foo' );
  275. const t2 = new Text( 'bar' );
  276. const root = new Element( 'p', null, [ t1, t2 ] );
  277. const position1 = new Position( t1, 2 );
  278. const position2 = new Position( t2, 0 );
  279. const position3 = new Position( root, 0 );
  280. const position4 = new Position( root, 2 );
  281. const position5 = new Position( t1, 0 );
  282. const position6 = new Position( root, 1 );
  283. expect( position1.isAfter( position2 ) ).to.be.false;
  284. expect( position2.isAfter( position1 ) ).to.be.true;
  285. expect( position3.isAfter( position1 ) ).to.be.false;
  286. expect( position3.isAfter( position2 ) ).to.be.false;
  287. expect( position1.isAfter( position3 ) ).to.be.true;
  288. expect( position2.isAfter( position3 ) ).to.be.true;
  289. expect( position4.isAfter( position1 ) ).to.be.true;
  290. expect( position4.isAfter( position3 ) ).to.be.true;
  291. expect( position3.isAfter( position4 ) ).to.be.false;
  292. expect( position5.isAfter( position3 ) ).to.be.true;
  293. expect( position2.isAfter( position6 ) ).to.be.true;
  294. } );
  295. } );
  296. describe( 'isAtStart', () => {
  297. it( 'should return true if it is at the start of it\'s parent', () => {
  298. const foo = new Text( 'foo' );
  299. const position = new Position( foo, 0 );
  300. expect( position.isAtStart ).to.be.true;
  301. } );
  302. it( 'should return false if it is not at the start of it\'s parent', () => {
  303. const foo = new Text( 'foo' );
  304. const position = new Position( foo, 1 );
  305. expect( position.isAtStart ).to.be.false;
  306. } );
  307. } );
  308. describe( 'isAtEnd', () => {
  309. it( 'should return true if it is at the end of it\'s parent', () => {
  310. const foo = new Text( 'foo' );
  311. const p = new Element( 'p', null, foo );
  312. expect( new Position( foo, 3 ).isAtEnd ).to.be.true;
  313. expect( new Position( p, 1 ).isAtEnd ).to.be.true;
  314. } );
  315. it( 'should return false if it is not at the end of it\'s parent', () => {
  316. const foo = new Text( 'foo' );
  317. const p = new Element( 'p', null, foo );
  318. expect( new Position( foo, 2 ).isAtEnd ).to.be.false;
  319. expect( new Position( p, 0 ).isAtEnd ).to.be.false;
  320. } );
  321. } );
  322. describe( 'compareWith', () => {
  323. it( 'should return same if positions are same', () => {
  324. const root = new Element();
  325. const position = new Position( root, 0 );
  326. const compared = new Position( root, 0 );
  327. expect( position.compareWith( compared ) ).to.equal( 'same' );
  328. } );
  329. it( 'should return before if the position is before compared one', () => {
  330. const root = new Element();
  331. const position = new Position( root, 0 );
  332. const compared = new Position( root, 1 );
  333. expect( position.compareWith( compared ) ).to.equal( 'before' );
  334. } );
  335. it( 'should return after if the position is after compared one', () => {
  336. const root = new Element();
  337. const position = new Position( root, 4 );
  338. const compared = new Position( root, 1 );
  339. expect( position.compareWith( compared ) ).to.equal( 'after' );
  340. } );
  341. it( 'should return different if positions are in different roots', () => {
  342. const root1 = new Element();
  343. const root2 = new Element();
  344. const position = new Position( root1, 4 );
  345. const compared = new Position( root2, 1 );
  346. expect( position.compareWith( compared ) ).to.equal( 'different' );
  347. } );
  348. it( 'should return correct results if position is in document fragment', () => {
  349. const node = new Element( 'name' );
  350. const docFrag = new DocumentFragment( [ node ] );
  351. const position = new Position( docFrag, 0 );
  352. const compared = new Position( docFrag, 1 );
  353. const posInNode = new Position( node, 0 );
  354. expect( position.compareWith( compared ) ).to.equal( 'before' );
  355. expect( compared.compareWith( position ) ).to.equal( 'after' );
  356. expect( compared.compareWith( compared ) ).to.equal( 'same' );
  357. expect( position.compareWith( posInNode ) ).to.equal( 'before' );
  358. expect( compared.compareWith( posInNode ) ).to.equal( 'after' );
  359. } );
  360. } );
  361. describe( 'createBefore', () => {
  362. it( 'should throw error if one try to create positions before root', () => {
  363. expect( () => {
  364. Position.createBefore( parse( '<p></p>' ) );
  365. } ).to.throw( CKEditorError, /view-position-before-root/ );
  366. } );
  367. it( 'should create positions before `Node`', () => {
  368. const { selection } = parse( '<p>[]<b></b></p>' );
  369. const position = selection.getFirstPosition();
  370. const nodeAfter = position.nodeAfter;
  371. expect( Position.createBefore( nodeAfter ).isEqual( position ) ).to.be.true;
  372. } );
  373. it( 'should create positions before `TextProxy`', () => {
  374. const text = new Text( 'abc' );
  375. const textProxy = new TextProxy( text, 1, 1 );
  376. const position = new Position( text, 1 );
  377. expect( Position.createBefore( textProxy ) ).deep.equal( position );
  378. } );
  379. } );
  380. describe( 'createAfter', () => {
  381. it( 'should throw error if one try to create positions after root', () => {
  382. expect( () => {
  383. Position.createAfter( parse( '<p></p>' ) );
  384. } ).to.throw( CKEditorError, /view-position-after-root/ );
  385. } );
  386. it( 'should create positions after `Node`', () => {
  387. const { selection } = parse( '<p><b></b>[]</p>' );
  388. const position = selection.getFirstPosition();
  389. const nodeBefore = position.nodeBefore;
  390. expect( Position.createAfter( nodeBefore ).isEqual( position ) ).to.be.true;
  391. } );
  392. it( 'should create positions after `TextProxy`', () => {
  393. const text = new Text( 'abcd' );
  394. const textProxy = new TextProxy( text, 1, 2 );
  395. const position = new Position( text, 3 );
  396. expect( Position.createAfter( textProxy ) ).deep.equal( position );
  397. } );
  398. } );
  399. describe( 'getEditableElement', () => {
  400. it( 'should return null if position is not inside EditableElement', () => {
  401. const position = new Position( new Element( 'p' ), 0 );
  402. expect( position.editableElement ).to.be.null;
  403. } );
  404. it( 'should return EditableElement when position is placed inside', () => {
  405. const document = new Document();
  406. const p = new Element( 'p' );
  407. const editable = new EditableElement( 'div', null, p );
  408. editable._document = document;
  409. const position = new Position( p, 0 );
  410. expect( position.editableElement ).to.equal( editable );
  411. } );
  412. } );
  413. describe( 'getCommonAncestor()', () => {
  414. let div, ul, liUl1, liUl2, texts, section, article, ol, liOl1, liOl2, p;
  415. // |- div
  416. // |- ul
  417. // | |- li
  418. // | | |- foz
  419. // | |- li
  420. // | |- bar
  421. // |- section
  422. // |- Sed id libero at libero tristique
  423. // |- article
  424. // | |- ol
  425. // | | |- li
  426. // | | | |- Lorem ipsum dolor sit amet.
  427. // | | |- li
  428. // | | |- Mauris tincidunt tincidunt leo ac rutrum.
  429. // | |- p
  430. // | | |- Maecenas accumsan tellus.
  431. beforeEach( () => {
  432. texts = {
  433. foz: new Text( 'foz' ),
  434. bar: new Text( 'bar' ),
  435. lorem: new Text( 'Lorem ipsum dolor sit amet.' ),
  436. mauris: new Text( 'Mauris tincidunt tincidunt leo ac rutrum.' ),
  437. maecenas: new Text( 'Maecenas accumsan tellus.' ),
  438. sed: new Text( 'Sed id libero at libero tristique.' )
  439. };
  440. liUl1 = new Element( 'li', null, texts.foz );
  441. liUl2 = new Element( 'li', null, texts.bar );
  442. ul = new Element( 'ul', null, [ liUl1, liUl2 ] );
  443. liOl1 = new Element( 'li', null, texts.lorem );
  444. liOl2 = new Element( 'li', null, texts.mauris );
  445. ol = new Element( 'ol', null, [ liOl1, liOl2 ] );
  446. p = new Element( 'p', null, texts.maecenas );
  447. article = new Element( 'article', null, [ ol, p ] );
  448. section = new Element( 'section', null, [ texts.sed, article ] );
  449. div = new Element( 'div', null, [ ul, section ] );
  450. } );
  451. it( 'for two the same positions returns the parent element', () => {
  452. const afterLoremPosition = new Position( liOl1, 5 );
  453. const otherPosition = Position.createFromPosition( afterLoremPosition );
  454. test( afterLoremPosition, otherPosition, liOl1 );
  455. } );
  456. it( 'for two positions in the same element returns the element', () => {
  457. const startMaecenasPosition = Position.createAt( liOl2 );
  458. const beforeTellusPosition = new Position( liOl2, 18 );
  459. test( startMaecenasPosition, beforeTellusPosition, liOl2 );
  460. } );
  461. it( 'works when one of the positions is nested deeper than the other #1', () => {
  462. const firstPosition = new Position( liUl1, 1 );
  463. const secondPosition = new Position( p, 3 );
  464. test( firstPosition, secondPosition, div );
  465. } );
  466. it( 'works when one of the positions is nested deeper than the other #2', () => {
  467. const firstPosition = new Position( liOl2, 10 );
  468. const secondPosition = new Position( section, 1 );
  469. test( firstPosition, secondPosition, section );
  470. } );
  471. it( 'for two positions in different trees returns null', () => {
  472. const div = new Element( 'div' );
  473. const posInDiv = new Position( div, 0 );
  474. const firstPosition = new Position( liOl2, 10 );
  475. test( posInDiv, firstPosition, null );
  476. } );
  477. function test( positionA, positionB, lca ) {
  478. expect( positionA.getCommonAncestor( positionB ) ).to.equal( lca );
  479. expect( positionB.getCommonAncestor( positionA ) ).to.equal( lca );
  480. }
  481. } );
  482. } );