range.js 11 KB

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