range.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Range from '../../src/view/range';
  6. import Position from '../../src/view/position';
  7. import Element from '../../src/view/element';
  8. import DocumentFragment from '../../src/view/documentfragment';
  9. import Text from '../../src/view/text';
  10. import TreeWalker from '../../src/view/treewalker';
  11. import { parse, stringify } from '../../src/dev-utils/view';
  12. function getRange( view, options = {} ) {
  13. const { selection } = parse( view, options );
  14. return selection.getFirstRange();
  15. }
  16. describe( 'Range', () => {
  17. describe( 'constructor()', () => {
  18. it( 'creates range from provided positions', () => {
  19. const start = new Position( {}, 1 );
  20. const end = new Position( {}, 2 );
  21. const range = new Range( start, end );
  22. expect( range ).to.be.an.instanceof( Range );
  23. expect( range ).to.have.property( 'start' ).that.not.equals( start );
  24. expect( range ).to.have.property( 'end' ).that.not.equals( end );
  25. expect( range.start.parent ).to.equal( start.parent );
  26. expect( range.end.parent ).to.equal( end.parent );
  27. expect( range.start.offset ).to.equal( start.offset );
  28. expect( range.end.offset ).to.equal( end.offset );
  29. } );
  30. it( 'creates collapsed range', () => {
  31. const start = new Position( {}, 1 );
  32. const range = new Range( start );
  33. expect( range.start.isEqual( start ) ).to.be.true;
  34. expect( range.isCollapsed ).to.be.true;
  35. } );
  36. } );
  37. describe( 'iterator', () => {
  38. it( 'should iterate over the range returning tree walker values', () => {
  39. const range = getRange( '<p>fo{o</p><p>bar</p><p>xy}z</p>' );
  40. const values = Array.from( range );
  41. expect( values.length ).to.equal( 5 );
  42. expect( values[ 0 ].item.data ).to.equal( 'o' );
  43. expect( values[ 1 ].item.name ).to.equal( 'p' );
  44. expect( values[ 2 ].item.data ).to.equal( 'bar' );
  45. expect( values[ 3 ].item.name ).to.equal( 'p' );
  46. expect( values[ 4 ].item.data ).to.equal( 'xy' );
  47. } );
  48. } );
  49. describe( 'isFlat', () => {
  50. it( 'should be true if range start and range end are in same parent', () => {
  51. const range = getRange( '<p>f{oo}</p><p>bar</p>' );
  52. expect( range.isFlat ).to.be.true;
  53. } );
  54. it( 'should be false if range start and range end are in different parents', () => {
  55. const range = getRange( '<p>fo{o</p><p>b}ar</p>' );
  56. expect( range.isFlat ).to.be.false;
  57. } );
  58. } );
  59. describe( 'getRoot', () => {
  60. it( 'should return root element in which range is created', () => {
  61. const viewRoot = new Element( 'div' );
  62. const range = getRange( '<p>f{oo</p><p>ba}r</p>', { rootElement: viewRoot } );
  63. expect( range.root ).to.equal( viewRoot );
  64. } );
  65. it( 'should return document fragment in which range is created', () => {
  66. const viewFrag = new DocumentFragment();
  67. const range = getRange( '<p>f{oo</p><p>ba}r</p>', { rootElement: viewFrag } );
  68. expect( range.root ).to.equal( viewFrag );
  69. } );
  70. } );
  71. describe( 'getEnlarged', () => {
  72. it( 'case 1', () => {
  73. expect( enlarge( '<p>f<b>{oo}</b></p><p>bar</p>' ) )
  74. .to.equal( '<p>f[<b>oo</b>]</p><p>bar</p>' );
  75. } );
  76. it( 'case 2', () => {
  77. expect( enlarge( '<p>f{oo}bar</p>' ) )
  78. .to.equal( '<p>f{oo}bar</p>' );
  79. } );
  80. it( 'case 3', () => {
  81. expect( enlarge( '<p>f<span></span>{oo}<span></span>bar</p>' ) )
  82. .to.equal( '<p>f[<span></span>oo<span></span>]bar</p>' );
  83. } );
  84. it( 'case 4', () => {
  85. expect( enlarge( '<p>f<img></img>{oo}<img></img>bar</p>' ) )
  86. .to.equal( '<p>f<img></img>[oo]<img></img>bar</p>' );
  87. } );
  88. it( 'case 5', () => {
  89. expect( enlarge( '<p><b>f</b>{oo}<b><span></span>bar</b></p>' ) )
  90. .to.equal( '<p><b>f[</b>oo<b><span></span>]bar</b></p>' );
  91. } );
  92. it( 'case6', () => {
  93. expect( enlarge( '<p>foo</p><p>[bar]</p><p>bom</p>' ) )
  94. .to.equal( '<p>foo</p><p>[bar]</p><p>bom</p>' );
  95. } );
  96. function enlarge( data ) {
  97. data = data
  98. .replace( /<p>/g, '<container:p>' )
  99. .replace( /<\/p>/g, '</container:p>' )
  100. .replace( /<b>/g, '<attribute:b>' )
  101. .replace( /<\/b>/g, '</attribute:b>' )
  102. .replace( /<img><\/img>/g, '<empty:img></empty:img>' )
  103. .replace( /<span><\/span>/g, '<ui:span></ui:span>' );
  104. const viewFrag = new DocumentFragment();
  105. const { view, selection } = parse( data, { rootElement: viewFrag } );
  106. const range = selection.getFirstRange();
  107. const enlargedRange = range.getEnlarged();
  108. return stringify( view, enlargedRange );
  109. }
  110. } );
  111. describe( 'getTrimmed', () => {
  112. it( 'case 1', () => {
  113. expect( trim( '<p>f[<b>oo</b>]</p><p>bar</p>' ) )
  114. .to.equal( '<p>f<b>{oo}</b></p><p>bar</p>' );
  115. } );
  116. it( 'case 2', () => {
  117. expect( trim( '<p>f{oo}bar</p>' ) )
  118. .to.equal( '<p>f{oo}bar</p>' );
  119. } );
  120. it( 'case 3', () => {
  121. expect( trim( '<p>f[<span></span>oo<span></span>]bar</p>' ) )
  122. .to.equal( '<p>f<span></span>{oo}<span></span>bar</p>' );
  123. } );
  124. it( 'case 4', () => {
  125. expect( trim( '<p>f<img></img>[oo]<img></img>bar</p>' ) )
  126. .to.equal( '<p>f<img></img>{oo}<img></img>bar</p>' );
  127. } );
  128. it( 'case 5', () => {
  129. expect( trim( '<p><b>f[</b>oo<b><span></span>]bar</b></p>' ) )
  130. .to.equal( '<p><b>f</b>{oo}<b><span></span>bar</b></p>' );
  131. } );
  132. it( 'case 6', () => {
  133. expect( trim( '<p>foo[</p><p>bar</p><p>]bom</p>' ) )
  134. .to.equal( '<p>foo[</p><p>bar</p><p>]bom</p>' );
  135. } );
  136. it( 'case 7', () => {
  137. expect( trim( '<p>foo[<b><img></img></b>]bom</p>' ) )
  138. .to.equal( '<p>foo<b>[<img></img>]</b>bom</p>' );
  139. } );
  140. // Other results may theoretically be correct too. It is not decided whether the trimmed range should
  141. // be collapsed in attribute element, at its start or its end. This is one of possible correct results
  142. // and we won't know for sure unless we have more cases. See #1058.
  143. it( 'case 8', () => {
  144. expect( trim( '<p>[<b></b>]</p>' ) )
  145. .to.equal( '<p><b></b>[]</p>' );
  146. } );
  147. // As above.
  148. it( 'case 9', () => {
  149. expect( trim( '<p><b></b>[<b></b>]<b></b></p>' ) )
  150. .to.equal( '<p><b></b><b></b>[]<b></b></p>' );
  151. } );
  152. // As above.
  153. it( 'case 10', () => {
  154. expect( trim( '<p>[<b></b><b></b>]</p>' ) )
  155. .to.equal( '<p><b></b><b></b>[]</p>' );
  156. } );
  157. function trim( data ) {
  158. data = data
  159. .replace( /<p>/g, '<container:p>' )
  160. .replace( /<\/p>/g, '</container:p>' )
  161. .replace( /<b>/g, '<attribute:b>' )
  162. .replace( /<\/b>/g, '</attribute:b>' )
  163. .replace( /<img><\/img>/g, '<empty:img></empty:img>' )
  164. .replace( /<span><\/span>/g, '<ui:span></ui:span>' );
  165. const viewFrag = new DocumentFragment();
  166. const { view, selection } = parse( data, { rootElement: viewFrag } );
  167. const range = selection.getFirstRange();
  168. const trimmedRange = range.getTrimmed();
  169. return stringify( view, trimmedRange );
  170. }
  171. } );
  172. describe( 'isEqual', () => {
  173. it( 'should return true for the same range', () => {
  174. const start = new Position( {}, 1 );
  175. const end = new Position( {}, 2 );
  176. const range = new Range( start, end );
  177. expect( range.isEqual( range ) ).to.be.true;
  178. } );
  179. it( 'should return true for ranges with same start and end positions', () => {
  180. const start = new Position( {}, 1 );
  181. const end = new Position( {}, 2 );
  182. const range1 = new Range( start, end );
  183. const range2 = new Range( start, end );
  184. expect( range1.isEqual( range2 ) ).to.be.true;
  185. } );
  186. it( 'should return false if start position is different', () => {
  187. const start1 = new Position( {}, 1 );
  188. const start2 = new Position( {}, 1 );
  189. const end = new Position( {}, 2 );
  190. const range1 = new Range( start1, end );
  191. const range2 = new Range( start2, end );
  192. expect( range1.isEqual( range2 ) ).to.be.false;
  193. } );
  194. it( 'should return false if end position is different', () => {
  195. const start = new Position( {}, 1 );
  196. const end1 = new Position( {}, 2 );
  197. const end2 = new Position( {}, 2 );
  198. const range1 = new Range( start, end1 );
  199. const range2 = new Range( start, end2 );
  200. expect( range1.isEqual( range2 ) ).to.be.false;
  201. } );
  202. it( 'should return false for ranges with same root and different offsets', () => {
  203. const mockObject = {};
  204. const range1 = new Range( new Position( mockObject, 0 ), new Position( mockObject, 10 ) );
  205. const range2 = new Range( new Position( mockObject, 2 ), new Position( mockObject, 10 ) );
  206. expect( range1.isEqual( range2 ) ).to.be.false;
  207. } );
  208. } );
  209. describe( 'containsPosition', () => {
  210. let viewRoot, range;
  211. beforeEach( () => {
  212. viewRoot = new Element( 'div' );
  213. range = getRange( '<p>fo{o</p><p>bar</p><p>xy}z</p>', { rootElement: viewRoot } );
  214. } );
  215. it( 'should return false if position is before range', () => {
  216. const position = new Position( viewRoot.getChild( 0 ).getChild( 0 ), 1 ); // After "f".
  217. expect( range.containsPosition( position ) ).to.be.false;
  218. } );
  219. it( 'should return false if position is after range', () => {
  220. const position = new Position( viewRoot.getChild( 2 ).getChild( 0 ), 3 ); // After "z".
  221. expect( range.containsPosition( position ) ).to.be.false;
  222. } );
  223. it( 'should return true if position is inside range', () => {
  224. const position = new Position( viewRoot.getChild( 1 ).getChild( 0 ), 1 ); // After "b".
  225. expect( range.containsPosition( position ) ).to.be.true;
  226. } );
  227. } );
  228. describe( 'containsRange', () => {
  229. let viewRoot, range, beforeF, afterF, beforeB, afterX;
  230. beforeEach( () => {
  231. viewRoot = new Element( 'div' );
  232. range = getRange( '<p>fo{o</p><p>bar</p><p>xy}z</p>', { rootElement: viewRoot } );
  233. beforeF = new Position( viewRoot.getChild( 0 ).getChild( 0 ), 0 );
  234. afterF = new Position( viewRoot.getChild( 0 ).getChild( 0 ), 1 );
  235. beforeB = new Position( viewRoot.getChild( 1 ).getChild( 0 ), 0 );
  236. afterX = new Position( viewRoot.getChild( 2 ).getChild( 0 ), 1 );
  237. } );
  238. it( 'should return false if ranges do not intersect', () => {
  239. const otherRange = new Range( beforeF, afterF );
  240. expect( range.containsRange( otherRange ) ).to.be.false;
  241. } );
  242. it( 'should return false if ranges intersect but only partially', () => {
  243. const otherRange = new Range( afterF, afterX );
  244. expect( range.containsRange( otherRange ) ).to.be.false;
  245. } );
  246. it( 'should return false if ranges are equal', () => {
  247. const otherRange = Range.createFromRange( range );
  248. expect( range.containsRange( otherRange ) ).to.be.false;
  249. } );
  250. it( 'should return true if given range is inside range', () => {
  251. const otherRange = new Range( beforeB, afterX );
  252. expect( range.containsRange( otherRange ) ).to.be.true;
  253. } );
  254. it( 'should return true if ranges are equal and check is not strict', () => {
  255. const otherRange = Range.createFromRange( range );
  256. expect( range.containsRange( otherRange, true ) ).to.be.true;
  257. } );
  258. it( 'should return true if ranges start at the same position and check is not strict', () => {
  259. const otherRange = new Range( range.start, afterX );
  260. expect( range.containsRange( otherRange, true ) ).to.be.true;
  261. } );
  262. it( 'should return true if ranges end at the same position and check is not strict', () => {
  263. const otherRange = new Range( beforeB, range.end );
  264. expect( range.containsRange( otherRange, true ) ).to.be.true;
  265. } );
  266. it( 'should return false if given range is collapsed and starts or ends at another range boundary', () => {
  267. expect( range.containsRange( new Range( range.start, range.start ) ) ).to.be.false;
  268. expect( range.containsRange( new Range( range.end, range.end ) ) ).to.be.false;
  269. expect( range.containsRange( new Range( range.start, range.start ), true ) ).to.be.false;
  270. expect( range.containsRange( new Range( range.end, range.end ), true ) ).to.be.false;
  271. } );
  272. } );
  273. describe( 'other range interaction', () => {
  274. let root, p1, p2, t1, t2, t3;
  275. // root
  276. // __________|__________
  277. // | |
  278. // ___p1___ p2
  279. // | | |
  280. // t1 t2 t3
  281. beforeEach( () => {
  282. t1 = new Text( 'foo' );
  283. t2 = new Text( 'bar' );
  284. t3 = new Text( 'baz' );
  285. p1 = new Element( 'p', null, [ t1, t2 ] );
  286. p2 = new Element( 'p', null, t3 );
  287. root = new Element( 'div', null, [ p1, p2 ] );
  288. } );
  289. describe( 'isIntersecting', () => {
  290. it( 'should return true if given range is equal', () => {
  291. const range = Range.createFromParentsAndOffsets( t1, 0, t3, 2 );
  292. const otherRange = Range.createFromRange( range );
  293. expect( range.isIntersecting( otherRange ) ).to.be.true;
  294. expect( otherRange.isIntersecting( range ) ).to.be.true;
  295. } );
  296. it( 'should return true if given range contains this range', () => {
  297. const range = Range.createFromParentsAndOffsets( t1, 0, t3, 3 );
  298. const otherRange = Range.createFromParentsAndOffsets( p1, 1, t2, 2 );
  299. expect( range.isIntersecting( otherRange ) ).to.be.true;
  300. expect( otherRange.isIntersecting( range ) ).to.be.true;
  301. } );
  302. it( 'should return true if given range ends in this range', () => {
  303. const range = Range.createFromParentsAndOffsets( root, 1, t3, 3 );
  304. const otherRange = Range.createFromParentsAndOffsets( t1, 0, p2, 0 );
  305. expect( range.isIntersecting( otherRange ) ).to.be.true;
  306. expect( otherRange.isIntersecting( range ) ).to.be.true;
  307. } );
  308. it( 'should return true if given range starts in this range', () => {
  309. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  310. const otherRange = Range.createFromParentsAndOffsets( p1, 1, p2, 0 );
  311. expect( range.isIntersecting( otherRange ) ).to.be.true;
  312. expect( otherRange.isIntersecting( range ) ).to.be.true;
  313. } );
  314. it( 'should return false if given range is fully before/after this range', () => {
  315. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  316. const otherRange = Range.createFromParentsAndOffsets( root, 1, t3, 0 );
  317. expect( range.isIntersecting( otherRange ) ).to.be.false;
  318. expect( otherRange.isIntersecting( range ) ).to.be.false;
  319. } );
  320. it( 'should return false if ranges are in different roots', () => {
  321. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  322. const otherRange = Range.createFromParentsAndOffsets( new Element( 'div' ), 1, t3, 0 );
  323. expect( range.isIntersecting( otherRange ) ).to.be.false;
  324. expect( otherRange.isIntersecting( range ) ).to.be.false;
  325. } );
  326. } );
  327. describe( 'getDifference', () => {
  328. it( 'should return range equal to original range if other range does not intersect with it', () => {
  329. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  330. const otherRange = Range.createFromParentsAndOffsets( root, 1, t3, 0 );
  331. const difference = range.getDifference( otherRange );
  332. expect( difference.length ).to.equal( 1 );
  333. expect( difference[ 0 ].isEqual( range ) ).to.be.true;
  334. } );
  335. it( 'should return shrunken range if other range intersects with it', () => {
  336. const range = Range.createFromParentsAndOffsets( root, 1, t3, 3 );
  337. const otherRange = Range.createFromParentsAndOffsets( t1, 0, p2, 0 );
  338. const difference = range.getDifference( otherRange );
  339. expect( difference.length ).to.equal( 1 );
  340. expect( difference[ 0 ].start.parent ).to.equal( p2 );
  341. expect( difference[ 0 ].start.offset ).to.equal( 0 );
  342. expect( difference[ 0 ].end.parent ).to.equal( t3 );
  343. expect( difference[ 0 ].end.offset ).to.equal( 3 );
  344. } );
  345. it( 'should return an empty array if other range contains or is same as the original range', () => {
  346. const range = Range.createFromParentsAndOffsets( p1, 1, t2, 2 );
  347. const otherRange = Range.createFromParentsAndOffsets( t1, 0, t3, 3 );
  348. const difference = range.getDifference( otherRange );
  349. expect( difference.length ).to.equal( 0 );
  350. } );
  351. it( 'should two ranges if other range is contained by the original range', () => {
  352. const range = Range.createFromParentsAndOffsets( t1, 0, t3, 3 );
  353. const otherRange = Range.createFromParentsAndOffsets( p1, 1, t2, 2 );
  354. const difference = range.getDifference( otherRange );
  355. expect( difference.length ).to.equal( 2 );
  356. expect( difference[ 0 ].start.parent ).to.equal( t1 );
  357. expect( difference[ 0 ].start.offset ).to.equal( 0 );
  358. expect( difference[ 0 ].end.parent ).to.equal( p1 );
  359. expect( difference[ 0 ].end.offset ).to.equal( 1 );
  360. expect( difference[ 1 ].start.parent ).to.equal( t2 );
  361. expect( difference[ 1 ].start.offset ).to.equal( 2 );
  362. expect( difference[ 1 ].end.parent ).to.equal( t3 );
  363. expect( difference[ 1 ].end.offset ).to.equal( 3 );
  364. } );
  365. } );
  366. describe( 'getIntersection', () => {
  367. it( 'should return range equal to original range if other range contains it', () => {
  368. const range = Range.createFromParentsAndOffsets( t2, 0, t3, 0 );
  369. const otherRange = Range.createFromParentsAndOffsets( t1, 1, t3, 1 );
  370. const intersection = range.getIntersection( otherRange );
  371. expect( intersection.isEqual( range ) ).to.be.true;
  372. } );
  373. it( 'should return range equal to other range if it is contained in original range', () => {
  374. const range = Range.createFromParentsAndOffsets( t1, 1, t3, 1 );
  375. const otherRange = Range.createFromParentsAndOffsets( t2, 0, t3, 0 );
  376. const intersection = range.getIntersection( otherRange );
  377. expect( intersection.isEqual( otherRange ) ).to.be.true;
  378. } );
  379. it( 'should return null if ranges do not intersect', () => {
  380. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  381. const otherRange = Range.createFromParentsAndOffsets( t3, 0, t3, 3 );
  382. const intersection = range.getIntersection( otherRange );
  383. expect( intersection ).to.be.null;
  384. } );
  385. it( 'should return common part if ranges intersect partially', () => {
  386. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  387. const otherRange = Range.createFromParentsAndOffsets( t2, 0, t3, 3 );
  388. const intersection = range.getIntersection( otherRange );
  389. expect( intersection.start.parent ).to.equal( t2 );
  390. expect( intersection.start.offset ).to.equal( 0 );
  391. expect( intersection.end.parent ).to.equal( t2 );
  392. expect( intersection.end.offset ).to.equal( 3 );
  393. } );
  394. } );
  395. } );
  396. describe( 'getWalker', () => {
  397. it( 'should be possible to iterate using this method', () => {
  398. const range = getRange( '<p>fo{o</p><p>ba}r</p><p>xyz</p>' );
  399. const values = [];
  400. for ( const value of range.getWalker() ) {
  401. values.push( value );
  402. }
  403. expect( values.length ).to.equal( 4 );
  404. expect( values[ 0 ].item.data ).to.equal( 'o' );
  405. expect( values[ 1 ].item.name ).to.equal( 'p' );
  406. expect( values[ 1 ].type ).to.equal( 'elementEnd' );
  407. expect( values[ 2 ].item.name ).to.equal( 'p' );
  408. expect( values[ 2 ].type ).to.equal( 'elementStart' );
  409. expect( values[ 3 ].item.data ).to.equal( 'ba' );
  410. } );
  411. it( 'should accept TreeWalker options', () => {
  412. const range = getRange( '<p>foo</p><p>b{ar</p><p>xy}z</p>' );
  413. const walker = range.getWalker( { singleCharacters: true, ignoreElementEnd: true } );
  414. const values = [];
  415. for ( const value of walker ) {
  416. values.push( value );
  417. }
  418. expect( walker ).to.be.instanceof( TreeWalker );
  419. expect( walker ).to.have.property( 'singleCharacters' ).that.is.true;
  420. expect( values.length ).to.equal( 5 );
  421. expect( values[ 0 ].item.data ).to.equal( 'a' );
  422. expect( values[ 1 ].item.data ).to.equal( 'r' );
  423. expect( values[ 2 ].item.name ).to.equal( 'p' );
  424. expect( values[ 3 ].item.data ).to.equal( 'x' );
  425. expect( values[ 4 ].item.data ).to.equal( 'y' );
  426. } );
  427. } );
  428. describe( 'getItems', () => {
  429. it( 'should return iterator that iterates over all view items in the range', () => {
  430. const range = getRange( '<p>fo{o</p><p>bar</p><p>xy}z</p>' );
  431. const nodes = [];
  432. for ( const node of range.getItems() ) {
  433. nodes.push( node );
  434. }
  435. expect( nodes.length ).to.equal( 5 );
  436. expect( nodes[ 0 ].data ).to.equal( 'o' );
  437. expect( nodes[ 1 ].name ).to.equal( 'p' );
  438. expect( nodes[ 2 ].data ).to.equal( 'bar' );
  439. expect( nodes[ 3 ].name ).to.equal( 'p' );
  440. expect( nodes[ 4 ].data ).to.equal( 'xy' );
  441. } );
  442. it( 'should accept TreeWalker options', () => {
  443. const range = getRange( '<p>foo</p><p>b{ar</p><p>xy}z</p>' );
  444. const nodes = [];
  445. for ( const node of range.getItems( { singleCharacters: true } ) ) {
  446. nodes.push( node );
  447. }
  448. expect( nodes.length ).to.equal( 5 );
  449. expect( nodes[ 0 ].data ).to.equal( 'a' );
  450. expect( nodes[ 1 ].data ).to.equal( 'r' );
  451. expect( nodes[ 2 ].name ).to.equal( 'p' );
  452. expect( nodes[ 3 ].data ).to.equal( 'x' );
  453. expect( nodes[ 4 ].data ).to.equal( 'y' );
  454. } );
  455. } );
  456. describe( 'getPositions', () => {
  457. it( 'should return iterator that iterates over all positions in the range', () => {
  458. const range = getRange( '<p>fo{o</p><p>b}ar</p><p>xyz</p>' );
  459. const positions = [];
  460. for ( const position of range.getPositions() ) {
  461. positions.push( position );
  462. }
  463. expect( positions.length ).to.equal( 5 );
  464. expect( positions[ 0 ].parent.data ).to.equal( 'foo' ); // Inside text node "foo".
  465. expect( positions[ 0 ].offset ).to.equal( 2 );
  466. expect( positions[ 1 ].parent.name ).to.equal( 'p' ); // At the end of the first P element.
  467. expect( positions[ 1 ].offset ).to.equal( 1 );
  468. expect( positions[ 2 ].parent ).to.be.instanceof( DocumentFragment ); // In document fragment, between Ps.
  469. expect( positions[ 2 ].offset ).to.equal( 1 );
  470. expect( positions[ 3 ].parent.name ).to.equal( 'p' ); // At the beginning of the second P element.
  471. expect( positions[ 3 ].offset ).to.equal( 0 );
  472. expect( positions[ 4 ].parent.data ).to.equal( 'bar' ); // Inside text node "bar".
  473. expect( positions[ 4 ].offset ).to.equal( 1 );
  474. } );
  475. it( 'should accept TreeWalker options', () => {
  476. const range = getRange( '<p>foo</p><p>b{ar</p><p>xy}z</p>' );
  477. const positions = [];
  478. for ( const position of range.getPositions( { singleCharacters: true } ) ) {
  479. positions.push( position );
  480. }
  481. expect( positions.length ).to.equal( 7 );
  482. expect( positions[ 0 ].parent.data ).to.equal( 'bar' ); // "b^ar".
  483. expect( positions[ 0 ].offset ).to.equal( 1 );
  484. expect( positions[ 1 ].parent.data ).to.equal( 'bar' ); // "ba^r".
  485. expect( positions[ 1 ].offset ).to.equal( 2 );
  486. expect( positions[ 2 ].parent.name ).to.equal( 'p' ); // <p>bar^</p> -- at the end of P node.
  487. expect( positions[ 2 ].offset ).to.equal( 1 );
  488. expect( positions[ 3 ].parent ).to.be.instanceof( DocumentFragment ); // "</p>^<p>" -- between P nodes.
  489. expect( positions[ 3 ].offset ).to.equal( 2 );
  490. expect( positions[ 4 ].parent.name ).to.equal( 'p' ); // <p>^xyz</p> -- at the start of P node.
  491. expect( positions[ 4 ].offset ).to.equal( 0 );
  492. expect( positions[ 5 ].parent.data ).to.equal( 'xyz' ); // "x^yz".
  493. expect( positions[ 5 ].offset ).to.equal( 1 );
  494. expect( positions[ 6 ].parent.data ).to.equal( 'xyz' ); // "xy^z".
  495. expect( positions[ 6 ].offset ).to.equal( 2 );
  496. } );
  497. } );
  498. describe( 'static constructors', () => {
  499. let div, p, foz;
  500. beforeEach( () => {
  501. foz = new Text( 'foz' );
  502. p = new Element( 'p', null, foz );
  503. div = new Element( 'div', null, p );
  504. } );
  505. describe( 'createIn', () => {
  506. it( 'should return range', () => {
  507. const range = Range.createIn( p );
  508. expect( range.start.parent ).to.deep.equal( p );
  509. expect( range.start.offset ).to.deep.equal( 0 );
  510. expect( range.end.parent ).to.deep.equal( p );
  511. expect( range.end.offset ).to.deep.equal( 1 );
  512. } );
  513. } );
  514. describe( 'createOn', () => {
  515. it( 'should return range', () => {
  516. const range = Range.createOn( p );
  517. expect( range.start.parent ).to.equal( div );
  518. expect( range.start.offset ).to.equal( 0 );
  519. expect( range.end.parent ).to.equal( div );
  520. expect( range.end.offset ).to.equal( 1 );
  521. } );
  522. } );
  523. describe( 'createFromParentsAndOffsets', () => {
  524. it( 'should return range', () => {
  525. const range = Range.createFromParentsAndOffsets( div, 0, foz, 1 );
  526. expect( range.start.parent ).to.deep.equal( div );
  527. expect( range.start.offset ).to.deep.equal( 0 );
  528. expect( range.end.parent ).to.deep.equal( foz );
  529. expect( range.end.offset ).to.deep.equal( 1 );
  530. } );
  531. } );
  532. describe( 'createFromPositionAndShift', () => {
  533. it( 'should make range from start position and offset', () => {
  534. const position = new Position( foz, 1 );
  535. const range = Range.createFromPositionAndShift( position, 2 );
  536. expect( range ).to.be.instanceof( Range );
  537. expect( range.start.isEqual( position ) ).to.be.true;
  538. expect( range.end.parent ).to.equal( foz );
  539. expect( range.end.offset ).to.deep.equal( 3 );
  540. } );
  541. it( 'should accept negative shift value', () => {
  542. const position = new Position( foz, 3 );
  543. const range = Range.createFromPositionAndShift( position, -1 );
  544. expect( range ).to.be.instanceof( Range );
  545. expect( range.end.isEqual( position ) ).to.be.true;
  546. expect( range.start.parent ).to.equal( foz );
  547. expect( range.start.offset ).to.deep.equal( 2 );
  548. } );
  549. } );
  550. describe( 'createFromRange', () => {
  551. it( 'should create a new instance of Range that is equal to passed range', () => {
  552. const range = new Range( new Position( foz, 0 ), new Position( foz, 2 ) );
  553. const clone = Range.createFromRange( range );
  554. expect( clone ).not.to.be.equal( range ); // clone is not pointing to the same object as position
  555. expect( clone.isEqual( range ) ).to.be.true; // but they are equal in the position-sense
  556. } );
  557. } );
  558. } );
  559. describe( 'getCommonAncestor()', () => {
  560. it( 'should return common ancestor for positions from Range', () => {
  561. const foz = new Text( 'foz' );
  562. const bar = new Text( 'bar' );
  563. const li1 = new Element( 'li', null, foz );
  564. const li2 = new Element( 'li', null, bar );
  565. const ul = new Element( 'ul', null, [ li1, li2 ] );
  566. const range = new Range( new Position( li1, 0 ), new Position( li2, 2 ) );
  567. expect( range.getCommonAncestor() ).to.equal( ul );
  568. } );
  569. } );
  570. } );