8
0

position.js 24 KB

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