range.js 19 KB

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