range.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 Text from '/ckeditor5/core/treemodel/text.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 Text( 'f' );
  108. o = new Text( 'o' );
  109. z = new Text( '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. const a = new Text( 'a' );
  148. const b = new Text( 'b' );
  149. const x = new Text( 'x' );
  150. const y = new Text( 'y' );
  151. const e1 = new Element( 'e1' );
  152. const e2 = new Element( 'e2' );
  153. e1.insertChildren( 0, [ a, b ] );
  154. e2.insertChildren( 0, [ x, y ] );
  155. root.insertChildren( 0, [ e1, e2 ] );
  156. let range = new Range(
  157. new Position( root, [ 0, 1 ] ),
  158. new Position( root, [ 1, 1 ] )
  159. );
  160. let nodes = Array.from( range.getAllNodes() );
  161. expect( nodes.length ).to.equal( 3 );
  162. expect( nodes[ 0 ].character ).to.equal( 'b' );
  163. expect( nodes[ 1 ] ).to.equal( e2 );
  164. expect( nodes[ 2 ].character ).to.equal( 'x' );
  165. } );
  166. it( 'should merge characters with same attributes', () => {
  167. prepareRichRoot( root );
  168. let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
  169. let nodes = Array.from( range.getAllNodes( true ) );
  170. let nodeNames = mapNodesToNames( nodes );
  171. 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' ] );
  172. } );
  173. } );
  174. describe( 'containsPosition', () => {
  175. beforeEach( () => {
  176. range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) );
  177. } );
  178. it( 'should return false if position is before range', () => {
  179. const position = new Position( root, [ 0, 4 ] );
  180. expect( range.containsPosition( position ) ).to.be.false;
  181. } );
  182. it( 'should return false if position is after range', () => {
  183. const position = new Position( root, [ 3, 0 ] );
  184. expect( range.containsPosition( position ) ).to.be.false;
  185. } );
  186. it( 'should return true if position is inside range', () => {
  187. const position = new Position( root, [ 2, 2 ] );
  188. expect( range.containsPosition( position ) ).to.be.true;
  189. } );
  190. } );
  191. describe( 'getTransformedByInsertion', () => {
  192. it( 'should return an array of Range objects', () => {
  193. const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  194. const transformed = range.getTransformedByInsertion( new Position( root, [ 2 ] ), 2 );
  195. expect( transformed ).to.be.instanceof( Array );
  196. expect( transformed[ 0 ] ).to.be.instanceof( Range );
  197. } );
  198. it( 'should update it\'s positions offsets if insertion is before range and they are affected', () => {
  199. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 4 ] ) );
  200. const transformed = range.getTransformedByInsertion( new Position( root, [ 3, 1 ] ), 2 );
  201. expect( transformed[ 0 ].start.offset ).to.equal( 4 );
  202. expect( transformed[ 0 ].end.offset ).to.equal( 6 );
  203. } );
  204. it( 'should update it\'s positions paths if insertion is before range and they are affected', () => {
  205. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  206. const transformed = range.getTransformedByInsertion( new Position( root, [ 0 ] ), 2 );
  207. expect( transformed[ 0 ].start.path[ 0 ] ).to.equal( 5 );
  208. expect( transformed[ 0 ].end.path[ 0 ] ).to.equal( 6 );
  209. } );
  210. it( 'should return array with two ranges and updated positions if insertion was in the middle of range', () => {
  211. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  212. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 1, 6 ] ), 4 );
  213. expect( transformed.length ).to.equal( 2 );
  214. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  215. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 1, 6 ] );
  216. expect( transformed[ 1 ].start.path ).to.deep.equal( [ 4, 1, 10 ] );
  217. expect( transformed[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  218. } );
  219. it( 'should not updated positions if insertion is after range', () => {
  220. const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 4, 4 ] ) );
  221. const transformed = range.getTransformedByInsertion( new Position( root, [ 4, 4 ] ), 3 );
  222. expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  223. expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 4 ] );
  224. } );
  225. } );
  226. describe( 'getDifference', () => {
  227. let range;
  228. beforeEach( () => {
  229. range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  230. } );
  231. it( 'should return an array of Range objects', () => {
  232. const otherRange = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  233. const diff = range.getDifference( otherRange );
  234. expect( diff ).to.be.instanceof( Array );
  235. expect( diff[ 0 ] ).to.be.instanceof( Range );
  236. } );
  237. it( 'should return original range if other range does not intersect with it', () => {
  238. const otherRange = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  239. const diff = range.getDifference( otherRange );
  240. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  241. expect( diff[ 0 ].end.path ).to.deep.equal( [ 5, 4 ] );
  242. } );
  243. it( 'should return shrunken range if other range intersects with it', () => {
  244. const otherRange = new Range( new Position( root, [ 4, 1 ] ), new Position( root, [ 7 ] ) );
  245. const diff = range.getDifference( otherRange );
  246. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  247. expect( diff[ 0 ].end.path ).to.deep.equal( [ 4, 1 ] );
  248. } );
  249. it( 'should return an empty array if other range contains or is same as the original range', () => {
  250. const otherRange = new Range( new Position( root, [ 2 ] ), new Position( root, [ 6 ] ) );
  251. const diff = range.getDifference( otherRange );
  252. expect( diff.length ).to.equal( 0 );
  253. } );
  254. it( 'should two ranges if other range is contained by the original range', () => {
  255. const otherRange = new Range( new Position( root, [ 3, 7 ] ), new Position( root, [ 4, 1 ] ) );
  256. const diff = range.getDifference( otherRange );
  257. expect( diff.length ).to.equal( 2 );
  258. expect( diff[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
  259. expect( diff[ 0 ].end.path ).to.deep.equal( [ 3, 7 ] );
  260. expect( diff[ 1 ].start.path ).to.deep.equal( [ 4, 1 ] );
  261. expect( diff[ 1 ].end.path ).to.deep.equal( [ 5, 4 ] );
  262. } );
  263. } );
  264. describe( 'getIntersection', () => {
  265. let range;
  266. beforeEach( () => {
  267. range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
  268. } );
  269. it( 'should return null if ranges do not intersect', () => {
  270. const otherRange = new Range( new Position( root, [ 5, 4 ] ), new Position( root, [ 7 ] ) );
  271. const common = range.getIntersection( otherRange );
  272. expect( common ).to.be.null;
  273. } );
  274. it( 'should return a range equal to the common part of ranges - original range contains the other range', () => {
  275. const otherRange = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  276. const common = range.getIntersection( otherRange );
  277. expect( common.isEqual( otherRange ) ).to.be.true;
  278. } );
  279. it( 'should return a range equal to the common part of ranges - original range is contained by the other range', () => {
  280. const otherRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6 ] ) );
  281. const common = range.getIntersection( otherRange );
  282. expect( common.isEqual( range ) ).to.be.true;
  283. } );
  284. it( 'should return a range equal to the common part of ranges - original range intersects with the other range', () => {
  285. const otherRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 4, 7 ] ) );
  286. const common = range.getIntersection( otherRange );
  287. expect( common.start.path ).to.deep.equal( [ 3, 2 ] );
  288. expect( common.end.path ).to.deep.equal( [ 4, 7 ] );
  289. } );
  290. } );
  291. describe( 'getMinimalFlatRanges', () => {
  292. beforeEach( () => {
  293. prepareRichRoot( root );
  294. } );
  295. it( 'should return empty array if range is collapsed', () => {
  296. let range = new Range( new Position( root, [ 1, 3 ] ), new Position( root, [ 1, 3 ] ) );
  297. let flat = range.getMinimalFlatRanges();
  298. expect( flat.length ).to.equal( 0 );
  299. } );
  300. it( 'should return empty array if range does not contain any node', () => {
  301. let range = new Range( new Position( root, [ 1, 3 ] ), new Position( root, [ 2, 0 ] ) );
  302. let flat = range.getMinimalFlatRanges();
  303. expect( flat.length ).to.equal( 0 );
  304. } );
  305. it( 'should return a minimal set of flat ranges that covers the range (start and end in different sub-trees)', () => {
  306. let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
  307. let flat = range.getMinimalFlatRanges();
  308. expect( flat.length ).to.equal( 4 );
  309. expect( flat[ 0 ].start.path ).to.deep.equal( [ 0, 0, 3 ] );
  310. expect( flat[ 0 ].end.path ).to.deep.equal( [ 0, 0, 5 ] );
  311. expect( flat[ 1 ].start.path ).to.deep.equal( [ 0, 1 ] );
  312. expect( flat[ 1 ].end.path ).to.deep.equal( [ 0, 2 ] );
  313. expect( flat[ 2 ].start.path ).to.deep.equal( [ 1 ] );
  314. expect( flat[ 2 ].end.path ).to.deep.equal( [ 3 ] );
  315. expect( flat[ 3 ].start.path ).to.deep.equal( [ 3, 0, 0 ] );
  316. expect( flat[ 3 ].end.path ).to.deep.equal( [ 3, 0, 2 ] );
  317. } );
  318. it( 'should return a minimal set of flat ranges that covers the range (start.path is prefix of end.path)', () => {
  319. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 0, 1, 4 ] ) );
  320. let flat = range.getMinimalFlatRanges();
  321. expect( flat.length ).to.equal( 2 );
  322. expect( flat[ 0 ].start.path ).to.deep.equal( [ 0, 0 ] );
  323. expect( flat[ 0 ].end.path ).to.deep.equal( [ 0, 1 ] );
  324. expect( flat[ 1 ].start.path ).to.deep.equal( [ 0, 1, 0 ] );
  325. expect( flat[ 1 ].end.path ).to.deep.equal( [ 0, 1, 4 ] );
  326. } );
  327. } );
  328. describe( 'getTopLevelNodes', () => {
  329. beforeEach( () => {
  330. prepareRichRoot( root );
  331. } );
  332. it( 'should iterate over all top-level nodes of this range', () => {
  333. let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
  334. let nodes = Array.from( range.getTopLevelNodes() );
  335. let nodeNames = mapNodesToNames( nodes );
  336. expect( nodeNames ).to.deep.equal( [ 'T:s', 'T:t', 'E:p', 'E:p', 'E:p', 'T:s', 'T:e' ] );
  337. } );
  338. it( 'should merge characters with same attributes', () => {
  339. let range = new Range( new Position( root, [ 0, 0, 3 ] ), new Position( root, [ 3, 0, 2 ] ) );
  340. let nodes = Array.from( range.getTopLevelNodes( true ) );
  341. let nodeNames = mapNodesToNames( nodes );
  342. expect( nodeNames ).to.deep.equal( [ 'T:st', 'E:p', 'E:p', 'E:p', 'T:se' ] );
  343. } );
  344. } );
  345. describe( 'getPositions', () => {
  346. beforeEach( () => {
  347. prepareRichRoot( root );
  348. } );
  349. it( 'should iterate over all positions in this range', () => {
  350. let expectedPaths = [
  351. [ 1, 2 ], [ 1, 3 ],
  352. [ 2 ], [ 2, 0 ], [ 2, 1 ], [ 2, 2 ], [ 2, 3 ],
  353. [ 3 ], [ 3, 0 ], [ 3, 0, 0 ], [ 3, 0, 1 ], [ 3, 0, 2 ]
  354. ];
  355. let range = new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 3, 0, 2 ] ) );
  356. let i = 0;
  357. for ( let position of range.getPositions() ) {
  358. expect( position.path ).to.deep.equal( expectedPaths[ i ] );
  359. i++;
  360. }
  361. expect( i ).to.equal( expectedPaths.length );
  362. } );
  363. it( 'should merge characters with same attributes', () => {
  364. let expectedPaths = [
  365. [ 1, 2 ], [ 1, 3 ],
  366. [ 2 ], [ 2, 0 ], [ 2, 3 ],
  367. [ 3 ], [ 3, 0 ], [ 3, 0, 0 ], [ 3, 0, 2 ]
  368. ];
  369. let range = new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 3, 0, 2 ] ) );
  370. let i = 0;
  371. for ( let position of range.getPositions( true ) ) {
  372. expect( position.path ).to.deep.equal( expectedPaths[ i ] );
  373. i++;
  374. }
  375. expect( i ).to.equal( expectedPaths.length );
  376. } );
  377. } );
  378. describe( 'isFlat', () => {
  379. beforeEach( () => {
  380. prepareRichRoot( root );
  381. } );
  382. it( 'should be true if start and end position are in the same parent', () => {
  383. let range = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 2 ] ) );
  384. expect( range.isFlat ).to.be.true;
  385. } );
  386. it( 'should be false if start and end position are in different parents', () => {
  387. let range = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 3, 0, 1 ] ) );
  388. expect( range.isFlat ).to.be.false;
  389. } );
  390. } );
  391. function mapNodesToNames( nodes ) {
  392. return nodes.map( ( node ) => {
  393. return ( node instanceof Element ) ? 'E:' + node.name : 'T:' + ( node.text || node.character );
  394. } );
  395. }
  396. function prepareRichRoot() {
  397. root.insertChildren( 0, [
  398. new Element( 'div', [], [
  399. new Element( 'h', [], 'first' ),
  400. new Element( 'p', [], 'lorem ipsum' )
  401. ] ),
  402. new Element( 'p', [], 'foo' ),
  403. new Element( 'p', [], 'bar' ),
  404. new Element( 'div', [], [
  405. new Element( 'h', [], 'second' ),
  406. new Element( 'p', [], 'lorem' )
  407. ] )
  408. ] );
  409. }
  410. } );