range.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /**
  2. * @license Copyright (c) 2003-2018, 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. // As above.
  158. it( 'case 11', () => {
  159. expect( trim( '<p><b></b><b>[]</b><b></b></p>' ) )
  160. .to.equal( '<p><b></b><b></b><b></b>[]</p>' );
  161. } );
  162. function trim( data ) {
  163. data = data
  164. .replace( /<p>/g, '<container:p>' )
  165. .replace( /<\/p>/g, '</container:p>' )
  166. .replace( /<b>/g, '<attribute:b>' )
  167. .replace( /<\/b>/g, '</attribute:b>' )
  168. .replace( /<img><\/img>/g, '<empty:img></empty:img>' )
  169. .replace( /<span><\/span>/g, '<ui:span></ui:span>' );
  170. const viewFrag = new DocumentFragment();
  171. const { view, selection } = parse( data, { rootElement: viewFrag } );
  172. const range = selection.getFirstRange();
  173. const trimmedRange = range.getTrimmed();
  174. return stringify( view, trimmedRange );
  175. }
  176. } );
  177. describe( 'isEqual', () => {
  178. it( 'should return true for the same range', () => {
  179. const start = new Position( {}, 1 );
  180. const end = new Position( {}, 2 );
  181. const range = new Range( start, end );
  182. expect( range.isEqual( range ) ).to.be.true;
  183. } );
  184. it( 'should return true for ranges with same start and end positions', () => {
  185. const start = new Position( {}, 1 );
  186. const end = new Position( {}, 2 );
  187. const range1 = new Range( start, end );
  188. const range2 = new Range( start, end );
  189. expect( range1.isEqual( range2 ) ).to.be.true;
  190. } );
  191. it( 'should return false if start position is different', () => {
  192. const start1 = new Position( {}, 1 );
  193. const start2 = new Position( {}, 1 );
  194. const end = new Position( {}, 2 );
  195. const range1 = new Range( start1, end );
  196. const range2 = new Range( start2, end );
  197. expect( range1.isEqual( range2 ) ).to.be.false;
  198. } );
  199. it( 'should return false if end position is different', () => {
  200. const start = new Position( {}, 1 );
  201. const end1 = new Position( {}, 2 );
  202. const end2 = new Position( {}, 2 );
  203. const range1 = new Range( start, end1 );
  204. const range2 = new Range( start, end2 );
  205. expect( range1.isEqual( range2 ) ).to.be.false;
  206. } );
  207. it( 'should return false for ranges with same root and different offsets', () => {
  208. const mockObject = {};
  209. const range1 = new Range( new Position( mockObject, 0 ), new Position( mockObject, 10 ) );
  210. const range2 = new Range( new Position( mockObject, 2 ), new Position( mockObject, 10 ) );
  211. expect( range1.isEqual( range2 ) ).to.be.false;
  212. } );
  213. } );
  214. describe( 'containsPosition', () => {
  215. let viewRoot, range;
  216. beforeEach( () => {
  217. viewRoot = new Element( 'div' );
  218. range = getRange( '<p>fo{o</p><p>bar</p><p>xy}z</p>', { rootElement: viewRoot } );
  219. } );
  220. it( 'should return false if position is before range', () => {
  221. const position = new Position( viewRoot.getChild( 0 ).getChild( 0 ), 1 ); // After "f".
  222. expect( range.containsPosition( position ) ).to.be.false;
  223. } );
  224. it( 'should return false if position is after range', () => {
  225. const position = new Position( viewRoot.getChild( 2 ).getChild( 0 ), 3 ); // After "z".
  226. expect( range.containsPosition( position ) ).to.be.false;
  227. } );
  228. it( 'should return true if position is inside range', () => {
  229. const position = new Position( viewRoot.getChild( 1 ).getChild( 0 ), 1 ); // After "b".
  230. expect( range.containsPosition( position ) ).to.be.true;
  231. } );
  232. } );
  233. describe( 'containsRange', () => {
  234. let viewRoot, range, beforeF, afterF, beforeB, afterX;
  235. beforeEach( () => {
  236. viewRoot = new Element( 'div' );
  237. range = getRange( '<p>fo{o</p><p>bar</p><p>xy}z</p>', { rootElement: viewRoot } );
  238. beforeF = new Position( viewRoot.getChild( 0 ).getChild( 0 ), 0 );
  239. afterF = new Position( viewRoot.getChild( 0 ).getChild( 0 ), 1 );
  240. beforeB = new Position( viewRoot.getChild( 1 ).getChild( 0 ), 0 );
  241. afterX = new Position( viewRoot.getChild( 2 ).getChild( 0 ), 1 );
  242. } );
  243. it( 'should return false if ranges do not intersect', () => {
  244. const otherRange = new Range( beforeF, afterF );
  245. expect( range.containsRange( otherRange ) ).to.be.false;
  246. } );
  247. it( 'should return false if ranges intersect but only partially', () => {
  248. const otherRange = new Range( afterF, afterX );
  249. expect( range.containsRange( otherRange ) ).to.be.false;
  250. } );
  251. it( 'should return false if ranges are equal', () => {
  252. const otherRange = Range.createFromRange( range );
  253. expect( range.containsRange( otherRange ) ).to.be.false;
  254. } );
  255. it( 'should return true if given range is inside range', () => {
  256. const otherRange = new Range( beforeB, afterX );
  257. expect( range.containsRange( otherRange ) ).to.be.true;
  258. } );
  259. it( 'should return true if ranges are equal and check is not strict', () => {
  260. const otherRange = Range.createFromRange( range );
  261. expect( range.containsRange( otherRange, true ) ).to.be.true;
  262. } );
  263. it( 'should return true if ranges start at the same position and check is not strict', () => {
  264. const otherRange = new Range( range.start, afterX );
  265. expect( range.containsRange( otherRange, true ) ).to.be.true;
  266. } );
  267. it( 'should return true if ranges end at the same position and check is not strict', () => {
  268. const otherRange = new Range( beforeB, range.end );
  269. expect( range.containsRange( otherRange, true ) ).to.be.true;
  270. } );
  271. it( 'should return false if given range is collapsed and starts or ends at another range boundary', () => {
  272. expect( range.containsRange( new Range( range.start, range.start ) ) ).to.be.false;
  273. expect( range.containsRange( new Range( range.end, range.end ) ) ).to.be.false;
  274. expect( range.containsRange( new Range( range.start, range.start ), true ) ).to.be.false;
  275. expect( range.containsRange( new Range( range.end, range.end ), true ) ).to.be.false;
  276. } );
  277. } );
  278. describe( 'other range interaction', () => {
  279. let root, p1, p2, t1, t2, t3;
  280. // root
  281. // __________|__________
  282. // | |
  283. // ___p1___ p2
  284. // | | |
  285. // t1 t2 t3
  286. beforeEach( () => {
  287. t1 = new Text( 'foo' );
  288. t2 = new Text( 'bar' );
  289. t3 = new Text( 'baz' );
  290. p1 = new Element( 'p', null, [ t1, t2 ] );
  291. p2 = new Element( 'p', null, t3 );
  292. root = new Element( 'div', null, [ p1, p2 ] );
  293. } );
  294. describe( 'isIntersecting', () => {
  295. it( 'should return true if given range is equal', () => {
  296. const range = Range.createFromParentsAndOffsets( t1, 0, t3, 2 );
  297. const otherRange = Range.createFromRange( range );
  298. expect( range.isIntersecting( otherRange ) ).to.be.true;
  299. expect( otherRange.isIntersecting( range ) ).to.be.true;
  300. } );
  301. it( 'should return true if given range contains this range', () => {
  302. const range = Range.createFromParentsAndOffsets( t1, 0, t3, 3 );
  303. const otherRange = Range.createFromParentsAndOffsets( p1, 1, t2, 2 );
  304. expect( range.isIntersecting( otherRange ) ).to.be.true;
  305. expect( otherRange.isIntersecting( range ) ).to.be.true;
  306. } );
  307. it( 'should return true if given range ends in this range', () => {
  308. const range = Range.createFromParentsAndOffsets( root, 1, t3, 3 );
  309. const otherRange = Range.createFromParentsAndOffsets( t1, 0, p2, 0 );
  310. expect( range.isIntersecting( otherRange ) ).to.be.true;
  311. expect( otherRange.isIntersecting( range ) ).to.be.true;
  312. } );
  313. it( 'should return true if given range starts in this range', () => {
  314. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  315. const otherRange = Range.createFromParentsAndOffsets( p1, 1, p2, 0 );
  316. expect( range.isIntersecting( otherRange ) ).to.be.true;
  317. expect( otherRange.isIntersecting( range ) ).to.be.true;
  318. } );
  319. it( 'should return false if given range is fully before/after this range', () => {
  320. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  321. const otherRange = Range.createFromParentsAndOffsets( root, 1, t3, 0 );
  322. expect( range.isIntersecting( otherRange ) ).to.be.false;
  323. expect( otherRange.isIntersecting( range ) ).to.be.false;
  324. } );
  325. it( 'should return false if ranges are in different roots', () => {
  326. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  327. const otherRange = Range.createFromParentsAndOffsets( new Element( 'div' ), 1, t3, 0 );
  328. expect( range.isIntersecting( otherRange ) ).to.be.false;
  329. expect( otherRange.isIntersecting( range ) ).to.be.false;
  330. } );
  331. } );
  332. describe( 'getDifference', () => {
  333. it( 'should return range equal to original range if other range does not intersect with it', () => {
  334. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  335. const otherRange = Range.createFromParentsAndOffsets( root, 1, t3, 0 );
  336. const difference = range.getDifference( otherRange );
  337. expect( difference.length ).to.equal( 1 );
  338. expect( difference[ 0 ].isEqual( range ) ).to.be.true;
  339. } );
  340. it( 'should return shrunken range if other range intersects with it', () => {
  341. const range = Range.createFromParentsAndOffsets( root, 1, t3, 3 );
  342. const otherRange = Range.createFromParentsAndOffsets( t1, 0, p2, 0 );
  343. const difference = range.getDifference( otherRange );
  344. expect( difference.length ).to.equal( 1 );
  345. expect( difference[ 0 ].start.parent ).to.equal( p2 );
  346. expect( difference[ 0 ].start.offset ).to.equal( 0 );
  347. expect( difference[ 0 ].end.parent ).to.equal( t3 );
  348. expect( difference[ 0 ].end.offset ).to.equal( 3 );
  349. } );
  350. it( 'should return an empty array if other range contains or is same as the original range', () => {
  351. const range = Range.createFromParentsAndOffsets( p1, 1, t2, 2 );
  352. const otherRange = Range.createFromParentsAndOffsets( t1, 0, t3, 3 );
  353. const difference = range.getDifference( otherRange );
  354. expect( difference.length ).to.equal( 0 );
  355. } );
  356. it( 'should two ranges if other range is contained by the original range', () => {
  357. const range = Range.createFromParentsAndOffsets( t1, 0, t3, 3 );
  358. const otherRange = Range.createFromParentsAndOffsets( p1, 1, t2, 2 );
  359. const difference = range.getDifference( otherRange );
  360. expect( difference.length ).to.equal( 2 );
  361. expect( difference[ 0 ].start.parent ).to.equal( t1 );
  362. expect( difference[ 0 ].start.offset ).to.equal( 0 );
  363. expect( difference[ 0 ].end.parent ).to.equal( p1 );
  364. expect( difference[ 0 ].end.offset ).to.equal( 1 );
  365. expect( difference[ 1 ].start.parent ).to.equal( t2 );
  366. expect( difference[ 1 ].start.offset ).to.equal( 2 );
  367. expect( difference[ 1 ].end.parent ).to.equal( t3 );
  368. expect( difference[ 1 ].end.offset ).to.equal( 3 );
  369. } );
  370. } );
  371. describe( 'getIntersection', () => {
  372. it( 'should return range equal to original range if other range contains it', () => {
  373. const range = Range.createFromParentsAndOffsets( t2, 0, t3, 0 );
  374. const otherRange = Range.createFromParentsAndOffsets( t1, 1, t3, 1 );
  375. const intersection = range.getIntersection( otherRange );
  376. expect( intersection.isEqual( range ) ).to.be.true;
  377. } );
  378. it( 'should return range equal to other range if it is contained in original range', () => {
  379. const range = Range.createFromParentsAndOffsets( t1, 1, t3, 1 );
  380. const otherRange = Range.createFromParentsAndOffsets( t2, 0, t3, 0 );
  381. const intersection = range.getIntersection( otherRange );
  382. expect( intersection.isEqual( otherRange ) ).to.be.true;
  383. } );
  384. it( 'should return null if ranges do not intersect', () => {
  385. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  386. const otherRange = Range.createFromParentsAndOffsets( t3, 0, t3, 3 );
  387. const intersection = range.getIntersection( otherRange );
  388. expect( intersection ).to.be.null;
  389. } );
  390. it( 'should return common part if ranges intersect partially', () => {
  391. const range = Range.createFromParentsAndOffsets( t1, 0, t2, 3 );
  392. const otherRange = Range.createFromParentsAndOffsets( t2, 0, t3, 3 );
  393. const intersection = range.getIntersection( otherRange );
  394. expect( intersection.start.parent ).to.equal( t2 );
  395. expect( intersection.start.offset ).to.equal( 0 );
  396. expect( intersection.end.parent ).to.equal( t2 );
  397. expect( intersection.end.offset ).to.equal( 3 );
  398. } );
  399. } );
  400. } );
  401. describe( 'getWalker', () => {
  402. it( 'should be possible to iterate using this method', () => {
  403. const range = getRange( '<p>fo{o</p><p>ba}r</p><p>xyz</p>' );
  404. const values = [];
  405. for ( const value of range.getWalker() ) {
  406. values.push( value );
  407. }
  408. expect( values.length ).to.equal( 4 );
  409. expect( values[ 0 ].item.data ).to.equal( 'o' );
  410. expect( values[ 1 ].item.name ).to.equal( 'p' );
  411. expect( values[ 1 ].type ).to.equal( 'elementEnd' );
  412. expect( values[ 2 ].item.name ).to.equal( 'p' );
  413. expect( values[ 2 ].type ).to.equal( 'elementStart' );
  414. expect( values[ 3 ].item.data ).to.equal( 'ba' );
  415. } );
  416. it( 'should accept TreeWalker options', () => {
  417. const range = getRange( '<p>foo</p><p>b{ar</p><p>xy}z</p>' );
  418. const walker = range.getWalker( { singleCharacters: true, ignoreElementEnd: true } );
  419. const values = [];
  420. for ( const value of walker ) {
  421. values.push( value );
  422. }
  423. expect( walker ).to.be.instanceof( TreeWalker );
  424. expect( walker ).to.have.property( 'singleCharacters' ).that.is.true;
  425. expect( values.length ).to.equal( 5 );
  426. expect( values[ 0 ].item.data ).to.equal( 'a' );
  427. expect( values[ 1 ].item.data ).to.equal( 'r' );
  428. expect( values[ 2 ].item.name ).to.equal( 'p' );
  429. expect( values[ 3 ].item.data ).to.equal( 'x' );
  430. expect( values[ 4 ].item.data ).to.equal( 'y' );
  431. } );
  432. } );
  433. describe( 'getItems', () => {
  434. it( 'should return iterator that iterates over all view items in the range', () => {
  435. const range = getRange( '<p>fo{o</p><p>bar</p><p>xy}z</p>' );
  436. const nodes = [];
  437. for ( const node of range.getItems() ) {
  438. nodes.push( node );
  439. }
  440. expect( nodes.length ).to.equal( 5 );
  441. expect( nodes[ 0 ].data ).to.equal( 'o' );
  442. expect( nodes[ 1 ].name ).to.equal( 'p' );
  443. expect( nodes[ 2 ].data ).to.equal( 'bar' );
  444. expect( nodes[ 3 ].name ).to.equal( 'p' );
  445. expect( nodes[ 4 ].data ).to.equal( 'xy' );
  446. } );
  447. it( 'should accept TreeWalker options', () => {
  448. const range = getRange( '<p>foo</p><p>b{ar</p><p>xy}z</p>' );
  449. const nodes = [];
  450. for ( const node of range.getItems( { singleCharacters: true } ) ) {
  451. nodes.push( node );
  452. }
  453. expect( nodes.length ).to.equal( 5 );
  454. expect( nodes[ 0 ].data ).to.equal( 'a' );
  455. expect( nodes[ 1 ].data ).to.equal( 'r' );
  456. expect( nodes[ 2 ].name ).to.equal( 'p' );
  457. expect( nodes[ 3 ].data ).to.equal( 'x' );
  458. expect( nodes[ 4 ].data ).to.equal( 'y' );
  459. } );
  460. } );
  461. describe( 'getPositions', () => {
  462. it( 'should return iterator that iterates over all positions in the range', () => {
  463. const range = getRange( '<p>fo{o</p><p>b}ar</p><p>xyz</p>' );
  464. const positions = [];
  465. for ( const position of range.getPositions() ) {
  466. positions.push( position );
  467. }
  468. expect( positions.length ).to.equal( 5 );
  469. expect( positions[ 0 ].parent.data ).to.equal( 'foo' ); // Inside text node "foo".
  470. expect( positions[ 0 ].offset ).to.equal( 2 );
  471. expect( positions[ 1 ].parent.name ).to.equal( 'p' ); // At the end of the first P element.
  472. expect( positions[ 1 ].offset ).to.equal( 1 );
  473. expect( positions[ 2 ].parent ).to.be.instanceof( DocumentFragment ); // In document fragment, between Ps.
  474. expect( positions[ 2 ].offset ).to.equal( 1 );
  475. expect( positions[ 3 ].parent.name ).to.equal( 'p' ); // At the beginning of the second P element.
  476. expect( positions[ 3 ].offset ).to.equal( 0 );
  477. expect( positions[ 4 ].parent.data ).to.equal( 'bar' ); // Inside text node "bar".
  478. expect( positions[ 4 ].offset ).to.equal( 1 );
  479. } );
  480. it( 'should accept TreeWalker options', () => {
  481. const range = getRange( '<p>foo</p><p>b{ar</p><p>xy}z</p>' );
  482. const positions = [];
  483. for ( const position of range.getPositions( { singleCharacters: true } ) ) {
  484. positions.push( position );
  485. }
  486. expect( positions.length ).to.equal( 7 );
  487. expect( positions[ 0 ].parent.data ).to.equal( 'bar' ); // "b^ar".
  488. expect( positions[ 0 ].offset ).to.equal( 1 );
  489. expect( positions[ 1 ].parent.data ).to.equal( 'bar' ); // "ba^r".
  490. expect( positions[ 1 ].offset ).to.equal( 2 );
  491. expect( positions[ 2 ].parent.name ).to.equal( 'p' ); // <p>bar^</p> -- at the end of P node.
  492. expect( positions[ 2 ].offset ).to.equal( 1 );
  493. expect( positions[ 3 ].parent ).to.be.instanceof( DocumentFragment ); // "</p>^<p>" -- between P nodes.
  494. expect( positions[ 3 ].offset ).to.equal( 2 );
  495. expect( positions[ 4 ].parent.name ).to.equal( 'p' ); // <p>^xyz</p> -- at the start of P node.
  496. expect( positions[ 4 ].offset ).to.equal( 0 );
  497. expect( positions[ 5 ].parent.data ).to.equal( 'xyz' ); // "x^yz".
  498. expect( positions[ 5 ].offset ).to.equal( 1 );
  499. expect( positions[ 6 ].parent.data ).to.equal( 'xyz' ); // "xy^z".
  500. expect( positions[ 6 ].offset ).to.equal( 2 );
  501. } );
  502. } );
  503. describe( 'static constructors', () => {
  504. let div, p, foz;
  505. beforeEach( () => {
  506. foz = new Text( 'foz' );
  507. p = new Element( 'p', null, foz );
  508. div = new Element( 'div', null, p );
  509. } );
  510. describe( 'createIn', () => {
  511. it( 'should return range', () => {
  512. const range = Range.createIn( p );
  513. expect( range.start.parent ).to.deep.equal( p );
  514. expect( range.start.offset ).to.deep.equal( 0 );
  515. expect( range.end.parent ).to.deep.equal( p );
  516. expect( range.end.offset ).to.deep.equal( 1 );
  517. } );
  518. } );
  519. describe( 'createOn', () => {
  520. it( 'should return range', () => {
  521. const range = Range.createOn( p );
  522. expect( range.start.parent ).to.equal( div );
  523. expect( range.start.offset ).to.equal( 0 );
  524. expect( range.end.parent ).to.equal( div );
  525. expect( range.end.offset ).to.equal( 1 );
  526. } );
  527. } );
  528. describe( 'createCollapsedAt()', () => {
  529. it( 'should return new collapsed range at the given item position', () => {
  530. const item = new Element( 'p', null, new Text( 'foo' ) );
  531. const range = Range.createCollapsedAt( item );
  532. expect( range.start.parent ).to.equal( item );
  533. expect( range.start.offset ).to.equal( 0 );
  534. expect( range.isCollapsed ).to.be.true;
  535. } );
  536. it( 'should return new collapse range at the given item position and offset', () => {
  537. const item = new Element( 'p', null, new Text( 'foo' ) );
  538. const range = Range.createCollapsedAt( item, 1 );
  539. expect( range.start.parent ).to.equal( item );
  540. expect( range.start.offset ).to.equal( 1 );
  541. expect( range.isCollapsed ).to.be.true;
  542. } );
  543. } );
  544. describe( 'createFromParentsAndOffsets', () => {
  545. it( 'should return range', () => {
  546. const range = Range.createFromParentsAndOffsets( div, 0, foz, 1 );
  547. expect( range.start.parent ).to.deep.equal( div );
  548. expect( range.start.offset ).to.deep.equal( 0 );
  549. expect( range.end.parent ).to.deep.equal( foz );
  550. expect( range.end.offset ).to.deep.equal( 1 );
  551. } );
  552. } );
  553. describe( 'createFromPositionAndShift', () => {
  554. it( 'should make range from start position and offset', () => {
  555. const position = new Position( foz, 1 );
  556. const range = Range.createFromPositionAndShift( position, 2 );
  557. expect( range ).to.be.instanceof( Range );
  558. expect( range.start.isEqual( position ) ).to.be.true;
  559. expect( range.end.parent ).to.equal( foz );
  560. expect( range.end.offset ).to.deep.equal( 3 );
  561. } );
  562. it( 'should accept negative shift value', () => {
  563. const position = new Position( foz, 3 );
  564. const range = Range.createFromPositionAndShift( position, -1 );
  565. expect( range ).to.be.instanceof( Range );
  566. expect( range.end.isEqual( position ) ).to.be.true;
  567. expect( range.start.parent ).to.equal( foz );
  568. expect( range.start.offset ).to.deep.equal( 2 );
  569. } );
  570. } );
  571. describe( 'createFromRange', () => {
  572. it( 'should create a new instance of Range that is equal to passed range', () => {
  573. const range = new Range( new Position( foz, 0 ), new Position( foz, 2 ) );
  574. const clone = Range.createFromRange( range );
  575. expect( clone ).not.to.be.equal( range ); // clone is not pointing to the same object as position
  576. expect( clone.isEqual( range ) ).to.be.true; // but they are equal in the position-sense
  577. } );
  578. } );
  579. } );
  580. describe( 'getCommonAncestor()', () => {
  581. it( 'should return common ancestor for positions from Range', () => {
  582. const foz = new Text( 'foz' );
  583. const bar = new Text( 'bar' );
  584. const li1 = new Element( 'li', null, foz );
  585. const li2 = new Element( 'li', null, bar );
  586. const ul = new Element( 'ul', null, [ li1, li2 ] );
  587. const range = new Range( new Position( li1, 0 ), new Position( li2, 2 ) );
  588. expect( range.getCommonAncestor() ).to.equal( ul );
  589. } );
  590. } );
  591. } );