8
0

range.js 27 KB

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