8
0

range.js 25 KB

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