position.js 24 KB

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