position.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 { parse, stringify } from '../../src/dev-utils/view';
  14. import TreeWalker from '../../src/view/treewalker';
  15. import createViewRoot from './_utils/createroot';
  16. import AttributeElement from '../../src/view/attributeelement';
  17. import ContainerElement from '../../src/view/containerelement';
  18. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  19. describe( 'Position', () => {
  20. const parentMock = {};
  21. describe( 'constructor()', () => {
  22. it( 'should create element without attributes', () => {
  23. const position = new Position( parentMock, 5 );
  24. expect( position ).to.have.property( 'parent' ).that.equals( parentMock );
  25. expect( position ).to.have.property( 'offset' ).that.equals( 5 );
  26. } );
  27. } );
  28. describe( 'is()', () => {
  29. let position;
  30. beforeEach( () => {
  31. position = new Position( parentMock, 5 );
  32. } );
  33. it( 'should return true for "position"', () => {
  34. expect( position.is( 'position' ) ).to.be.true;
  35. expect( position.is( 'view:position' ) ).to.be.true;
  36. } );
  37. it( 'should return false for other accept values', () => {
  38. expect( position.is( 'rootElement' ) ).to.be.false;
  39. expect( position.is( 'containerElement' ) ).to.be.false;
  40. expect( position.is( 'element' ) ).to.be.false;
  41. expect( position.is( 'p' ) ).to.be.false;
  42. expect( position.is( 'text' ) ).to.be.false;
  43. expect( position.is( 'textProxy' ) ).to.be.false;
  44. expect( position.is( 'attributeElement' ) ).to.be.false;
  45. expect( position.is( 'uiElement' ) ).to.be.false;
  46. expect( position.is( 'emptyElement' ) ).to.be.false;
  47. expect( position.is( 'documentFragment' ) ).to.be.false;
  48. expect( position.is( 'model:position' ) ).to.be.false;
  49. } );
  50. } );
  51. describe( 'nodeBefore', () => {
  52. it( 'should equal to node that is before position', () => {
  53. const b1 = new Element( 'b' );
  54. const el = new Element( 'p', null, [ b1 ] );
  55. const position = new Position( el, 1 );
  56. expect( position.nodeBefore ).to.equal( b1 );
  57. } );
  58. it( 'should equal null if there is no node before', () => {
  59. const b1 = new Element( 'b' );
  60. const el = new Element( 'p', null, [ b1 ] );
  61. const position = new Position( el, 0 );
  62. expect( position.nodeBefore ).to.be.null;
  63. } );
  64. it( 'should equal null if position is located inside text node', () => {
  65. const text = new Text( 'foobar' );
  66. const position = new Position( text, 3 );
  67. expect( position.nodeBefore ).to.be.null;
  68. } );
  69. } );
  70. describe( 'nodeAfter', () => {
  71. it( 'should equal to node that is after position', () => {
  72. const b1 = new Element( 'b' );
  73. const el = new Element( 'p', null, [ b1 ] );
  74. const position = new Position( el, 0 );
  75. expect( position.nodeAfter ).to.equal( b1 );
  76. } );
  77. it( 'should equal null if there is no node before', () => {
  78. const b1 = new Element( 'b' );
  79. const el = new Element( 'p', null, [ b1 ] );
  80. const position = new Position( el, 1 );
  81. expect( position.nodeAfter ).to.be.null;
  82. } );
  83. it( 'should equal null if position is located inside text node', () => {
  84. const text = new Text( 'foobar' );
  85. const position = new Position( text, 3 );
  86. expect( position.nodeAfter ).to.be.null;
  87. } );
  88. } );
  89. describe( 'getShiftedBy', () => {
  90. it( 'returns new instance with shifted offset', () => {
  91. const position = new Position( parentMock, 10 );
  92. const shifted = position.getShiftedBy( 12 );
  93. expect( shifted.offset ).to.equal( 22 );
  94. } );
  95. it( 'accepts negative values', () => {
  96. const position = new Position( parentMock, 10 );
  97. const shifted = position.getShiftedBy( -5 );
  98. expect( shifted.offset ).to.equal( 5 );
  99. } );
  100. it( 'prevents offset to be a negative value', () => {
  101. const position = new Position( parentMock, 10 );
  102. const shifted = position.getShiftedBy( -20 );
  103. expect( shifted.offset ).to.equal( 0 );
  104. } );
  105. } );
  106. describe( 'getLastMatchingPosition', () => {
  107. it( 'should skip forward', () => {
  108. const { view, selection } = parse( '<p><b>{}foo</b></p>' );
  109. let position = selection.getFirstPosition();
  110. position = position.getLastMatchingPosition( value => value.type == 'text' );
  111. expect( stringify( view, position ) ).to.equal( '<p><b>foo[]</b></p>' );
  112. } );
  113. it( 'should skip backward', () => {
  114. const { view, selection } = parse( '<p><b>foo{}</b></p>' );
  115. let position = selection.getFirstPosition();
  116. position = position.getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } );
  117. expect( stringify( view, position ) ).to.equal( '<p><b>[]foo</b></p>' );
  118. } );
  119. } );
  120. describe( 'getRoot', () => {
  121. it( 'should return it\'s parent root', () => {
  122. const foo = new Text( 'foo' );
  123. const docFrag = new DocumentFragment( foo );
  124. expect( new Position( foo, 1 ).root ).to.equal( docFrag );
  125. const bar = new Text( 'bar' );
  126. const p = new Element( 'p', null, bar );
  127. expect( new Position( bar, 2 ).root ).to.equal( p );
  128. expect( new Position( p, 0 ).root ).to.equal( p );
  129. } );
  130. } );
  131. describe( 'getAncestors', () => {
  132. it( 'should return it\'s parent and all it\'s ancestors', () => {
  133. const foo = new Text( 'foo' );
  134. const p = new Element( 'p', null, foo );
  135. const div = new Element( 'div', null, p );
  136. const docFrag = new DocumentFragment( div );
  137. expect( new Position( foo, 1 ).getAncestors() ).to.deep.equal( [ docFrag, div, p, foo ] );
  138. } );
  139. it( 'should return DocumentFragment if position is directly in document fragment', () => {
  140. const docFrag = new DocumentFragment();
  141. expect( new Position( docFrag, 0 ).getAncestors() ).to.deep.equal( [ docFrag ] );
  142. } );
  143. } );
  144. describe( 'isEqual', () => {
  145. it( 'should return true for same object', () => {
  146. const position = new Position( {}, 12 );
  147. expect( position.isEqual( position ) ).to.be.true;
  148. } );
  149. it( 'should return true for positions with same parent and offset', () => {
  150. const parentMock = {};
  151. const position1 = new Position( parentMock, 12 );
  152. const position2 = new Position( parentMock, 12 );
  153. expect( position1.isEqual( position2 ) ).to.be.true;
  154. } );
  155. it( 'should return false for positions with different parents', () => {
  156. const position1 = new Position( {}, 12 );
  157. const position2 = new Position( {}, 12 );
  158. expect( position1.isEqual( position2 ) ).to.be.false;
  159. } );
  160. it( 'should return false for positions with different positions', () => {
  161. const parentMock = {};
  162. const position1 = new Position( parentMock, 12 );
  163. const position2 = new Position( parentMock, 2 );
  164. expect( position1.isEqual( position2 ) ).to.be.false;
  165. } );
  166. } );
  167. describe( 'isBefore', () => {
  168. it( 'should return false for same positions', () => {
  169. const node = new Node();
  170. const position1 = new Position( node, 10 );
  171. const position2 = new Position( node, 10 );
  172. expect( position1.isBefore( position1 ) ).to.be.false;
  173. expect( position1.isBefore( position2 ) ).to.be.false;
  174. expect( position2.isBefore( position1 ) ).to.be.false;
  175. } );
  176. it( 'should return false if no common ancestor is found', () => {
  177. const t1 = new Text( 'foo' );
  178. const t2 = new Text( 'bar' );
  179. const e1 = new Element( 'p', null, [ t1 ] );
  180. const e2 = new Element( 'p', null, [ t2 ] );
  181. const position1 = new Position( e1, 0 );
  182. const position2 = new Position( e2, 1 );
  183. expect( position1.isBefore( position2 ) );
  184. expect( position2.isBefore( position1 ) );
  185. } );
  186. it( 'should return true if position is before in same node', () => {
  187. const node = new Node();
  188. const p1 = new Position( node, 10 );
  189. const p2 = new Position( node, 5 );
  190. expect( p2.isBefore( p1 ) ).to.be.true;
  191. expect( p1.isBefore( p2 ) ).to.be.false;
  192. } );
  193. it( 'should compare positions that have common parent', () => {
  194. const t1 = new Text( 'foo' );
  195. const t2 = new Text( 'bar' );
  196. const root = new Element( 'p', null, [ t1, t2 ] );
  197. const position1 = new Position( t1, 2 );
  198. const position2 = new Position( t2, 0 );
  199. const position3 = new Position( root, 0 );
  200. const position4 = new Position( root, 2 );
  201. const position5 = new Position( t1, 0 );
  202. const position6 = new Position( root, 1 );
  203. expect( position1.isBefore( position2 ) ).to.be.true;
  204. expect( position2.isBefore( position1 ) ).to.be.false;
  205. expect( position3.isBefore( position1 ) ).to.be.true;
  206. expect( position3.isBefore( position2 ) ).to.be.true;
  207. expect( position1.isBefore( position3 ) ).to.be.false;
  208. expect( position2.isBefore( position3 ) ).to.be.false;
  209. expect( position4.isBefore( position1 ) ).to.be.false;
  210. expect( position4.isBefore( position3 ) ).to.be.false;
  211. expect( position3.isBefore( position4 ) ).to.be.true;
  212. expect( position3.isBefore( position5 ) ).to.be.true;
  213. expect( position6.isBefore( position2 ) ).to.be.true;
  214. expect( position1.isBefore( position6 ) ).to.be.true;
  215. } );
  216. } );
  217. describe( 'isAfter', () => {
  218. it( 'should return false for same positions', () => {
  219. const node = new Node();
  220. const position1 = new Position( node, 10 );
  221. const position2 = new Position( node, 10 );
  222. expect( position1.isAfter( position1 ) ).to.be.false;
  223. expect( position1.isAfter( position2 ) ).to.be.false;
  224. expect( position2.isAfter( position1 ) ).to.be.false;
  225. } );
  226. it( 'should return false if no common ancestor is found', () => {
  227. const t1 = new Text( 'foo' );
  228. const t2 = new Text( 'bar' );
  229. const e1 = new Element( 'p', null, [ t1 ] );
  230. const e2 = new Element( 'p', null, [ t2 ] );
  231. const position1 = new Position( e1, 0 );
  232. const position2 = new Position( e2, 1 );
  233. expect( position1.isAfter( position2 ) );
  234. expect( position2.isAfter( position1 ) );
  235. } );
  236. it( 'should return true if position is after in same node', () => {
  237. const node = new Node();
  238. const p1 = new Position( node, 10 );
  239. const p2 = new Position( node, 5 );
  240. expect( p2.isAfter( p1 ) ).to.be.false;
  241. expect( p1.isAfter( p2 ) ).to.be.true;
  242. } );
  243. it( 'should compare positions that have common parent', () => {
  244. const t1 = new Text( 'foo' );
  245. const t2 = new Text( 'bar' );
  246. const root = new Element( 'p', null, [ t1, t2 ] );
  247. const position1 = new Position( t1, 2 );
  248. const position2 = new Position( t2, 0 );
  249. const position3 = new Position( root, 0 );
  250. const position4 = new Position( root, 2 );
  251. const position5 = new Position( t1, 0 );
  252. const position6 = new Position( root, 1 );
  253. expect( position1.isAfter( position2 ) ).to.be.false;
  254. expect( position2.isAfter( position1 ) ).to.be.true;
  255. expect( position3.isAfter( position1 ) ).to.be.false;
  256. expect( position3.isAfter( position2 ) ).to.be.false;
  257. expect( position1.isAfter( position3 ) ).to.be.true;
  258. expect( position2.isAfter( position3 ) ).to.be.true;
  259. expect( position4.isAfter( position1 ) ).to.be.true;
  260. expect( position4.isAfter( position3 ) ).to.be.true;
  261. expect( position3.isAfter( position4 ) ).to.be.false;
  262. expect( position5.isAfter( position3 ) ).to.be.true;
  263. expect( position2.isAfter( position6 ) ).to.be.true;
  264. } );
  265. } );
  266. describe( 'isAtStart', () => {
  267. it( 'should return true if it is at the start of it\'s parent', () => {
  268. const foo = new Text( 'foo' );
  269. const position = new Position( foo, 0 );
  270. expect( position.isAtStart ).to.be.true;
  271. } );
  272. it( 'should return false if it is not at the start of it\'s parent', () => {
  273. const foo = new Text( 'foo' );
  274. const position = new Position( foo, 1 );
  275. expect( position.isAtStart ).to.be.false;
  276. } );
  277. } );
  278. describe( 'isAtEnd', () => {
  279. it( 'should return true if it is at the end of it\'s parent', () => {
  280. const foo = new Text( 'foo' );
  281. const p = new Element( 'p', null, foo );
  282. expect( new Position( foo, 3 ).isAtEnd ).to.be.true;
  283. expect( new Position( p, 1 ).isAtEnd ).to.be.true;
  284. } );
  285. it( 'should return false if it is not at the end of it\'s parent', () => {
  286. const foo = new Text( 'foo' );
  287. const p = new Element( 'p', null, foo );
  288. expect( new Position( foo, 2 ).isAtEnd ).to.be.false;
  289. expect( new Position( p, 0 ).isAtEnd ).to.be.false;
  290. } );
  291. } );
  292. describe( 'compareWith', () => {
  293. it( 'should return same if positions are same', () => {
  294. const root = new Element();
  295. const position = new Position( root, 0 );
  296. const compared = new Position( root, 0 );
  297. expect( position.compareWith( compared ) ).to.equal( 'same' );
  298. } );
  299. it( 'should return before if the position is before compared one', () => {
  300. const root = new Element();
  301. const position = new Position( root, 0 );
  302. const compared = new Position( root, 1 );
  303. expect( position.compareWith( compared ) ).to.equal( 'before' );
  304. } );
  305. it( 'should return after if the position is after compared one', () => {
  306. const root = new Element();
  307. const position = new Position( root, 4 );
  308. const compared = new Position( root, 1 );
  309. expect( position.compareWith( compared ) ).to.equal( 'after' );
  310. } );
  311. it( 'should return different if positions are in different roots', () => {
  312. const root1 = new Element();
  313. const root2 = new Element();
  314. const position = new Position( root1, 4 );
  315. const compared = new Position( root2, 1 );
  316. expect( position.compareWith( compared ) ).to.equal( 'different' );
  317. } );
  318. it( 'should return correct results if position is in document fragment', () => {
  319. const node = new Element( 'name' );
  320. const docFrag = new DocumentFragment( [ node ] );
  321. const position = new Position( docFrag, 0 );
  322. const compared = new Position( docFrag, 1 );
  323. const posInNode = new Position( node, 0 );
  324. expect( position.compareWith( compared ) ).to.equal( 'before' );
  325. expect( compared.compareWith( position ) ).to.equal( 'after' );
  326. expect( compared.compareWith( compared ) ).to.equal( 'same' );
  327. expect( position.compareWith( posInNode ) ).to.equal( 'before' );
  328. expect( compared.compareWith( posInNode ) ).to.equal( 'after' );
  329. } );
  330. } );
  331. describe( 'static creators', () => {
  332. describe( '_createAt()', () => {
  333. it( 'should throw if no offset is passed', () => {
  334. const element = new Element( 'p' );
  335. expectToThrowCKEditorError( () => {
  336. Position._createAt( element );
  337. }, /view-createPositionAt-offset-required/ );
  338. } );
  339. it( 'should create positions from positions', () => {
  340. const p = new Element( 'p' );
  341. const position = new Position( p, 0 );
  342. const created = Position._createAt( position, 0 );
  343. expect( created.isEqual( position ) ).to.be.true;
  344. expect( created ).to.not.be.equal( position );
  345. } );
  346. it( 'should create positions from node and offset', () => {
  347. const foo = new Text( 'foo' );
  348. const p = new Element( 'p', null, foo );
  349. expect( Position._createAt( foo, 0 ).parent ).to.equal( foo );
  350. expect( Position._createAt( foo, 0 ).offset ).to.equal( 0 );
  351. expect( Position._createAt( foo, 2 ).parent ).to.equal( foo );
  352. expect( Position._createAt( foo, 2 ).offset ).to.equal( 2 );
  353. expect( Position._createAt( p, 1 ).parent ).to.equal( p );
  354. expect( Position._createAt( p, 1 ).offset ).to.equal( 1 );
  355. } );
  356. it( 'should create positions from node and flag', () => {
  357. const foo = new Text( 'foo' );
  358. const p = new Element( 'p', null, foo );
  359. const fooEnd = Position._createAt( foo, 'end' );
  360. const fooBefore = Position._createAt( foo, 'before' );
  361. const fooAfter = Position._createAt( foo, 'after' );
  362. const pEnd = Position._createAt( p, 'end' );
  363. // pBefore and pAfter would throw.
  364. expect( fooEnd.parent ).to.equal( foo );
  365. expect( fooEnd.offset ).to.equal( 3 );
  366. expect( fooBefore.parent ).to.equal( p );
  367. expect( fooBefore.offset ).to.equal( 0 );
  368. expect( fooAfter.parent ).to.equal( p );
  369. expect( fooAfter.offset ).to.equal( 1 );
  370. expect( pEnd.parent ).to.equal( p );
  371. expect( pEnd.offset ).to.equal( 1 );
  372. } );
  373. it( 'should create positions in document fragment', () => {
  374. const foo = new Text( 'foo' );
  375. const docFrag = new DocumentFragment( [ foo ] );
  376. const pStart = Position._createAt( docFrag, 0 );
  377. const pEnd = Position._createAt( docFrag, 'end' );
  378. expect( pStart.parent ).to.equal( docFrag );
  379. expect( pStart.offset ).to.equal( 0 );
  380. expect( pEnd.parent ).to.equal( docFrag );
  381. expect( pEnd.offset ).to.equal( 1 );
  382. } );
  383. } );
  384. describe( '_createBefore()', () => {
  385. it( 'should throw error if one try to create positions before root', () => {
  386. const paragraph = parse( '<p></p>' );
  387. expectToThrowCKEditorError( () => {
  388. Position._createBefore( paragraph );
  389. }, /view-position-before-root/, paragraph );
  390. } );
  391. it( 'should create positions before `Node`', () => {
  392. const { selection } = parse( '<p>[]<b></b></p>' );
  393. const position = selection.getFirstPosition();
  394. const nodeAfter = position.nodeAfter;
  395. expect( Position._createBefore( nodeAfter ).isEqual( position ) ).to.be.true;
  396. } );
  397. it( 'should create positions before `TextProxy`', () => {
  398. const text = new Text( 'abc' );
  399. const textProxy = new TextProxy( text, 1, 1 );
  400. const position = new Position( text, 1 );
  401. expect( Position._createBefore( textProxy ) ).deep.equal( position );
  402. } );
  403. } );
  404. describe( '_createAfter()', () => {
  405. it( 'should throw error if one try to create positions after root', () => {
  406. const paragraph = parse( '<p></p>' );
  407. expectToThrowCKEditorError( () => {
  408. Position._createAfter( paragraph );
  409. }, /view-position-after-root/, paragraph );
  410. } );
  411. it( 'should create positions after `Node`', () => {
  412. const { selection } = parse( '<p><b></b>[]</p>' );
  413. const position = selection.getFirstPosition();
  414. const nodeBefore = position.nodeBefore;
  415. expect( Position._createAfter( nodeBefore ).isEqual( position ) ).to.be.true;
  416. } );
  417. it( 'should create positions after `TextProxy`', () => {
  418. const text = new Text( 'abcd' );
  419. const textProxy = new TextProxy( text, 1, 2 );
  420. const position = new Position( text, 3 );
  421. expect( Position._createAfter( textProxy ) ).deep.equal( position );
  422. } );
  423. } );
  424. } );
  425. describe( 'getEditableElement', () => {
  426. it( 'should return null if position is not inside EditableElement', () => {
  427. const position = new Position( new Element( 'p' ), 0 );
  428. expect( position.editableElement ).to.be.null;
  429. } );
  430. it( 'should return EditableElement when position is placed inside', () => {
  431. const document = new Document();
  432. const p = new Element( 'p' );
  433. const editable = new EditableElement( 'div', null, p );
  434. editable._document = document;
  435. const position = new Position( p, 0 );
  436. expect( position.editableElement ).to.equal( editable );
  437. } );
  438. } );
  439. describe( 'getCommonAncestor()', () => {
  440. let div, ul, liUl1, liUl2, texts, section, article, ol, liOl1, liOl2, p;
  441. // |- div
  442. // |- ul
  443. // | |- li
  444. // | | |- foz
  445. // | |- li
  446. // | |- bar
  447. // |- section
  448. // |- Sed id libero at libero tristique
  449. // |- article
  450. // | |- ol
  451. // | | |- li
  452. // | | | |- Lorem ipsum dolor sit amet.
  453. // | | |- li
  454. // | | |- Mauris tincidunt tincidunt leo ac rutrum.
  455. // | |- p
  456. // | | |- Maecenas accumsan tellus.
  457. beforeEach( () => {
  458. texts = {
  459. foz: new Text( 'foz' ),
  460. bar: new Text( 'bar' ),
  461. lorem: new Text( 'Lorem ipsum dolor sit amet.' ),
  462. mauris: new Text( 'Mauris tincidunt tincidunt leo ac rutrum.' ),
  463. maecenas: new Text( 'Maecenas accumsan tellus.' ),
  464. sed: new Text( 'Sed id libero at libero tristique.' )
  465. };
  466. liUl1 = new Element( 'li', null, texts.foz );
  467. liUl2 = new Element( 'li', null, texts.bar );
  468. ul = new Element( 'ul', null, [ liUl1, liUl2 ] );
  469. liOl1 = new Element( 'li', null, texts.lorem );
  470. liOl2 = new Element( 'li', null, texts.mauris );
  471. ol = new Element( 'ol', null, [ liOl1, liOl2 ] );
  472. p = new Element( 'p', null, texts.maecenas );
  473. article = new Element( 'article', null, [ ol, p ] );
  474. section = new Element( 'section', null, [ texts.sed, article ] );
  475. div = new Element( 'div', null, [ ul, section ] );
  476. } );
  477. it( 'for two the same positions returns the parent element', () => {
  478. const afterLoremPosition = new Position( liOl1, 5 );
  479. const otherPosition = Position._createAt( afterLoremPosition );
  480. test( afterLoremPosition, otherPosition, liOl1 );
  481. } );
  482. it( 'for two positions in the same element returns the element', () => {
  483. const startMaecenasPosition = Position._createAt( liOl2, 0 );
  484. const beforeTellusPosition = new Position( liOl2, 18 );
  485. test( startMaecenasPosition, beforeTellusPosition, liOl2 );
  486. } );
  487. it( 'works when one of the positions is nested deeper than the other #1', () => {
  488. const firstPosition = new Position( liUl1, 1 );
  489. const secondPosition = new Position( p, 3 );
  490. test( firstPosition, secondPosition, div );
  491. } );
  492. it( 'works when one of the positions is nested deeper than the other #2', () => {
  493. const firstPosition = new Position( liOl2, 10 );
  494. const secondPosition = new Position( section, 1 );
  495. test( firstPosition, secondPosition, section );
  496. } );
  497. it( 'for two positions in different trees returns null', () => {
  498. const div = new Element( 'div' );
  499. const posInDiv = new Position( div, 0 );
  500. const firstPosition = new Position( liOl2, 10 );
  501. test( posInDiv, firstPosition, null );
  502. } );
  503. function test( positionA, positionB, lca ) {
  504. expect( positionA.getCommonAncestor( positionB ) ).to.equal( lca );
  505. expect( positionB.getCommonAncestor( positionA ) ).to.equal( lca );
  506. }
  507. } );
  508. describe( 'getWalker()', () => {
  509. let root;
  510. beforeEach( () => {
  511. const doc = new Document();
  512. root = createViewRoot( doc );
  513. const textAbcd = new Text( 'abcd' );
  514. const bold = new AttributeElement( 'b', null, [ textAbcd ] );
  515. const paragraph = new ContainerElement( 'p', null, [ bold ] );
  516. const img = new ContainerElement( 'img' );
  517. root._insertChild( 0, [ img, paragraph ] );
  518. } );
  519. it( 'should be possible to iterate using this method', () => {
  520. const position = new Position( root, 0 );
  521. const items = [];
  522. const walker = position.getWalker();
  523. for ( const value of walker ) {
  524. items.push( value.type + ':' + ( value.item.name || value.item.data ) );
  525. }
  526. expect( items ).to.deep.equal( [
  527. 'elementStart:img',
  528. 'elementEnd:img',
  529. 'elementStart:p',
  530. 'elementStart:b',
  531. 'text:abcd',
  532. 'elementEnd:b',
  533. 'elementEnd:p'
  534. ] );
  535. } );
  536. it( 'should return treewalker with given options', () => {
  537. const position = new Position( root, 0 );
  538. const walker = position.getWalker( { singleCharacters: true } );
  539. expect( walker ).to.be.instanceof( TreeWalker );
  540. expect( walker ).to.have.property( 'singleCharacters' ).that.is.true;
  541. expect( walker ).to.have.property( 'position' );
  542. expect( walker.position.isEqual( position ) ).to.be.true;
  543. expect( walker ).to.have.property( 'shallow' ).that.is.false;
  544. } );
  545. } );
  546. } );