range.js 26 KB

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