range.js 23 KB

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