range.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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;
  24. beforeEach( () => {
  25. let doc = new Document();
  26. root = doc.createRoot( 'root' );
  27. start = new Position( root, [ 0 ] );
  28. end = new Position( root, [ 1 ] );
  29. range = new Range( start, end );
  30. } );
  31. describe( 'constructor', () => {
  32. it( 'should create a range with given positions', () => {
  33. expect( range.start.isEqual( start ) ).to.be.true;
  34. expect( range.end.isEqual( end ) ).to.be.true;
  35. } );
  36. } );
  37. describe( 'root', () => {
  38. it( 'should be equal to start position root', () => {
  39. expect( range.root ).to.equal( start.root );
  40. } );
  41. } );
  42. describe( 'isCollapsed', () => {
  43. it( 'should be true if range start and end positions are equal', () => {
  44. let collapsedRange = new Range( start, start );
  45. expect( collapsedRange.isCollapsed ).to.be.true;
  46. } );
  47. it( 'should be false if range start and end positions are not equal', () => {
  48. expect( range.isCollapsed ).to.be.false;
  49. } );
  50. } );
  51. describe( 'isEqual', () => {
  52. it( 'should return true if the ranges are the same', () => {
  53. let sameStart = new Position( root, [ 0 ] );
  54. let sameEnd = new Position( root, [ 1 ] );
  55. let sameRange = new Range( sameStart, sameEnd );
  56. expect( range.isEqual( sameRange ) ).to.be.true;
  57. } );
  58. it( 'should return false if the start position is different', () => {
  59. let range = new Range( start, end );
  60. let diffStart = new Position( root, [ 1 ] );
  61. let sameEnd = new Position( root, [ 1 ] );
  62. let diffRange = new Range( diffStart, sameEnd );
  63. expect( range.isEqual( diffRange ) ).to.not.be.true;
  64. } );
  65. it( 'should return false if the end position is different', () => {
  66. let sameStart = new Position( root, [ 0 ] );
  67. let diffEnd = new Position( root, [ 0 ] );
  68. let diffRange = new Range( sameStart, diffEnd );
  69. expect( range.isEqual( diffRange ) ).to.not.be.true;
  70. } );
  71. } );
  72. describe( 'static constructors', () => {
  73. let p, f, o, z;
  74. // root
  75. // |- p
  76. // |- f
  77. // |- o
  78. // |- z
  79. before( () => {
  80. f = new Character( 'f' );
  81. o = new Character( 'o' );
  82. z = new Character( 'z' );
  83. p = new Element( 'p', [], [ f, o, z ] );
  84. root.insertChildren( 0, [ p ] );
  85. } );
  86. describe( 'createFromElement', () => {
  87. it( 'should return range', () => {
  88. const range = Range.createFromElement( p );
  89. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  90. expect( range.end.path ).to.deep.equal( [ 0, 3 ] );
  91. } );
  92. } );
  93. describe( 'createFromParentsAndOffsets', () => {
  94. it( 'should return range', () => {
  95. const range = Range.createFromParentsAndOffsets( root, 0, p, 2 );
  96. expect( range.start.path ).to.deep.equal( [ 0 ] );
  97. expect( range.end.path ).to.deep.equal( [ 0, 2 ] );
  98. } );
  99. } );
  100. describe( 'createFromPositionAndShift', () => {
  101. it( 'should make range from start position and offset', () => {
  102. const position = new Position( root, [ 1, 2, 3 ] );
  103. const range = Range.createFromPositionAndShift( position, 4 );
  104. expect( range ).to.be.instanceof( Range );
  105. expect( range.start.isEqual( position ) ).to.be.true;
  106. expect( range.end.root ).to.equal( range.start.root );
  107. expect( range.end.path ).to.deep.equal( [ 1, 2, 7 ] );
  108. } );
  109. } );
  110. describe( 'createFromRange', () => {
  111. it( 'should create a new instance of Range that is equal to passed range', () => {
  112. const clone = Range.createFromRange( range );
  113. expect( clone ).not.to.be.equal( range ); // clone is not pointing to the same object as position
  114. expect( clone.isEqual( range ) ).to.be.true; // but they are equal in the position-sense
  115. } );
  116. } );
  117. } );
  118. describe( 'getNodes', () => {
  119. it( 'should iterate over all nodes which "starts" in the range', () => {
  120. let nodes = [];
  121. const a = new Character( 'a' );
  122. const b = new Character( 'b' );
  123. const x = new Character( 'x' );
  124. const y = new Character( 'y' );
  125. const e1 = new Element( 'e1' );
  126. const e2 = new Element( 'e2' );
  127. e1.insertChildren( 0, [ a, b ] );
  128. e2.insertChildren( 0, [ x, y ] );
  129. root.insertChildren( 0, [ e1, e2 ] );
  130. let range = new Range(
  131. new Position( root, [ 0, 1 ] ),
  132. new Position( root, [ 1, 1 ] )
  133. );
  134. for ( let node of range.getNodes() ) {
  135. nodes.push( node );
  136. }
  137. expect( nodes ).to.deep.equal( [ b, e2, x ] );
  138. } );
  139. } );
  140. describe( 'containsPosition', () => {
  141. beforeEach( () => {
  142. range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) );
  143. } );
  144. it( 'should return false if position is before range', () => {
  145. const position = new Position( root, [ 0, 4 ] );
  146. expect( range.containsPosition( position ) ).to.be.false;
  147. } );
  148. it( 'should return false if position is after range', () => {
  149. const position = new Position( root, [ 3, 0 ] );
  150. expect( range.containsPosition( position ) ).to.be.false;
  151. } );
  152. it( 'should return true if position is inside range', () => {
  153. const position = new Position( root, [ 2, 2 ] );
  154. expect( range.containsPosition( position ) ).to.be.true;
  155. } );
  156. } );
  157. describe( 'getTransformedByInsertion', () => {
  158. it( 'should return an array of Range objects', () => {
  159. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  160. const transformed = range.getTransformedByInsertion( new Position( root, [ 2 ] ), 2 );
  161. expect( transformed ).to.be.instanceof( Array );
  162. expect( transformed[ 0 ] ).to.be.instanceof( Range );
  163. } );
  164. it( 'should update it\'s positions offsets if insertion is before range and they are affected', () => {
  165. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 4 ] ) );
  166. const transformed = range.getTransformedByInsertion( new Position( root, [ 3, 1 ] ), 2 );
  167. expect( transformed[ 0 ].start.offset ).to.equal( 4 );
  168. expect( transformed[ 0 ].end.offset ).to.equal( 6 );
  169. } );
  170. it( 'should update it\'s positions paths if insertion is before range and they are affected', () => {
  171. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  172. const transformed = range.getTransformedByInsertion( new Position( root, [ 0 ] ), 2 );
  173. expect( transformed[ 0 ].start.path[ 0 ] ).to.equal( 5 );
  174. expect( transformed[ 0 ].end.path[ 0 ] ).to.equal( 6 );
  175. } );
  176. it( 'should return array with two ranges and updated positions if insertion was in the middle of range', () => {
  177. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  178. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 1, 6 ] ), 4 );
  179. expect( transformed.length ).to.equal( 2 );
  180. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  181. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 1, 6 ] );
  182. expect( transformed[ 1 ].start.path ).to.deep.equal( [ 4, 1, 10 ] );
  183. expect( transformed[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  184. } );
  185. it( 'should not updated positions if insertion is after range', () => {
  186. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  187. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 4 ] ), 3 );
  188. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  189. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 4 ] );
  190. } );
  191. } );
  192. describe( 'getDifference', () => {
  193. let range;
  194. beforeEach( () => {
  195. range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  196. } );
  197. it( 'should return an array of Range objects', () => {
  198. const otherRange = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  199. const diff = range.getDifference( otherRange );
  200. expect( diff ).to.be.instanceof( Array );
  201. expect( diff[ 0 ] ).to.be.instanceof( Range );
  202. } );
  203. it( 'should return original range if other range does not intersect with it', () => {
  204. const otherRange = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  205. const diff = range.getDifference( otherRange );
  206. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  207. expect( diff[ 0 ].end.path ).to.deep.equal( [ 5, 4 ] );
  208. } );
  209. it( 'should return shrunken range if other range intersects with it', () => {
  210. const otherRange = new Range( new Position( root, [ 4, 1 ] ), new Position( root, [ 7 ] ) );
  211. const diff = range.getDifference( otherRange );
  212. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  213. expect( diff[ 0 ].end.path ).to.deep.equal( [ 4, 1 ] );
  214. } );
  215. it( 'should return an empty array if other range contains or is same as the original range', () => {
  216. const otherRange = new Range( new Position( root, [ 2 ] ), new Position( root, [ 6 ] ) );
  217. const diff = range.getDifference( otherRange );
  218. expect( diff.length ).to.equal( 0 );
  219. } );
  220. it( 'should two ranges if other range is contained by the original range', () => {
  221. const otherRange = new Range( new Position( root, [ 3, 7 ] ), new Position( root, [ 4, 1 ] ) );
  222. const diff = range.getDifference( otherRange );
  223. expect( diff.length ).to.equal( 2 );
  224. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  225. expect( diff[ 0 ].end.path ).to.deep.equal( [ 3, 7 ] );
  226. expect( diff[ 1 ].start.path ).to.deep.equal( [ 4, 1 ] );
  227. expect( diff[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  228. } );
  229. } );
  230. describe( 'getIntersection', () => {
  231. let range;
  232. beforeEach( () => {
  233. range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  234. } );
  235. it( 'should return null if ranges do not intersect', () => {
  236. const otherRange = new Range( new Position( root, [ 5, 4 ] ), new Position( root, [ 7 ] ) );
  237. const common = range.getIntersection( otherRange );
  238. expect( common ).to.be.null;
  239. } );
  240. it( 'should return a range equal to the common part of ranges - original range contains the other range', () => {
  241. const otherRange = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  242. const common = range.getIntersection( otherRange );
  243. expect( common.isEqual( otherRange ) ).to.be.true;
  244. } );
  245. it( 'should return a range equal to the common part of ranges - original range is contained by the other range', () => {
  246. const otherRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6 ] ) );
  247. const common = range.getIntersection( otherRange );
  248. expect( common.isEqual( range ) ).to.be.true;
  249. } );
  250. it( 'should return a range equal to the common part of ranges - original range intersects with the other range', () => {
  251. const otherRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 4, 7 ] ) );
  252. const common = range.getIntersection( otherRange );
  253. expect( common.start.path ).to.deep.equal( [ 3, 2 ] );
  254. expect( common.end.path ).to.deep.equal( [ 4, 7 ] );
  255. } );
  256. } );
  257. } );