8
0

range.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import Range from '/ckeditor5/engine/treemodel/range.js';
  8. import Position from '/ckeditor5/engine/treemodel/position.js';
  9. import Element from '/ckeditor5/engine/treemodel/element.js';
  10. import Text from '/ckeditor5/engine/treemodel/text.js';
  11. import Document from '/ckeditor5/engine/treemodel/document.js';
  12. import TreeWalker from '/ckeditor5/engine/treemodel/treewalker.js';
  13. describe( 'Range', () => {
  14. let range, start, end, root, otherRoot;
  15. beforeEach( () => {
  16. let doc = new Document();
  17. root = doc.createRoot( 'root' );
  18. otherRoot = doc.createRoot( 'otherRoot' );
  19. start = new Position( root, [ 1 ] );
  20. end = new Position( root, [ 2 ] );
  21. range = new Range( start, end );
  22. } );
  23. describe( 'constructor', () => {
  24. it( 'should create a range with given positions', () => {
  25. expect( range.start.isEqual( start ) ).to.be.true;
  26. expect( range.end.isEqual( end ) ).to.be.true;
  27. } );
  28. } );
  29. describe( 'root', () => {
  30. it( 'should be equal to start position root', () => {
  31. expect( range.root ).to.equal( start.root );
  32. } );
  33. } );
  34. describe( 'isCollapsed', () => {
  35. it( 'should be true if range start and end positions are equal', () => {
  36. let collapsedRange = new Range( start, start );
  37. expect( collapsedRange.isCollapsed ).to.be.true;
  38. } );
  39. it( 'should be false if range start and end positions are not equal', () => {
  40. expect( range.isCollapsed ).to.be.false;
  41. } );
  42. } );
  43. describe( 'isEqual', () => {
  44. it( 'should return true if the ranges are the same', () => {
  45. let sameStart = Position.createFromPosition( start );
  46. let sameEnd = Position.createFromPosition( end );
  47. let sameRange = new Range( sameStart, sameEnd );
  48. expect( range.isEqual( sameRange ) ).to.be.true;
  49. } );
  50. it( 'should return false if the start position is different', () => {
  51. let range = new Range( start, end );
  52. let diffStart = new Position( root, [ 0 ] );
  53. let sameEnd = Position.createFromPosition( end );
  54. let diffRange = new Range( diffStart, sameEnd );
  55. expect( range.isEqual( diffRange ) ).to.be.false;
  56. } );
  57. it( 'should return false if the end position is different', () => {
  58. let sameStart = new Position( root, [ 0 ] );
  59. let diffEnd = new Position( root, [ 0 ] );
  60. let diffRange = new Range( sameStart, diffEnd );
  61. expect( range.isEqual( diffRange ) ).to.be.false;
  62. } );
  63. it( 'should return false if ranges are in different roots', () => {
  64. let otherRootStart = new Position( otherRoot, start.path.slice() );
  65. let otherRootEnd = new Position( otherRoot, end.path.slice() );
  66. let otherRootRange = new Range( otherRootStart, otherRootEnd );
  67. expect( range.isEqual( otherRootRange ) ).to.be.false;
  68. } );
  69. } );
  70. describe( 'isIntersecting', () => {
  71. it( 'should return true if given range is equal', () => {
  72. let otherRange = Range.createFromRange( range );
  73. expect( range.isIntersecting( otherRange ) ).to.be.true;
  74. } );
  75. it( 'should return true if given range contains this range', () => {
  76. let otherRange = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  77. expect( range.isIntersecting( otherRange ) ).to.be.true;
  78. } );
  79. it( 'should return true if given range ends in this range', () => {
  80. let otherRange = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1, 4 ] ) );
  81. expect( range.isIntersecting( otherRange ) ).to.be.true;
  82. } );
  83. it( 'should return true if given range starts in this range', () => {
  84. let otherRange = new Range( new Position( root, [ 1, 4 ] ), new Position( root, [ 3 ] ) );
  85. expect( range.isIntersecting( otherRange ) ).to.be.true;
  86. } );
  87. it( 'should return false if given range is fully before this range', () => {
  88. let otherRange = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  89. expect( range.isIntersecting( otherRange ) ).to.be.false;
  90. } );
  91. it( 'should return false if given range is fully after this range', () => {
  92. let otherRange = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 0 ] ) );
  93. expect( range.isIntersecting( otherRange ) ).to.be.false;
  94. } );
  95. it( 'should return false if ranges are in different roots', () => {
  96. let otherRange = new Range( new Position( otherRoot, [ 0 ] ), new Position( otherRoot, [ 1, 4 ] ) );
  97. expect( range.isIntersecting( otherRange ) ).to.be.false;
  98. } );
  99. } );
  100. describe( 'static constructors', () => {
  101. let p, f, o, z;
  102. // root
  103. // |- p
  104. // |- f
  105. // |- o
  106. // |- z
  107. beforeEach( () => {
  108. f = new Text( 'f' );
  109. o = new Text( 'o' );
  110. z = new Text( 'z' );
  111. p = new Element( 'p', [], [ f, o, z ] );
  112. root.insertChildren( 0, [ p ] );
  113. } );
  114. describe( 'createFromElement', () => {
  115. it( 'should return range', () => {
  116. const range = Range.createFromElement( p );
  117. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  118. expect( range.end.path ).to.deep.equal( [ 0, 3 ] );
  119. } );
  120. } );
  121. describe( 'createOnElement', () => {
  122. it( 'should return range', () => {
  123. const range = Range.createOnElement( p );
  124. expect( range.start.path ).to.deep.equal( [ 0 ] );
  125. expect( range.end.path ).to.deep.equal( [ 0, 0 ] );
  126. } );
  127. } );
  128. describe( 'createFromParentsAndOffsets', () => {
  129. it( 'should return range', () => {
  130. const range = Range.createFromParentsAndOffsets( root, 0, p, 2 );
  131. expect( range.start.path ).to.deep.equal( [ 0 ] );
  132. expect( range.end.path ).to.deep.equal( [ 0, 2 ] );
  133. } );
  134. } );
  135. describe( 'createFromPositionAndShift', () => {
  136. it( 'should make range from start position and offset', () => {
  137. const position = new Position( root, [ 1, 2, 3 ] );
  138. const range = Range.createFromPositionAndShift( position, 4 );
  139. expect( range ).to.be.instanceof( Range );
  140. expect( range.start.isEqual( position ) ).to.be.true;
  141. expect( range.end.root ).to.equal( range.start.root );
  142. expect( range.end.path ).to.deep.equal( [ 1, 2, 7 ] );
  143. } );
  144. } );
  145. describe( 'createFromRange', () => {
  146. it( 'should create a new instance of Range that is equal to passed range', () => {
  147. const clone = Range.createFromRange( range );
  148. expect( clone ).not.to.be.equal( range ); // clone is not pointing to the same object as position
  149. expect( clone.isEqual( range ) ).to.be.true; // but they are equal in the position-sense
  150. } );
  151. } );
  152. } );
  153. describe( 'iterator', () => {
  154. it( 'should merge characters with same attributes', () => {
  155. prepareRichRoot( root );
  156. let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
  157. let nodes = Array.from( range ).map( value => value.item );
  158. let lengths = Array.from( range ).map( value => value.length );
  159. let nodeNames = mapNodesToNames( nodes );
  160. expect( nodeNames ).to.deep.equal(
  161. [ 'T:st', 'E:p', 'T:lorem ipsum', 'E:p', 'T:foo', 'E:p', 'T:bar', 'E:div', 'E:h', 'T:se' ] );
  162. expect( lengths ).to.deep.equal( [ 2, 1, 11, 1, 3, 1, 3, 1, 1, 2 ] );
  163. } );
  164. } );
  165. describe( 'getWalker', () => {
  166. it( 'should be possible to iterate using this method', () => {
  167. prepareRichRoot( root );
  168. const range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
  169. const items = [];
  170. const walker = range.getWalker();
  171. for ( let value of walker ) {
  172. items.push( value.item );
  173. }
  174. expect( mapNodesToNames( items ) ).to.deep.equal(
  175. [ 'T:st', 'E:h', 'E:p', 'T:lorem ipsum', 'E:p', 'E:div', 'E:p',
  176. 'T:foo', 'E:p', 'E:p', 'T:bar', 'E:p', 'E:div', 'E:h', 'T:se' ] );
  177. } );
  178. it( 'should return treewalker with given options', () => {
  179. prepareRichRoot( root );
  180. const range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
  181. const walker = range.getWalker( { singleCharacters: true } );
  182. expect( walker ).to.be.instanceof( TreeWalker );
  183. expect( walker ).to.have.property( 'singleCharacters' ).that.is.true;
  184. expect( walker ).to.have.property( 'boundaries' ).that.equals( range );
  185. expect( walker ).to.have.property( 'shallow' ).that.is.false;
  186. } );
  187. } );
  188. describe( 'getItems', () => {
  189. it( 'should iterate over all items in the range', () => {
  190. prepareRichRoot( root );
  191. let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
  192. let items = Array.from( range.getItems() );
  193. let nodeNames = mapNodesToNames( items );
  194. expect( nodeNames ).to.deep.equal(
  195. [ 'T:st', 'E:p', 'T:lorem ipsum', 'E:p', 'T:foo', 'E:p', 'T:bar', 'E:div', 'E:h', 'T:se' ] );
  196. } );
  197. it( 'should iterate over all items in the range as single characters', () => {
  198. const a = new Text( 'a' );
  199. const b = new Text( 'b' );
  200. const x = new Text( 'x' );
  201. const y = new Text( 'y' );
  202. const e1 = new Element( 'e1' );
  203. const e2 = new Element( 'e2' );
  204. e1.insertChildren( 0, [ a, b ] );
  205. e2.insertChildren( 0, [ x, y ] );
  206. root.insertChildren( 0, [ e1, e2 ] );
  207. let range = new Range(
  208. new Position( root, [ 0, 1 ] ),
  209. new Position( root, [ 1, 1 ] )
  210. );
  211. let items = Array.from( range.getItems( { singleCharacters: true } ) );
  212. expect( items.length ).to.equal( 3 );
  213. expect( items[ 0 ].character ).to.equal( 'b' );
  214. expect( items[ 1 ] ).to.equal( e2 );
  215. expect( items[ 2 ].character ).to.equal( 'x' );
  216. } );
  217. } );
  218. describe( 'getPositions', () => {
  219. beforeEach( () => {
  220. prepareRichRoot( root );
  221. } );
  222. it( 'should iterate over all positions in this range', () => {
  223. let expectedPaths = [
  224. [ 1, 2 ], [ 1, 3 ],
  225. [ 2 ], [ 2, 0 ], [ 2, 3 ],
  226. [ 3 ], [ 3, 0 ], [ 3, 0, 0 ], [ 3, 0, 2 ]
  227. ];
  228. let range = new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 3, 0, 2 ] ) );
  229. let i = 0;
  230. for ( let position of range.getPositions() ) {
  231. expect( position.path ).to.deep.equal( expectedPaths[ i ] );
  232. i++;
  233. }
  234. expect( i ).to.equal( expectedPaths.length );
  235. } );
  236. it( 'should return single nodes iterating over all positions in this range', () => {
  237. let expectedPaths = [
  238. [ 1, 2 ], [ 1, 3 ],
  239. [ 2 ], [ 2, 0 ], [ 2, 1 ], [ 2, 2 ], [ 2, 3 ],
  240. [ 3 ], [ 3, 0 ], [ 3, 0, 0 ], [ 3, 0, 1 ], [ 3, 0, 2 ]
  241. ];
  242. let range = new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 3, 0, 2 ] ) );
  243. let i = 0;
  244. for ( let position of range.getPositions( { singleCharacters: true } ) ) {
  245. expect( position.path ).to.deep.equal( expectedPaths[ i ] );
  246. i++;
  247. }
  248. expect( i ).to.equal( expectedPaths.length );
  249. } );
  250. } );
  251. describe( 'containsPosition', () => {
  252. beforeEach( () => {
  253. range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) );
  254. } );
  255. it( 'should return false if position is before range', () => {
  256. const position = new Position( root, [ 0, 4 ] );
  257. expect( range.containsPosition( position ) ).to.be.false;
  258. } );
  259. it( 'should return false if position is after range', () => {
  260. const position = new Position( root, [ 3, 0 ] );
  261. expect( range.containsPosition( position ) ).to.be.false;
  262. } );
  263. it( 'should return true if position is inside range', () => {
  264. const position = new Position( root, [ 2, 2 ] );
  265. expect( range.containsPosition( position ) ).to.be.true;
  266. } );
  267. } );
  268. describe( 'getTransformedByInsertion', () => {
  269. it( 'should return an array of Range objects', () => {
  270. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  271. const transformed = range.getTransformedByInsertion( new Position( root, [ 2 ] ), 2 );
  272. expect( transformed ).to.be.instanceof( Array );
  273. expect( transformed[ 0 ] ).to.be.instanceof( Range );
  274. } );
  275. it( 'should update it\'s positions offsets if insertion is before range and they are affected', () => {
  276. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 4 ] ) );
  277. const transformed = range.getTransformedByInsertion( new Position( root, [ 3, 1 ] ), 2 );
  278. expect( transformed[ 0 ].start.offset ).to.equal( 4 );
  279. expect( transformed[ 0 ].end.offset ).to.equal( 6 );
  280. } );
  281. it( 'should update it\'s positions paths if insertion is before range and they are affected', () => {
  282. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  283. const transformed = range.getTransformedByInsertion( new Position( root, [ 0 ] ), 2 );
  284. expect( transformed[ 0 ].start.path[ 0 ] ).to.equal( 5 );
  285. expect( transformed[ 0 ].end.path[ 0 ] ).to.equal( 6 );
  286. } );
  287. it( 'should expand range if insertion was in the middle of range', () => {
  288. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  289. const transformed = range.getTransformedByInsertion( new Position( root, [ 5, 0 ] ), 4 );
  290. expect( transformed.length ).to.equal( 1 );
  291. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  292. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 5, 8 ] );
  293. } );
  294. it( 'should return array with two ranges if insertion was in the middle of range and spread flag was set', () => {
  295. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  296. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 1, 6 ] ), 4, true );
  297. expect( transformed.length ).to.equal( 2 );
  298. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  299. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 1, 6 ] );
  300. expect( transformed[ 1 ].start.path ).to.deep.equal( [ 4, 1, 10 ] );
  301. expect( transformed[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  302. } );
  303. it( 'should not expand range if insertion is equal to start boundary of the range', () => {
  304. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 8 ] ) );
  305. const transformed = range.getTransformedByInsertion( new Position( root, [ 3, 2 ] ), 3 );
  306. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 5 ] );
  307. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 3, 11 ] );
  308. } );
  309. it( 'should expand range if insertion is equal to start boundary of the range and sticky flag is set', () => {
  310. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 8 ] ) );
  311. const transformed = range.getTransformedByInsertion( new Position( root, [ 3, 2 ] ), 3, false, true );
  312. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  313. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 3, 11 ] );
  314. } );
  315. it( 'should not update positions if insertion is before range (but not equal to the start boundary)', () => {
  316. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 8 ] ) );
  317. const transformed = range.getTransformedByInsertion( new Position( root, [ 3, 1 ] ), 3 );
  318. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 5 ] );
  319. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 3, 11 ] );
  320. } );
  321. it( 'should not expand range if insertion is equal to end boundary of the range', () => {
  322. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  323. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 4 ] ), 3 );
  324. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  325. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 4 ] );
  326. } );
  327. it( 'should expand range if insertion is equal to end boundary of the range and sticky flag is set', () => {
  328. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  329. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 4 ] ), 3, false, true );
  330. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  331. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 7 ] );
  332. } );
  333. it( 'should not update positions if insertion is after range (but not equal to the end boundary)', () => {
  334. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  335. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 5 ] ), 3 );
  336. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  337. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 4 ] );
  338. } );
  339. it( 'should move after inserted nodes if the range is collapsed', () => {
  340. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 2 ] ) );
  341. const transformed = range.getTransformedByInsertion( new Position( root, [ 3, 2 ] ), 3 );
  342. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 5 ] );
  343. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 3, 5 ] );
  344. } );
  345. } );
  346. describe( 'getTransformedByMove', () => {
  347. it( 'should return an array of Range objects', () => {
  348. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  349. const transformed = range.getTransformedByMove( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ), 2 );
  350. expect( transformed ).to.be.instanceof( Array );
  351. expect( transformed[ 0 ] ).to.be.instanceof( Range );
  352. } );
  353. it( 'should update it\'s positions offsets if target is before range and they are affected', () => {
  354. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 4 ] ) );
  355. const transformed = range.getTransformedByMove( new Position( root, [ 8, 1 ] ), new Position( root, [ 3, 1 ] ), 2 );
  356. expect( transformed[ 0 ].start.offset ).to.equal( 4 );
  357. expect( transformed[ 0 ].end.offset ).to.equal( 6 );
  358. } );
  359. it( 'should update it\'s positions paths if target is before range and they are affected', () => {
  360. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  361. const transformed = range.getTransformedByMove( new Position( root, [ 8 ] ), new Position( root, [ 0 ] ), 2 );
  362. expect( transformed[ 0 ].start.path[ 0 ] ).to.equal( 5 );
  363. expect( transformed[ 0 ].end.path[ 0 ] ).to.equal( 6 );
  364. } );
  365. it( 'should expand range if target was in the middle of range', () => {
  366. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  367. const transformed = range.getTransformedByMove( new Position( root, [ 8 ] ), new Position( root, [ 5, 0 ] ), 4 );
  368. expect( transformed.length ).to.equal( 1 );
  369. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  370. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 5, 8 ] );
  371. } );
  372. it( 'should not expand range if insertion is equal to start boundary of the range', () => {
  373. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 8 ] ) );
  374. const transformed = range.getTransformedByMove( new Position( root, [ 8, 2 ] ), new Position( root, [ 3, 2 ] ), 3 );
  375. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 5 ] );
  376. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 3, 11 ] );
  377. } );
  378. it( 'should not expand range if insertion is equal to end boundary of the range', () => {
  379. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  380. const transformed = range.getTransformedByMove( new Position( root, [ 8, 4 ] ), new Position( root, [ 4, 4 ] ), 3 );
  381. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  382. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 4 ] );
  383. } );
  384. it( 'should update it\'s positions offsets if source is before range and they are affected', () => {
  385. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 4 ] ) );
  386. const transformed = range.getTransformedByMove( new Position( root, [ 3, 0 ] ), new Position( root, [ 8, 1 ] ), 2 );
  387. expect( transformed[ 0 ].start.offset ).to.equal( 0 );
  388. expect( transformed[ 0 ].end.offset ).to.equal( 2 );
  389. } );
  390. it( 'should update it\'s positions paths if source is before range and they are affected', () => {
  391. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  392. const transformed = range.getTransformedByMove( new Position( root, [ 0 ] ), new Position( root, [ 8 ] ), 2 );
  393. expect( transformed[ 0 ].start.path[ 0 ] ).to.equal( 1 );
  394. expect( transformed[ 0 ].end.path[ 0 ] ).to.equal( 2 );
  395. } );
  396. it( 'should shrink range if source was in the middle of range', () => {
  397. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  398. const transformed = range.getTransformedByMove( new Position( root, [ 5, 0 ] ), new Position( root, [ 8 ] ), 4 );
  399. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  400. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 5, 0 ] );
  401. } );
  402. it( 'should shrink range if source contained range start position', () => {
  403. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  404. const transformed = range.getTransformedByMove( new Position( root, [ 3, 1 ] ), new Position( root, [ 8 ] ), 2 );
  405. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 1 ] );
  406. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 5, 4 ] );
  407. } );
  408. it( 'should shrink range if source contained range end position', () => {
  409. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  410. const transformed = range.getTransformedByMove( new Position( root, [ 5, 3 ] ), new Position( root, [ 8 ] ), 2 );
  411. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  412. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 5, 3 ] );
  413. } );
  414. it( 'should move range if it was contained in moved range', () => {
  415. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 7 ] ) );
  416. const transformed = range.getTransformedByMove( new Position( root, [ 3 ] ), new Position( root, [ 6 ] ), 2 );
  417. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 6, 2 ] );
  418. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 6, 7 ] );
  419. } );
  420. } );
  421. describe( 'getDifference', () => {
  422. let range;
  423. beforeEach( () => {
  424. range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  425. } );
  426. it( 'should return an array of Range objects', () => {
  427. const otherRange = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  428. const diff = range.getDifference( otherRange );
  429. expect( diff ).to.be.instanceof( Array );
  430. expect( diff[ 0 ] ).to.be.instanceof( Range );
  431. } );
  432. it( 'should return original range if other range does not intersect with it', () => {
  433. const otherRange = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  434. const diff = range.getDifference( otherRange );
  435. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  436. expect( diff[ 0 ].end.path ).to.deep.equal( [ 5, 4 ] );
  437. } );
  438. it( 'should return shrunken range if other range intersects with it', () => {
  439. const otherRange = new Range( new Position( root, [ 4, 1 ] ), new Position( root, [ 7 ] ) );
  440. const diff = range.getDifference( otherRange );
  441. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  442. expect( diff[ 0 ].end.path ).to.deep.equal( [ 4, 1 ] );
  443. } );
  444. it( 'should return an empty array if other range contains or is same as the original range', () => {
  445. const otherRange = new Range( new Position( root, [ 2 ] ), new Position( root, [ 6 ] ) );
  446. const diff = range.getDifference( otherRange );
  447. expect( diff.length ).to.equal( 0 );
  448. } );
  449. it( 'should two ranges if other range is contained by the original range', () => {
  450. const otherRange = new Range( new Position( root, [ 3, 7 ] ), new Position( root, [ 4, 1 ] ) );
  451. const diff = range.getDifference( otherRange );
  452. expect( diff.length ).to.equal( 2 );
  453. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  454. expect( diff[ 0 ].end.path ).to.deep.equal( [ 3, 7 ] );
  455. expect( diff[ 1 ].start.path ).to.deep.equal( [ 4, 1 ] );
  456. expect( diff[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  457. } );
  458. } );
  459. describe( 'getIntersection', () => {
  460. let range;
  461. beforeEach( () => {
  462. range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  463. } );
  464. it( 'should return null if ranges do not intersect', () => {
  465. const otherRange = new Range( new Position( root, [ 5, 4 ] ), new Position( root, [ 7 ] ) );
  466. const common = range.getIntersection( otherRange );
  467. expect( common ).to.be.null;
  468. } );
  469. it( 'should return a range equal to the common part of ranges - original range contains the other range', () => {
  470. const otherRange = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  471. const common = range.getIntersection( otherRange );
  472. expect( common.isEqual( otherRange ) ).to.be.true;
  473. } );
  474. it( 'should return a range equal to the common part of ranges - original range is contained by the other range', () => {
  475. const otherRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6 ] ) );
  476. const common = range.getIntersection( otherRange );
  477. expect( common.isEqual( range ) ).to.be.true;
  478. } );
  479. it( 'should return a range equal to the common part of ranges - original range intersects with the other range', () => {
  480. const otherRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 4, 7 ] ) );
  481. const common = range.getIntersection( otherRange );
  482. expect( common.start.path ).to.deep.equal( [ 3, 2 ] );
  483. expect( common.end.path ).to.deep.equal( [ 4, 7 ] );
  484. } );
  485. } );
  486. describe( 'getMinimalFlatRanges', () => {
  487. beforeEach( () => {
  488. prepareRichRoot( root );
  489. } );
  490. it( 'should return empty array if range is collapsed', () => {
  491. let range = new Range( new Position( root, [ 1, 3 ] ), new Position( root, [ 1, 3 ] ) );
  492. let flat = range.getMinimalFlatRanges();
  493. expect( flat.length ).to.equal( 0 );
  494. } );
  495. it( 'should return empty array if range does not contain any node', () => {
  496. let range = new Range( new Position( root, [ 1, 3 ] ), new Position( root, [ 2, 0 ] ) );
  497. let flat = range.getMinimalFlatRanges();
  498. expect( flat.length ).to.equal( 0 );
  499. } );
  500. it( 'should return a minimal set of flat ranges that covers the range (start and end in different sub-trees)', () => {
  501. let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
  502. let flat = range.getMinimalFlatRanges();
  503. expect( flat.length ).to.equal( 4 );
  504. expect( flat[ 0 ].start.path ).to.deep.equal( [ 0, 0, 3 ] );
  505. expect( flat[ 0 ].end.path ).to.deep.equal( [ 0, 0, 5 ] );
  506. expect( flat[ 1 ].start.path ).to.deep.equal( [ 0, 1 ] );
  507. expect( flat[ 1 ].end.path ).to.deep.equal( [ 0, 2 ] );
  508. expect( flat[ 2 ].start.path ).to.deep.equal( [ 1 ] );
  509. expect( flat[ 2 ].end.path ).to.deep.equal( [ 3 ] );
  510. expect( flat[ 3 ].start.path ).to.deep.equal( [ 3, 0, 0 ] );
  511. expect( flat[ 3 ].end.path ).to.deep.equal( [ 3, 0, 2 ] );
  512. } );
  513. it( 'should return a minimal set of flat ranges that covers the range (start.path is prefix of end.path)', () => {
  514. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 0, 1, 4 ] ) );
  515. let flat = range.getMinimalFlatRanges();
  516. expect( flat.length ).to.equal( 2 );
  517. expect( flat[ 0 ].start.path ).to.deep.equal( [ 0, 0 ] );
  518. expect( flat[ 0 ].end.path ).to.deep.equal( [ 0, 1 ] );
  519. expect( flat[ 1 ].start.path ).to.deep.equal( [ 0, 1, 0 ] );
  520. expect( flat[ 1 ].end.path ).to.deep.equal( [ 0, 1, 4 ] );
  521. } );
  522. } );
  523. describe( 'isFlat', () => {
  524. beforeEach( () => {
  525. prepareRichRoot( root );
  526. } );
  527. it( 'should be true if start and end position are in the same parent', () => {
  528. let range = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 2 ] ) );
  529. expect( range.isFlat ).to.be.true;
  530. } );
  531. it( 'should be false if start and end position are in different parents', () => {
  532. let range = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 3, 0, 1 ] ) );
  533. expect( range.isFlat ).to.be.false;
  534. } );
  535. } );
  536. function mapNodesToNames( nodes ) {
  537. return nodes.map( ( node ) => {
  538. return ( node instanceof Element ) ? 'E:' + node.name : 'T:' + ( node.text || node.character );
  539. } );
  540. }
  541. function prepareRichRoot() {
  542. root.insertChildren( 0, [
  543. new Element( 'div', [], [
  544. new Element( 'h', [], 'first' ),
  545. new Element( 'p', [], 'lorem ipsum' )
  546. ] ),
  547. new Element( 'p', [], 'foo' ),
  548. new Element( 'p', [], 'bar' ),
  549. new Element( 'div', [], [
  550. new Element( 'h', [], 'second' ),
  551. new Element( 'p', [], 'lorem' )
  552. ] )
  553. ] );
  554. }
  555. } );