8
0

range.js 27 KB

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