range.js 28 KB

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