8
0

range.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document */
  6. 'use strict';
  7. const modules = bender.amd.require(
  8. 'document/range',
  9. 'document/position',
  10. 'document/element',
  11. 'document/character',
  12. 'document/document'
  13. );
  14. describe( 'Range', () => {
  15. let Range, Position, Element, Character, Document;
  16. let start, end, root;
  17. before( () => {
  18. Position = modules[ 'document/position' ];
  19. Range = modules[ 'document/range' ];
  20. Element = modules[ 'document/element' ];
  21. Character = modules[ 'document/character' ];
  22. Document = modules[ 'document/document' ];
  23. let doc = new Document();
  24. root = doc.createRoot( 'root' );
  25. start = new Position( [ 0 ], root );
  26. end = new Position( [ 1 ], root );
  27. } );
  28. let range;
  29. beforeEach( () => {
  30. range = new Range( start, end );
  31. } );
  32. describe( 'constructor', () => {
  33. it( 'should create a range with given positions', () => {
  34. expect( range ).to.have.property( 'start' ).that.equal( start );
  35. expect( range ).to.have.property( 'end' ).that.equal( end );
  36. } );
  37. } );
  38. describe( 'isEqual', () => {
  39. it( 'should return true if the ranges are the same', () => {
  40. let sameStart = new Position( [ 0 ], root );
  41. let sameEnd = new Position( [ 1 ], root );
  42. let sameRange = new Range( sameStart, sameEnd );
  43. expect( range.isEqual( sameRange ) ).to.be.true;
  44. } );
  45. it( 'should return false if the start position is different', () => {
  46. let range = new Range( start, end );
  47. let diffStart = new Position( [ 1 ], root );
  48. let sameEnd = new Position( [ 1 ], root );
  49. let diffRange = new Range( diffStart, sameEnd );
  50. expect( range.isEqual( diffRange ) ).to.not.be.true;
  51. } );
  52. it( 'should return false if the end position is different', () => {
  53. let sameStart = new Position( [ 0 ], root );
  54. let diffEnd = new Position( [ 0 ], root );
  55. let diffRange = new Range( sameStart, diffEnd );
  56. expect( range.isEqual( diffRange ) ).to.not.be.true;
  57. } );
  58. } );
  59. describe( 'static constructors', () => {
  60. let doc, root, p, f, o, z;
  61. // root
  62. // |- p
  63. // |- f
  64. // |- o
  65. // |- z
  66. before( () => {
  67. doc = new Document();
  68. root = doc.createRoot( 'root' );
  69. f = new Character( 'f' );
  70. o = new Character( 'o' );
  71. z = new Character( 'z' );
  72. p = new Element( 'p', [], [ f, o, z ] );
  73. root.insertChildren( 0, [ p ] );
  74. } );
  75. describe( 'createFromElement', () => {
  76. it( 'should return range', () => {
  77. const range = Range.createFromElement( p );
  78. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  79. expect( range.end.path ).to.deep.equal( [ 0, 3 ] );
  80. } );
  81. } );
  82. describe( 'createFromParentsAndOffsets', () => {
  83. it( 'should return range', () => {
  84. const range = Range.createFromParentsAndOffsets( root, 0, p, 2 );
  85. expect( range.start.path ).to.deep.equal( [ 0 ] );
  86. expect( range.end.path ).to.deep.equal( [ 0, 2 ] );
  87. } );
  88. } );
  89. describe( 'createFromPositionAndOffset', () => {
  90. it( 'should make range from start position and offset', () => {
  91. const position = new Position( [ 1, 2, 3 ], root );
  92. const range = Range.createFromPositionAndOffset( position, 4 );
  93. expect( range ).to.be.instanceof( Range );
  94. expect( range.start.isEqual( position ) ).to.be.true;
  95. expect( range.end.root ).to.equal( range.start.root );
  96. expect( range.end.path ).to.deep.equal( [ 1, 2, 7 ] );
  97. } );
  98. } );
  99. } );
  100. describe( 'clone', () => {
  101. it( 'should return a new, equal position', () => {
  102. const clone = range.clone();
  103. expect( clone ).not.to.be.equal( range ); // clone is not pointing to the same object as position
  104. expect( clone.isEqual( range ) ).to.be.true; // but they are equal in the position-sense
  105. } );
  106. } );
  107. describe( 'containsPosition', () => {
  108. beforeEach( () => {
  109. range = new Range( new Position( [ 1 ], root ), new Position( [ 3 ], root ) );
  110. } );
  111. it( 'should return false if position is before range', () => {
  112. const position = new Position( [ 0, 4 ], root );
  113. expect( range.containsPosition( position ) ).to.be.false;
  114. } );
  115. it( 'should return false if position is after range', () => {
  116. const position = new Position( [ 3, 0 ], root );
  117. expect( range.containsPosition( position ) ).to.be.false;
  118. } );
  119. it( 'should return true if position is inside range', () => {
  120. const position = new Position( [ 2, 2 ], root );
  121. expect( range.containsPosition( position ) ).to.be.true;
  122. } );
  123. } );
  124. describe( 'getTransformedByInsertion', () => {
  125. it( 'should return an array of Range objects', () => {
  126. const range = new Range( new Position( [ 0 ], root ), new Position( [ 1 ], root ) );
  127. const transformed = range.getTransformedByInsertion( new Position( [ 2 ], root ), 2 );
  128. expect( transformed ).to.be.instanceof( Array );
  129. expect( transformed[ 0 ] ).to.be.instanceof( Range );
  130. } );
  131. it( 'should update it\'s positions offsets if insertion is before range and they are affected', () => {
  132. const range = new Range( new Position( [ 3, 2 ], root ), new Position( [ 3, 4 ], root ) );
  133. const transformed = range.getTransformedByInsertion( new Position( [ 3, 1 ], root ), 2 );
  134. expect( transformed[ 0 ].start.offset ).to.equal( 4 );
  135. expect( transformed[ 0 ].end.offset ).to.equal( 6 );
  136. } );
  137. it( 'should update it\'s positions paths if insertion is before range and they are affected', () => {
  138. const range = new Range( new Position( [ 3, 2 ], root ), new Position( [ 4, 4 ], root ) );
  139. const transformed = range.getTransformedByInsertion( new Position( [ 0 ], root ), 2 );
  140. expect( transformed[ 0 ].start.path[ 0 ] ).to.equal( 5 );
  141. expect( transformed[ 0 ].end.path[ 0 ] ).to.equal( 6 );
  142. } );
  143. it( 'should return array with two ranges and updated positions if insertion was in the middle of range', () => {
  144. const range = new Range( new Position( [ 3, 2 ], root ), new Position( [ 5, 4 ], root ) );
  145. const transformed = range.getTransformedByInsertion( new Position( [ 4, 1, 6 ], root ), 4 );
  146. expect( transformed.length ).to.equal( 2 );
  147. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  148. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 1, 6 ] );
  149. expect( transformed[ 1 ].start.path ).to.deep.equal( [ 4, 1, 10 ] );
  150. expect( transformed[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  151. } );
  152. it( 'should not updated positions if insertion is after range', () => {
  153. const range = new Range( new Position( [ 3, 2 ], root ), new Position( [ 4, 4 ], root ) );
  154. const transformed = range.getTransformedByInsertion( new Position( [ 4, 4 ], root ), 3 );
  155. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  156. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 4 ] );
  157. } );
  158. } );
  159. describe( 'getDifference', () => {
  160. let range;
  161. beforeEach( () => {
  162. range = new Range( new Position( [ 3, 2 ], root ), new Position( [ 5, 4 ], root ) );
  163. } );
  164. it( 'should return an array of Range objects', () => {
  165. const otherRange = new Range( new Position( [ 6 ], root ), new Position( [ 7 ], root ) );
  166. const diff = range.getDifference( otherRange );
  167. expect( diff ).to.be.instanceof( Array );
  168. expect( diff[ 0 ] ).to.be.instanceof( Range );
  169. } );
  170. it( 'should return original range if other range does not intersect with it', () => {
  171. const otherRange = new Range( new Position( [ 6 ], root ), new Position( [ 7 ], root ) );
  172. const diff = range.getDifference( otherRange );
  173. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  174. expect( diff[ 0 ].end.path ).to.deep.equal( [ 5, 4 ] );
  175. } );
  176. it( 'should return shrunken range if other range intersects with it', () => {
  177. const otherRange = new Range( new Position( [ 4, 1 ], root ), new Position( [ 7 ], root ) );
  178. const diff = range.getDifference( otherRange );
  179. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  180. expect( diff[ 0 ].end.path ).to.deep.equal( [ 4, 1 ] );
  181. } );
  182. it( 'should return an empty array if other range contains or is same as the original range', () => {
  183. const otherRange = new Range( new Position( [ 2 ], root ), new Position( [ 6 ], root ) );
  184. const diff = range.getDifference( otherRange );
  185. expect( diff.length ).to.equal( 0 );
  186. } );
  187. it( 'should two ranges if other range is contained by the original range', () => {
  188. const otherRange = new Range( new Position( [ 3, 7 ], root ), new Position( [ 4, 1 ], root ) );
  189. const diff = range.getDifference( otherRange );
  190. expect( diff.length ).to.equal( 2 );
  191. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  192. expect( diff[ 0 ].end.path ).to.deep.equal( [ 3, 7 ] );
  193. expect( diff[ 1 ].start.path ).to.deep.equal( [ 4, 1 ] );
  194. expect( diff[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  195. } );
  196. } );
  197. describe( 'getIntersection', () => {
  198. let range;
  199. beforeEach( () => {
  200. range = new Range( new Position( [ 3, 2 ], root ), new Position( [ 5, 4 ], root ) );
  201. } );
  202. it( 'should return null if ranges do not intersect', () => {
  203. const otherRange = new Range( new Position( [ 5, 4 ], root ), new Position( [ 7 ], root ) );
  204. const common = range.getIntersection( otherRange );
  205. expect( common ).to.be.null;
  206. } );
  207. it( 'should return a range equal to the common part of ranges - original range contains the other range', () => {
  208. const otherRange = new Range( new Position( [ 4 ], root ), new Position( [ 5 ], root ) );
  209. const common = range.getIntersection( otherRange );
  210. expect( common.isEqual( otherRange ) ).to.be.true;
  211. } );
  212. it( 'should return a range equal to the common part of ranges - original range is contained by the other range', () => {
  213. const otherRange = new Range( new Position( [ 3 ], root ), new Position( [ 6 ], root ) );
  214. const common = range.getIntersection( otherRange );
  215. expect( common.isEqual( range ) ).to.be.true;
  216. } );
  217. it( 'should return a range equal to the common part of ranges - original range intersects with the other range', () => {
  218. const otherRange = new Range( new Position( [ 3 ], root ), new Position( [ 4, 7 ], root ) );
  219. const common = range.getIntersection( otherRange );
  220. expect( common.start.path ).to.deep.equal( [ 3, 2 ] );
  221. expect( common.end.path ).to.deep.equal( [ 4, 7 ] );
  222. } );
  223. } );
  224. } );