range.js 20 KB

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