8
0

position.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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, 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( 'getFurtherPosition', () => {
  80. it( 'should return skip', () => {
  81. const { view, selection } = parse( '<p><b>{}foo</b></p>' );
  82. let position = selection.getFirstPosition();
  83. position = position.getFurtherPosition( ( value ) => value.type == 'text' );
  84. expect( stringify( view, position ) ).to.equal( '<p><b>foo[]</b></p>' );
  85. } );
  86. } );
  87. describe( 'getPriorPosition', () => {
  88. it( 'should return skip', () => {
  89. const { view, selection } = parse( '<p><b>foo{}</b></p>' );
  90. let position = selection.getFirstPosition();
  91. position = position.getPriorPosition( ( value ) => value.type == 'text' );
  92. expect( stringify( view, position ) ).to.equal( '<p><b>[]foo</b></p>' );
  93. } );
  94. } );
  95. describe( 'getRoot', () => {
  96. it( 'should return it\'s parent root', () => {
  97. const foo = new Text( 'foo' );
  98. const docFrag = new DocumentFragment( foo );
  99. expect( new Position( foo, 1 ).root ).to.equal( docFrag );
  100. const bar = new Text( 'bar' );
  101. const p = new Element( 'p', null, bar );
  102. expect( new Position( bar, 2 ).root ).to.equal( p );
  103. expect( new Position( p, 0 ).root ).to.equal( p );
  104. } );
  105. } );
  106. describe( 'getAncestors', () => {
  107. it( 'should return it\'s parent and all it\'s ancestors', () => {
  108. const foo = new Text( 'foo' );
  109. const p = new Element( 'p', null, foo );
  110. const div = new Element( 'div', null, p );
  111. const docFrag = new DocumentFragment( div );
  112. expect( new Position( foo, 1 ).getAncestors() ).to.deep.equal( [ docFrag, div, p, foo ] );
  113. } );
  114. it( 'should return DocumentFragment if position is directly in document fragment', () => {
  115. const docFrag = new DocumentFragment();
  116. expect( new Position( docFrag, 0 ).getAncestors() ).to.deep.equal( [ docFrag ] );
  117. } );
  118. } );
  119. describe( 'createAt', () => {
  120. it( 'should create positions from positions', () => {
  121. const spy = sinon.spy( Position, 'createFromPosition' );
  122. const p = new Element( 'p' );
  123. const position = new Position( p, 0 );
  124. const created = Position.createAt( position );
  125. expect( created.isEqual( position ) ).to.be.true;
  126. expect( spy.calledOnce ).to.be.true;
  127. } );
  128. it( 'should create positions from node and offset', () => {
  129. const foo = new Text( 'foo' );
  130. const p = new Element( 'p', null, foo );
  131. expect( Position.createAt( foo ).parent ).to.equal( foo );
  132. expect( Position.createAt( foo ).offset ).to.equal( 0 );
  133. expect( Position.createAt( foo, 2 ).parent ).to.equal( foo );
  134. expect( Position.createAt( foo, 2 ).offset ).to.equal( 2 );
  135. expect( Position.createAt( p, 1 ).parent ).to.equal( p );
  136. expect( Position.createAt( p, 1 ).offset ).to.equal( 1 );
  137. } );
  138. it( 'should create positions from node and flag', () => {
  139. const foo = new Text( 'foo' );
  140. const p = new Element( 'p', null, foo );
  141. const fooEnd = Position.createAt( foo, 'end' );
  142. const fooBefore = Position.createAt( foo, 'before' );
  143. const fooAfter = Position.createAt( foo, 'after' );
  144. const pEnd = Position.createAt( p, 'end' );
  145. // pBefore and pAfter would throw.
  146. expect( fooEnd.parent ).to.equal( foo );
  147. expect( fooEnd.offset ).to.equal( 3 );
  148. expect( fooBefore.parent ).to.equal( p );
  149. expect( fooBefore.offset ).to.equal( 0 );
  150. expect( fooAfter.parent ).to.equal( p );
  151. expect( fooAfter.offset ).to.equal( 1 );
  152. expect( pEnd.parent ).to.equal( p );
  153. expect( pEnd.offset ).to.equal( 1 );
  154. } );
  155. it( 'should create positions in document fragment', () => {
  156. const foo = new Text( 'foo' );
  157. const docFrag = new DocumentFragment( [ foo ] );
  158. const pStart = Position.createAt( docFrag, 0 );
  159. const pEnd = Position.createAt( docFrag, 'end' );
  160. expect( pStart.parent ).to.equal( docFrag );
  161. expect( pStart.offset ).to.equal( 0 );
  162. expect( pEnd.parent ).to.equal( docFrag );
  163. expect( pEnd.offset ).to.equal( 1 );
  164. } );
  165. } );
  166. describe( 'createFromPosition', () => {
  167. it( 'creates new Position with same parent and offset', () => {
  168. const offset = 50;
  169. const position = new Position( parentMock, offset );
  170. const newPosition = Position.createFromPosition( position );
  171. expect( position ).to.not.equal( newPosition );
  172. expect( position.offset ).to.equal( offset );
  173. expect( position.parent ).to.equal( parentMock );
  174. } );
  175. } );
  176. describe( 'isEqual', () => {
  177. it( 'should return true for same object', () => {
  178. const position = new Position( {}, 12 );
  179. expect( position.isEqual( position ) ).to.be.true;
  180. } );
  181. it( 'should return true for positions with same parent and offset', () => {
  182. const parentMock = {};
  183. const position1 = new Position( parentMock, 12 );
  184. const position2 = new Position( parentMock, 12 );
  185. expect( position1.isEqual( position2 ) ).to.be.true;
  186. } );
  187. it( 'should return false for positions with different parents', () => {
  188. const position1 = new Position( {}, 12 );
  189. const position2 = new Position( {}, 12 );
  190. expect( position1.isEqual( position2 ) ).to.be.false;
  191. } );
  192. it( 'should return false for positions with different positions', () => {
  193. const parentMock = {};
  194. const position1 = new Position( parentMock, 12 );
  195. const position2 = new Position( parentMock, 2 );
  196. expect( position1.isEqual( position2 ) ).to.be.false;
  197. } );
  198. } );
  199. describe( 'isBefore', () => {
  200. it( 'should return false for same positions', () => {
  201. const node = new Node();
  202. const position1 = new Position( node, 10 );
  203. const position2 = new Position( node, 10 );
  204. expect( position1.isBefore( position1 ) ).to.be.false;
  205. expect( position1.isBefore( position2 ) ).to.be.false;
  206. expect( position2.isBefore( position1 ) ).to.be.false;
  207. } );
  208. it( 'should return false if no common ancestor is found', () => {
  209. const t1 = new Text( 'foo' );
  210. const t2 = new Text( 'bar' );
  211. const e1 = new Element( 'p', null, [ t1 ] );
  212. const e2 = new Element( 'p', null, [ t2 ] );
  213. const position1 = new Position( e1, 0 );
  214. const position2 = new Position( e2, 1 );
  215. expect( position1.isBefore( position2 ) );
  216. expect( position2.isBefore( position1 ) );
  217. } );
  218. it( 'should return true if position is before in same node', () => {
  219. const node = new Node();
  220. const p1 = new Position( node, 10 );
  221. const p2 = new Position( node, 5 );
  222. expect( p2.isBefore( p1 ) ).to.be.true;
  223. expect( p1.isBefore( p2 ) ).to.be.false;
  224. } );
  225. it( 'should compare positions that have common parent', () => {
  226. const t1 = new Text( 'foo' );
  227. const t2 = new Text( 'bar' );
  228. const root = new Element( 'p', null, [ t1, t2 ] );
  229. const position1 = new Position( t1, 2 );
  230. const position2 = new Position( t2, 0 );
  231. const position3 = new Position( root, 0 );
  232. const position4 = new Position( root, 2 );
  233. const position5 = new Position( t1, 0 );
  234. const position6 = new Position( root, 1 );
  235. expect( position1.isBefore( position2 ) ).to.be.true;
  236. expect( position2.isBefore( position1 ) ).to.be.false;
  237. expect( position3.isBefore( position1 ) ).to.be.true;
  238. expect( position3.isBefore( position2 ) ).to.be.true;
  239. expect( position1.isBefore( position3 ) ).to.be.false;
  240. expect( position2.isBefore( position3 ) ).to.be.false;
  241. expect( position4.isBefore( position1 ) ).to.be.false;
  242. expect( position4.isBefore( position3 ) ).to.be.false;
  243. expect( position3.isBefore( position4 ) ).to.be.true;
  244. expect( position3.isBefore( position5 ) ).to.be.true;
  245. expect( position6.isBefore( position2 ) ).to.be.true;
  246. expect( position1.isBefore( position6 ) ).to.be.true;
  247. } );
  248. } );
  249. describe( 'isAfter', () => {
  250. it( 'should return false for same positions', () => {
  251. const node = new Node();
  252. const position1 = new Position( node, 10 );
  253. const position2 = new Position( node, 10 );
  254. expect( position1.isAfter( position1 ) ).to.be.false;
  255. expect( position1.isAfter( position2 ) ).to.be.false;
  256. expect( position2.isAfter( position1 ) ).to.be.false;
  257. } );
  258. it( 'should return false if no common ancestor is found', () => {
  259. const t1 = new Text( 'foo' );
  260. const t2 = new Text( 'bar' );
  261. const e1 = new Element( 'p', null, [ t1 ] );
  262. const e2 = new Element( 'p', null, [ t2 ] );
  263. const position1 = new Position( e1, 0 );
  264. const position2 = new Position( e2, 1 );
  265. expect( position1.isAfter( position2 ) );
  266. expect( position2.isAfter( position1 ) );
  267. } );
  268. it( 'should return true if position is after in same node', () => {
  269. const node = new Node();
  270. const p1 = new Position( node, 10 );
  271. const p2 = new Position( node, 5 );
  272. expect( p2.isAfter( p1 ) ).to.be.false;
  273. expect( p1.isAfter( p2 ) ).to.be.true;
  274. } );
  275. it( 'should compare positions that have common parent', () => {
  276. const t1 = new Text( 'foo' );
  277. const t2 = new Text( 'bar' );
  278. const root = new Element( 'p', null, [ t1, t2 ] );
  279. const position1 = new Position( t1, 2 );
  280. const position2 = new Position( t2, 0 );
  281. const position3 = new Position( root, 0 );
  282. const position4 = new Position( root, 2 );
  283. const position5 = new Position( t1, 0 );
  284. const position6 = new Position( root, 1 );
  285. expect( position1.isAfter( position2 ) ).to.be.false;
  286. expect( position2.isAfter( position1 ) ).to.be.true;
  287. expect( position3.isAfter( position1 ) ).to.be.false;
  288. expect( position3.isAfter( position2 ) ).to.be.false;
  289. expect( position1.isAfter( position3 ) ).to.be.true;
  290. expect( position2.isAfter( position3 ) ).to.be.true;
  291. expect( position4.isAfter( position1 ) ).to.be.true;
  292. expect( position4.isAfter( position3 ) ).to.be.true;
  293. expect( position3.isAfter( position4 ) ).to.be.false;
  294. expect( position5.isAfter( position3 ) ).to.be.true;
  295. expect( position2.isAfter( position6 ) ).to.be.true;
  296. } );
  297. } );
  298. describe( 'isAtStart', () => {
  299. it( 'should return true if it is at the start of it\'s parent', () => {
  300. const foo = new Text( 'foo' );
  301. const position = new Position( foo, 0 );
  302. expect( position.isAtStart ).to.be.true;
  303. } );
  304. it( 'should return false if it is not at the start of it\'s parent', () => {
  305. const foo = new Text( 'foo' );
  306. const position = new Position( foo, 1 );
  307. expect( position.isAtStart ).to.be.false;
  308. } );
  309. } );
  310. describe( 'isAtEnd', () => {
  311. it( 'should return true if it is at the end of it\'s parent', () => {
  312. const foo = new Text( 'foo' );
  313. const p = new Element( 'p', null, foo );
  314. expect( new Position( foo, 3 ).isAtEnd ).to.be.true;
  315. expect( new Position( p, 1 ).isAtEnd ).to.be.true;
  316. } );
  317. it( 'should return false if it is not at the end of it\'s parent', () => {
  318. const foo = new Text( 'foo' );
  319. const p = new Element( 'p', null, foo );
  320. expect( new Position( foo, 2 ).isAtEnd ).to.be.false;
  321. expect( new Position( p, 0 ).isAtEnd ).to.be.false;
  322. } );
  323. } );
  324. describe( 'compareWith', () => {
  325. it( 'should return same if positions are same', () => {
  326. const root = new Node();
  327. const position = new Position( root, 0 );
  328. const compared = new Position( root, 0 );
  329. expect( position.compareWith( compared ) ).to.equal( 'same' );
  330. } );
  331. it( 'should return before if the position is before compared one', () => {
  332. const root = new Node();
  333. const position = new Position( root, 0 );
  334. const compared = new Position( root, 1 );
  335. expect( position.compareWith( compared ) ).to.equal( 'before' );
  336. } );
  337. it( 'should return after if the position is after compared one', () => {
  338. const root = new Node();
  339. const position = new Position( root, 4 );
  340. const compared = new Position( root, 1 );
  341. expect( position.compareWith( compared ) ).to.equal( 'after' );
  342. } );
  343. it( 'should return different if positions are in different roots', () => {
  344. const root1 = new Node();
  345. const root2 = new Node();
  346. const position = new Position( root1, 4 );
  347. const compared = new Position( root2, 1 );
  348. expect( position.compareWith( compared ) ).to.equal( 'different' );
  349. } );
  350. it( 'should return correct results if position is in document fragment', () => {
  351. const node = new Node( 'name' );
  352. const docFrag = new DocumentFragment( [ node ] );
  353. const position = new Position( docFrag, 0 );
  354. const compared = new Position( docFrag, 1 );
  355. const posInNode = new Position( node, 0 );
  356. expect( position.compareWith( compared ) ).to.equal( 'before' );
  357. expect( compared.compareWith( position ) ).to.equal( 'after' );
  358. expect( compared.compareWith( compared ) ).to.equal( 'same' );
  359. expect( position.compareWith( posInNode ) ).to.equal( 'before' );
  360. expect( compared.compareWith( posInNode ) ).to.equal( 'after' );
  361. } );
  362. } );
  363. describe( 'createBefore', () => {
  364. it( 'should throw error if one try to create positions before root', () => {
  365. expect( () => {
  366. Position.createBefore( parse( '<p></p>' ) );
  367. } ).to.throw( CKEditorError, /view-position-before-root/ );
  368. } );
  369. it( 'should create positions before `Node`', () => {
  370. const { selection } = parse( '<p>[]<b></b></p>' );
  371. const position = selection.getFirstPosition();
  372. const nodeAfter = position.nodeAfter;
  373. expect( Position.createBefore( nodeAfter ).isEqual( position ) ).to.be.true;
  374. } );
  375. it( 'should create positions before `TextProxy`', () => {
  376. const text = new Text( 'abc' );
  377. const textProxy = new TextProxy( text, 1, 1 );
  378. const position = new Position( text, 1 );
  379. expect( Position.createBefore( textProxy ) ).deep.equal( position );
  380. } );
  381. } );
  382. describe( 'createAfter', () => {
  383. it( 'should throw error if one try to create positions after root', () => {
  384. expect( () => {
  385. Position.createAfter( parse( '<p></p>' ) );
  386. } ).to.throw( CKEditorError, /view-position-after-root/ );
  387. } );
  388. it( 'should create positions after `Node`', () => {
  389. const { selection } = parse( '<p><b></b>[]</p>' );
  390. const position = selection.getFirstPosition();
  391. const nodeBefore = position.nodeBefore;
  392. expect( Position.createAfter( nodeBefore ).isEqual( position ) ).to.be.true;
  393. } );
  394. it( 'should create positions after `TextProxy`', () => {
  395. const text = new Text( 'abcd' );
  396. const textProxy = new TextProxy( text, 1, 2 );
  397. const position = new Position( text, 3 );
  398. expect( Position.createAfter( textProxy ) ).deep.equal( position );
  399. } );
  400. } );
  401. describe( 'getEditableElement', () => {
  402. it( 'should return null if position is not inside EditableElement', () => {
  403. const position = new Position( new Element( 'p' ), 0 );
  404. expect( position.editableElement ).to.be.null;
  405. } );
  406. it( 'should return EditableElement when position is placed inside', () => {
  407. const document = new Document();
  408. const p = new Element( 'p' );
  409. const editable = new EditableElement( 'div', null, p );
  410. editable.document = document;
  411. const position = new Position( p, 0 );
  412. expect( position.editableElement ).to.equal( editable );
  413. document.destroy();
  414. } );
  415. } );
  416. } );