range.js 11 KB

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