position.js 22 KB

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