position.js 24 KB

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