range.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. const modules = bender.amd.require(
  8. 'treemodel/range',
  9. 'treemodel/position',
  10. 'treemodel/element',
  11. 'treemodel/character',
  12. 'treemodel/document'
  13. );
  14. describe( 'Range', () => {
  15. let Range, Position, Element, Character, Document;
  16. before( () => {
  17. Position = modules[ 'treemodel/position' ];
  18. Range = modules[ 'treemodel/range' ];
  19. Element = modules[ 'treemodel/element' ];
  20. Character = modules[ 'treemodel/character' ];
  21. Document = modules[ 'treemodel/document' ];
  22. } );
  23. let range, start, end, root, otherRoot;
  24. beforeEach( () => {
  25. let doc = new Document();
  26. root = doc.createRoot( 'root' );
  27. otherRoot = doc.createRoot( 'otherRoot' );
  28. start = new Position( root, [ 1 ] );
  29. end = new Position( root, [ 2 ] );
  30. range = new Range( start, end );
  31. } );
  32. describe( 'constructor', () => {
  33. it( 'should create a range with given positions', () => {
  34. expect( range.start.isEqual( start ) ).to.be.true;
  35. expect( range.end.isEqual( end ) ).to.be.true;
  36. } );
  37. } );
  38. describe( 'root', () => {
  39. it( 'should be equal to start position root', () => {
  40. expect( range.root ).to.equal( start.root );
  41. } );
  42. } );
  43. describe( 'isCollapsed', () => {
  44. it( 'should be true if range start and end positions are equal', () => {
  45. let collapsedRange = new Range( start, start );
  46. expect( collapsedRange.isCollapsed ).to.be.true;
  47. } );
  48. it( 'should be false if range start and end positions are not equal', () => {
  49. expect( range.isCollapsed ).to.be.false;
  50. } );
  51. } );
  52. describe( 'isEqual', () => {
  53. it( 'should return true if the ranges are the same', () => {
  54. let sameStart = Position.createFromPosition( start );
  55. let sameEnd = Position.createFromPosition( end );
  56. let sameRange = new Range( sameStart, sameEnd );
  57. expect( range.isEqual( sameRange ) ).to.be.true;
  58. } );
  59. it( 'should return false if the start position is different', () => {
  60. let range = new Range( start, end );
  61. let diffStart = new Position( root, [ 0 ] );
  62. let sameEnd = Position.createFromPosition( end );
  63. let diffRange = new Range( diffStart, sameEnd );
  64. expect( range.isEqual( diffRange ) ).to.be.false;
  65. } );
  66. it( 'should return false if the end position is different', () => {
  67. let sameStart = new Position( root, [ 0 ] );
  68. let diffEnd = new Position( root, [ 0 ] );
  69. let diffRange = new Range( sameStart, diffEnd );
  70. expect( range.isEqual( diffRange ) ).to.be.false;
  71. } );
  72. it( 'should return false if ranges are in different roots', () => {
  73. let otherRootStart = new Position( otherRoot, start.path.slice() );
  74. let otherRootEnd = new Position( otherRoot, end.path.slice() );
  75. let otherRootRange = new Range( otherRootStart, otherRootEnd );
  76. expect( range.isEqual( otherRootRange ) ).to.be.false;
  77. } );
  78. } );
  79. describe( 'isIntersecting', () => {
  80. it( 'should return true if given range is equal', () => {
  81. let otherRange = Range.createFromRange( range );
  82. expect( range.isIntersecting( otherRange ) ).to.be.true;
  83. } );
  84. it( 'should return true if given range contains this range', () => {
  85. let otherRange = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  86. expect( range.isIntersecting( otherRange ) ).to.be.true;
  87. } );
  88. it( 'should return true if given range ends in this range', () => {
  89. let otherRange = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1, 4 ] ) );
  90. expect( range.isIntersecting( otherRange ) ).to.be.true;
  91. } );
  92. it( 'should return true if given range starts in this range', () => {
  93. let otherRange = new Range( new Position( root, [ 1, 4 ] ), new Position( root, [ 3 ] ) );
  94. expect( range.isIntersecting( otherRange ) ).to.be.true;
  95. } );
  96. it( 'should return false if given range is fully before this range', () => {
  97. let otherRange = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  98. expect( range.isIntersecting( otherRange ) ).to.be.false;
  99. } );
  100. it( 'should return false if given range is fully after this range', () => {
  101. let otherRange = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 0 ] ) );
  102. expect( range.isIntersecting( otherRange ) ).to.be.false;
  103. } );
  104. it( 'should return false if ranges are in different roots', () => {
  105. let otherRange = new Range( new Position( otherRoot, [ 0 ] ), new Position( otherRoot, [ 1, 4 ] ) );
  106. expect( range.isIntersecting( otherRange ) ).to.be.false;
  107. } );
  108. } );
  109. describe( 'static constructors', () => {
  110. let p, f, o, z;
  111. // root
  112. // |- p
  113. // |- f
  114. // |- o
  115. // |- z
  116. before( () => {
  117. f = new Character( 'f' );
  118. o = new Character( 'o' );
  119. z = new Character( 'z' );
  120. p = new Element( 'p', [], [ f, o, z ] );
  121. root.insertChildren( 0, [ p ] );
  122. } );
  123. describe( 'createFromElement', () => {
  124. it( 'should return range', () => {
  125. const range = Range.createFromElement( p );
  126. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  127. expect( range.end.path ).to.deep.equal( [ 0, 3 ] );
  128. } );
  129. } );
  130. describe( 'createFromParentsAndOffsets', () => {
  131. it( 'should return range', () => {
  132. const range = Range.createFromParentsAndOffsets( root, 0, p, 2 );
  133. expect( range.start.path ).to.deep.equal( [ 0 ] );
  134. expect( range.end.path ).to.deep.equal( [ 0, 2 ] );
  135. } );
  136. } );
  137. describe( 'createFromPositionAndShift', () => {
  138. it( 'should make range from start position and offset', () => {
  139. const position = new Position( root, [ 1, 2, 3 ] );
  140. const range = Range.createFromPositionAndShift( position, 4 );
  141. expect( range ).to.be.instanceof( Range );
  142. expect( range.start.isEqual( position ) ).to.be.true;
  143. expect( range.end.root ).to.equal( range.start.root );
  144. expect( range.end.path ).to.deep.equal( [ 1, 2, 7 ] );
  145. } );
  146. } );
  147. describe( 'createFromRange', () => {
  148. it( 'should create a new instance of Range that is equal to passed range', () => {
  149. const clone = Range.createFromRange( range );
  150. expect( clone ).not.to.be.equal( range ); // clone is not pointing to the same object as position
  151. expect( clone.isEqual( range ) ).to.be.true; // but they are equal in the position-sense
  152. } );
  153. } );
  154. } );
  155. describe( 'getNodes', () => {
  156. it( 'should iterate over all nodes which "starts" in the range', () => {
  157. let nodes = [];
  158. const a = new Character( 'a' );
  159. const b = new Character( 'b' );
  160. const x = new Character( 'x' );
  161. const y = new Character( 'y' );
  162. const e1 = new Element( 'e1' );
  163. const e2 = new Element( 'e2' );
  164. e1.insertChildren( 0, [ a, b ] );
  165. e2.insertChildren( 0, [ x, y ] );
  166. root.insertChildren( 0, [ e1, e2 ] );
  167. let range = new Range(
  168. new Position( root, [ 0, 1 ] ),
  169. new Position( root, [ 1, 1 ] )
  170. );
  171. for ( let node of range.getNodes() ) {
  172. nodes.push( node );
  173. }
  174. expect( nodes ).to.deep.equal( [ b, e2, x ] );
  175. } );
  176. } );
  177. describe( 'containsPosition', () => {
  178. beforeEach( () => {
  179. range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) );
  180. } );
  181. it( 'should return false if position is before range', () => {
  182. const position = new Position( root, [ 0, 4 ] );
  183. expect( range.containsPosition( position ) ).to.be.false;
  184. } );
  185. it( 'should return false if position is after range', () => {
  186. const position = new Position( root, [ 3, 0 ] );
  187. expect( range.containsPosition( position ) ).to.be.false;
  188. } );
  189. it( 'should return true if position is inside range', () => {
  190. const position = new Position( root, [ 2, 2 ] );
  191. expect( range.containsPosition( position ) ).to.be.true;
  192. } );
  193. } );
  194. describe( 'getTransformedByInsertion', () => {
  195. it( 'should return an array of Range objects', () => {
  196. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  197. const transformed = range.getTransformedByInsertion( new Position( root, [ 2 ] ), 2 );
  198. expect( transformed ).to.be.instanceof( Array );
  199. expect( transformed[ 0 ] ).to.be.instanceof( Range );
  200. } );
  201. it( 'should update it\'s positions offsets if insertion is before range and they are affected', () => {
  202. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 4 ] ) );
  203. const transformed = range.getTransformedByInsertion( new Position( root, [ 3, 1 ] ), 2 );
  204. expect( transformed[ 0 ].start.offset ).to.equal( 4 );
  205. expect( transformed[ 0 ].end.offset ).to.equal( 6 );
  206. } );
  207. it( 'should update it\'s positions paths if insertion is before range and they are affected', () => {
  208. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  209. const transformed = range.getTransformedByInsertion( new Position( root, [ 0 ] ), 2 );
  210. expect( transformed[ 0 ].start.path[ 0 ] ).to.equal( 5 );
  211. expect( transformed[ 0 ].end.path[ 0 ] ).to.equal( 6 );
  212. } );
  213. it( 'should return array with two ranges and updated positions if insertion was in the middle of range', () => {
  214. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  215. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 1, 6 ] ), 4 );
  216. expect( transformed.length ).to.equal( 2 );
  217. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  218. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 1, 6 ] );
  219. expect( transformed[ 1 ].start.path ).to.deep.equal( [ 4, 1, 10 ] );
  220. expect( transformed[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  221. } );
  222. it( 'should not updated positions if insertion is after range', () => {
  223. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  224. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 4 ] ), 3 );
  225. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  226. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 4 ] );
  227. } );
  228. } );
  229. describe( 'getDifference', () => {
  230. let range;
  231. beforeEach( () => {
  232. range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  233. } );
  234. it( 'should return an array of Range objects', () => {
  235. const otherRange = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  236. const diff = range.getDifference( otherRange );
  237. expect( diff ).to.be.instanceof( Array );
  238. expect( diff[ 0 ] ).to.be.instanceof( Range );
  239. } );
  240. it( 'should return original range if other range does not intersect with it', () => {
  241. const otherRange = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  242. const diff = range.getDifference( otherRange );
  243. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  244. expect( diff[ 0 ].end.path ).to.deep.equal( [ 5, 4 ] );
  245. } );
  246. it( 'should return shrunken range if other range intersects with it', () => {
  247. const otherRange = new Range( new Position( root, [ 4, 1 ] ), new Position( root, [ 7 ] ) );
  248. const diff = range.getDifference( otherRange );
  249. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  250. expect( diff[ 0 ].end.path ).to.deep.equal( [ 4, 1 ] );
  251. } );
  252. it( 'should return an empty array if other range contains or is same as the original range', () => {
  253. const otherRange = new Range( new Position( root, [ 2 ] ), new Position( root, [ 6 ] ) );
  254. const diff = range.getDifference( otherRange );
  255. expect( diff.length ).to.equal( 0 );
  256. } );
  257. it( 'should two ranges if other range is contained by the original range', () => {
  258. const otherRange = new Range( new Position( root, [ 3, 7 ] ), new Position( root, [ 4, 1 ] ) );
  259. const diff = range.getDifference( otherRange );
  260. expect( diff.length ).to.equal( 2 );
  261. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  262. expect( diff[ 0 ].end.path ).to.deep.equal( [ 3, 7 ] );
  263. expect( diff[ 1 ].start.path ).to.deep.equal( [ 4, 1 ] );
  264. expect( diff[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  265. } );
  266. } );
  267. describe( 'getIntersection', () => {
  268. let range;
  269. beforeEach( () => {
  270. range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  271. } );
  272. it( 'should return null if ranges do not intersect', () => {
  273. const otherRange = new Range( new Position( root, [ 5, 4 ] ), new Position( root, [ 7 ] ) );
  274. const common = range.getIntersection( otherRange );
  275. expect( common ).to.be.null;
  276. } );
  277. it( 'should return a range equal to the common part of ranges - original range contains the other range', () => {
  278. const otherRange = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  279. const common = range.getIntersection( otherRange );
  280. expect( common.isEqual( otherRange ) ).to.be.true;
  281. } );
  282. it( 'should return a range equal to the common part of ranges - original range is contained by the other range', () => {
  283. const otherRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6 ] ) );
  284. const common = range.getIntersection( otherRange );
  285. expect( common.isEqual( range ) ).to.be.true;
  286. } );
  287. it( 'should return a range equal to the common part of ranges - original range intersects with the other range', () => {
  288. const otherRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 4, 7 ] ) );
  289. const common = range.getIntersection( otherRange );
  290. expect( common.start.path ).to.deep.equal( [ 3, 2 ] );
  291. expect( common.end.path ).to.deep.equal( [ 4, 7 ] );
  292. } );
  293. } );
  294. } );