8
0

range.js 18 KB

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