8
0

range.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import Range from '/ckeditor5/core/treemodel/range.js';
  8. import Position from '/ckeditor5/core/treemodel/position.js';
  9. import Element from '/ckeditor5/core/treemodel/element.js';
  10. import Character from '/ckeditor5/core/treemodel/character.js';
  11. import Document from '/ckeditor5/core/treemodel/document.js';
  12. describe( 'Range', () => {
  13. let range, start, end, root, otherRoot;
  14. beforeEach( () => {
  15. let doc = new Document();
  16. root = doc.createRoot( 'root' );
  17. otherRoot = doc.createRoot( 'otherRoot' );
  18. start = new Position( root, [ 1 ] );
  19. end = new Position( root, [ 2 ] );
  20. range = new Range( start, end );
  21. } );
  22. describe( 'constructor', () => {
  23. it( 'should create a range with given positions', () => {
  24. expect( range.start.isEqual( start ) ).to.be.true;
  25. expect( range.end.isEqual( end ) ).to.be.true;
  26. } );
  27. } );
  28. describe( 'root', () => {
  29. it( 'should be equal to start position root', () => {
  30. expect( range.root ).to.equal( start.root );
  31. } );
  32. } );
  33. describe( 'isCollapsed', () => {
  34. it( 'should be true if range start and end positions are equal', () => {
  35. let collapsedRange = new Range( start, start );
  36. expect( collapsedRange.isCollapsed ).to.be.true;
  37. } );
  38. it( 'should be false if range start and end positions are not equal', () => {
  39. expect( range.isCollapsed ).to.be.false;
  40. } );
  41. } );
  42. describe( 'isEqual', () => {
  43. it( 'should return true if the ranges are the same', () => {
  44. let sameStart = Position.createFromPosition( start );
  45. let sameEnd = Position.createFromPosition( end );
  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, [ 0 ] );
  52. let sameEnd = Position.createFromPosition( end );
  53. let diffRange = new Range( diffStart, sameEnd );
  54. expect( range.isEqual( diffRange ) ).to.be.false;
  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.be.false;
  61. } );
  62. it( 'should return false if ranges are in different roots', () => {
  63. let otherRootStart = new Position( otherRoot, start.path.slice() );
  64. let otherRootEnd = new Position( otherRoot, end.path.slice() );
  65. let otherRootRange = new Range( otherRootStart, otherRootEnd );
  66. expect( range.isEqual( otherRootRange ) ).to.be.false;
  67. } );
  68. } );
  69. describe( 'isIntersecting', () => {
  70. it( 'should return true if given range is equal', () => {
  71. let otherRange = Range.createFromRange( range );
  72. expect( range.isIntersecting( otherRange ) ).to.be.true;
  73. } );
  74. it( 'should return true if given range contains this range', () => {
  75. let otherRange = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  76. expect( range.isIntersecting( otherRange ) ).to.be.true;
  77. } );
  78. it( 'should return true if given range ends in this range', () => {
  79. let otherRange = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1, 4 ] ) );
  80. expect( range.isIntersecting( otherRange ) ).to.be.true;
  81. } );
  82. it( 'should return true if given range starts in this range', () => {
  83. let otherRange = new Range( new Position( root, [ 1, 4 ] ), new Position( root, [ 3 ] ) );
  84. expect( range.isIntersecting( otherRange ) ).to.be.true;
  85. } );
  86. it( 'should return false if given range is fully before this range', () => {
  87. let otherRange = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  88. expect( range.isIntersecting( otherRange ) ).to.be.false;
  89. } );
  90. it( 'should return false if given range is fully after this range', () => {
  91. let otherRange = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 0 ] ) );
  92. expect( range.isIntersecting( otherRange ) ).to.be.false;
  93. } );
  94. it( 'should return false if ranges are in different roots', () => {
  95. let otherRange = new Range( new Position( otherRoot, [ 0 ] ), new Position( otherRoot, [ 1, 4 ] ) );
  96. expect( range.isIntersecting( otherRange ) ).to.be.false;
  97. } );
  98. } );
  99. describe( 'static constructors', () => {
  100. let p, f, o, z;
  101. // root
  102. // |- p
  103. // |- f
  104. // |- o
  105. // |- z
  106. before( () => {
  107. f = new Character( 'f' );
  108. o = new Character( 'o' );
  109. z = new Character( 'z' );
  110. p = new Element( 'p', [], [ f, o, z ] );
  111. root.insertChildren( 0, [ p ] );
  112. } );
  113. describe( 'createFromElement', () => {
  114. it( 'should return range', () => {
  115. const range = Range.createFromElement( p );
  116. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  117. expect( range.end.path ).to.deep.equal( [ 0, 3 ] );
  118. } );
  119. } );
  120. describe( 'createFromParentsAndOffsets', () => {
  121. it( 'should return range', () => {
  122. const range = Range.createFromParentsAndOffsets( root, 0, p, 2 );
  123. expect( range.start.path ).to.deep.equal( [ 0 ] );
  124. expect( range.end.path ).to.deep.equal( [ 0, 2 ] );
  125. } );
  126. } );
  127. describe( 'createFromPositionAndShift', () => {
  128. it( 'should make range from start position and offset', () => {
  129. const position = new Position( root, [ 1, 2, 3 ] );
  130. const range = Range.createFromPositionAndShift( position, 4 );
  131. expect( range ).to.be.instanceof( Range );
  132. expect( range.start.isEqual( position ) ).to.be.true;
  133. expect( range.end.root ).to.equal( range.start.root );
  134. expect( range.end.path ).to.deep.equal( [ 1, 2, 7 ] );
  135. } );
  136. } );
  137. describe( 'createFromRange', () => {
  138. it( 'should create a new instance of Range that is equal to passed range', () => {
  139. const clone = Range.createFromRange( range );
  140. expect( clone ).not.to.be.equal( range ); // clone is not pointing to the same object as position
  141. expect( clone.isEqual( range ) ).to.be.true; // but they are equal in the position-sense
  142. } );
  143. } );
  144. } );
  145. describe( 'getAllNodes', () => {
  146. it( 'should iterate over all nodes which "starts" in the range', () => {
  147. let nodes = [];
  148. const a = new Character( 'a' );
  149. const b = new Character( 'b' );
  150. const x = new Character( 'x' );
  151. const y = new Character( 'y' );
  152. const e1 = new Element( 'e1' );
  153. const e2 = new Element( 'e2' );
  154. e1.insertChildren( 0, [ a, b ] );
  155. e2.insertChildren( 0, [ x, y ] );
  156. root.insertChildren( 0, [ e1, e2 ] );
  157. let range = new Range(
  158. new Position( root, [ 0, 1 ] ),
  159. new Position( root, [ 1, 1 ] )
  160. );
  161. for ( let node of range.getAllNodes() ) {
  162. nodes.push( node );
  163. }
  164. expect( nodes ).to.deep.equal( [ b, e2, x ] );
  165. } );
  166. } );
  167. describe( 'containsPosition', () => {
  168. beforeEach( () => {
  169. range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) );
  170. } );
  171. it( 'should return false if position is before range', () => {
  172. const position = new Position( root, [ 0, 4 ] );
  173. expect( range.containsPosition( position ) ).to.be.false;
  174. } );
  175. it( 'should return false if position is after range', () => {
  176. const position = new Position( root, [ 3, 0 ] );
  177. expect( range.containsPosition( position ) ).to.be.false;
  178. } );
  179. it( 'should return true if position is inside range', () => {
  180. const position = new Position( root, [ 2, 2 ] );
  181. expect( range.containsPosition( position ) ).to.be.true;
  182. } );
  183. } );
  184. describe( 'getTransformedByInsertion', () => {
  185. it( 'should return an array of Range objects', () => {
  186. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  187. const transformed = range.getTransformedByInsertion( new Position( root, [ 2 ] ), 2 );
  188. expect( transformed ).to.be.instanceof( Array );
  189. expect( transformed[ 0 ] ).to.be.instanceof( Range );
  190. } );
  191. it( 'should update it\'s positions offsets if insertion is before range and they are affected', () => {
  192. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 4 ] ) );
  193. const transformed = range.getTransformedByInsertion( new Position( root, [ 3, 1 ] ), 2 );
  194. expect( transformed[ 0 ].start.offset ).to.equal( 4 );
  195. expect( transformed[ 0 ].end.offset ).to.equal( 6 );
  196. } );
  197. it( 'should update it\'s positions paths if insertion is before range and they are affected', () => {
  198. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  199. const transformed = range.getTransformedByInsertion( new Position( root, [ 0 ] ), 2 );
  200. expect( transformed[ 0 ].start.path[ 0 ] ).to.equal( 5 );
  201. expect( transformed[ 0 ].end.path[ 0 ] ).to.equal( 6 );
  202. } );
  203. it( 'should return array with two ranges and updated positions if insertion was in the middle of range', () => {
  204. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  205. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 1, 6 ] ), 4 );
  206. expect( transformed.length ).to.equal( 2 );
  207. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  208. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 1, 6 ] );
  209. expect( transformed[ 1 ].start.path ).to.deep.equal( [ 4, 1, 10 ] );
  210. expect( transformed[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  211. } );
  212. it( 'should not updated positions if insertion is after range', () => {
  213. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  214. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 4 ] ), 3 );
  215. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  216. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 4 ] );
  217. } );
  218. } );
  219. describe( 'getDifference', () => {
  220. let range;
  221. beforeEach( () => {
  222. range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  223. } );
  224. it( 'should return an array of Range objects', () => {
  225. const otherRange = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  226. const diff = range.getDifference( otherRange );
  227. expect( diff ).to.be.instanceof( Array );
  228. expect( diff[ 0 ] ).to.be.instanceof( Range );
  229. } );
  230. it( 'should return original range if other range does not intersect with it', () => {
  231. const otherRange = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  232. const diff = range.getDifference( otherRange );
  233. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  234. expect( diff[ 0 ].end.path ).to.deep.equal( [ 5, 4 ] );
  235. } );
  236. it( 'should return shrunken range if other range intersects with it', () => {
  237. const otherRange = new Range( new Position( root, [ 4, 1 ] ), new Position( root, [ 7 ] ) );
  238. const diff = range.getDifference( otherRange );
  239. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  240. expect( diff[ 0 ].end.path ).to.deep.equal( [ 4, 1 ] );
  241. } );
  242. it( 'should return an empty array if other range contains or is same as the original range', () => {
  243. const otherRange = new Range( new Position( root, [ 2 ] ), new Position( root, [ 6 ] ) );
  244. const diff = range.getDifference( otherRange );
  245. expect( diff.length ).to.equal( 0 );
  246. } );
  247. it( 'should two ranges if other range is contained by the original range', () => {
  248. const otherRange = new Range( new Position( root, [ 3, 7 ] ), new Position( root, [ 4, 1 ] ) );
  249. const diff = range.getDifference( otherRange );
  250. expect( diff.length ).to.equal( 2 );
  251. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  252. expect( diff[ 0 ].end.path ).to.deep.equal( [ 3, 7 ] );
  253. expect( diff[ 1 ].start.path ).to.deep.equal( [ 4, 1 ] );
  254. expect( diff[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  255. } );
  256. } );
  257. describe( 'getIntersection', () => {
  258. let range;
  259. beforeEach( () => {
  260. range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  261. } );
  262. it( 'should return null if ranges do not intersect', () => {
  263. const otherRange = new Range( new Position( root, [ 5, 4 ] ), new Position( root, [ 7 ] ) );
  264. const common = range.getIntersection( otherRange );
  265. expect( common ).to.be.null;
  266. } );
  267. it( 'should return a range equal to the common part of ranges - original range contains the other range', () => {
  268. const otherRange = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  269. const common = range.getIntersection( otherRange );
  270. expect( common.isEqual( otherRange ) ).to.be.true;
  271. } );
  272. it( 'should return a range equal to the common part of ranges - original range is contained by the other range', () => {
  273. const otherRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6 ] ) );
  274. const common = range.getIntersection( otherRange );
  275. expect( common.isEqual( range ) ).to.be.true;
  276. } );
  277. it( 'should return a range equal to the common part of ranges - original range intersects with the other range', () => {
  278. const otherRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 4, 7 ] ) );
  279. const common = range.getIntersection( otherRange );
  280. expect( common.start.path ).to.deep.equal( [ 3, 2 ] );
  281. expect( common.end.path ).to.deep.equal( [ 4, 7 ] );
  282. } );
  283. } );
  284. describe( 'getMinimalFlatRanges', () => {
  285. beforeEach( () => {
  286. prepareRichRoot( root );
  287. } );
  288. it( 'should return empty array if range is collapsed', () => {
  289. let range = new Range( new Position( root, [ 1, 3 ] ), new Position( root, [ 1, 3 ] ) );
  290. let flat = range.getMinimalFlatRanges();
  291. expect( flat.length ).to.equal( 0 );
  292. } );
  293. it( 'should return empty array if range does not contain any node', () => {
  294. let range = new Range( new Position( root, [ 1, 3 ] ), new Position( root, [ 2, 0 ] ) );
  295. let flat = range.getMinimalFlatRanges();
  296. expect( flat.length ).to.equal( 0 );
  297. } );
  298. it( 'should return a minimal set of flat ranges that covers the range (start and end in different sub-trees)', () => {
  299. let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
  300. let flat = range.getMinimalFlatRanges();
  301. expect( flat.length ).to.equal( 4 );
  302. expect( flat[ 0 ].start.path ).to.deep.equal( [ 0, 0, 3 ] );
  303. expect( flat[ 0 ].end.path ).to.deep.equal( [ 0, 0, 5 ] );
  304. expect( flat[ 1 ].start.path ).to.deep.equal( [ 0, 1 ] );
  305. expect( flat[ 1 ].end.path ).to.deep.equal( [ 0, 2 ] );
  306. expect( flat[ 2 ].start.path ).to.deep.equal( [ 1 ] );
  307. expect( flat[ 2 ].end.path ).to.deep.equal( [ 3 ] );
  308. expect( flat[ 3 ].start.path ).to.deep.equal( [ 3, 0, 0 ] );
  309. expect( flat[ 3 ].end.path ).to.deep.equal( [ 3, 0, 2 ] );
  310. } );
  311. it( 'should return a minimal set of flat ranges that covers the range (start.path is prefix of end.path)', () => {
  312. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 0, 1, 4 ] ) );
  313. let flat = range.getMinimalFlatRanges();
  314. expect( flat.length ).to.equal( 2 );
  315. expect( flat[ 0 ].start.path ).to.deep.equal( [ 0, 0 ] );
  316. expect( flat[ 0 ].end.path ).to.deep.equal( [ 0, 1 ] );
  317. expect( flat[ 1 ].start.path ).to.deep.equal( [ 0, 1, 0 ] );
  318. expect( flat[ 1 ].end.path ).to.deep.equal( [ 0, 1, 4 ] );
  319. } );
  320. } );
  321. describe( 'getTopLevelNodes', () => {
  322. beforeEach( () => {
  323. prepareRichRoot( root );
  324. } );
  325. it( 'should iterate over all top-level nodes of this range', () => {
  326. let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
  327. let nodes = Array.from( range.getTopLevelNodes() );
  328. let nodeNames = nodes.map( ( node ) => {
  329. return ( node instanceof Element ) ? 'E:' + node.name : 'C:' + node.character;
  330. } );
  331. expect( nodeNames ).to.deep.equal( [ 'C:s', 'C:t', 'E:p', 'E:p', 'E:p', 'C:s', 'C:e' ] );
  332. } );
  333. } );
  334. describe( 'isFlat', () => {
  335. beforeEach( () => {
  336. prepareRichRoot( root );
  337. } );
  338. it( 'should be true if start and end position are in the same parent', () => {
  339. let range = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 2 ] ) );
  340. expect( range.isFlat ).to.be.true;
  341. } );
  342. it( 'should be false if start and end position are in different parents', () => {
  343. let range = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 3, 0, 1 ] ) );
  344. expect( range.isFlat ).to.be.false;
  345. } );
  346. } );
  347. function prepareRichRoot() {
  348. root.insertChildren( 0, [
  349. new Element( 'div', [], [
  350. new Element( 'h', [], 'first' ),
  351. new Element( 'p', [], 'lorem ipsum' )
  352. ] ),
  353. new Element( 'p', [], 'foo' ),
  354. new Element( 'p', [], 'bar' ),
  355. new Element( 'div', [], [
  356. new Element( 'h', [], 'second' ),
  357. new Element( 'p', [], 'lorem' )
  358. ] )
  359. ] );
  360. }
  361. } );