liverange.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../src/model/document';
  6. import Element from '../../src/model/element';
  7. import Position from '../../src/model/position';
  8. import LiveRange from '../../src/model/liverange';
  9. import Range from '../../src/model/range';
  10. import Text from '../../src/model/text';
  11. import { stringify, setData } from '../../src/dev-utils/model';
  12. describe( 'LiveRange', () => {
  13. let doc, root, ul, p;
  14. beforeEach( () => {
  15. doc = new Document();
  16. root = doc.createRoot();
  17. const lis = [
  18. new Element( 'li', [], new Text( 'aaaaaaaaaa' ) ),
  19. new Element( 'li', [], new Text( 'bbbbbbbbbb' ) ),
  20. new Element( 'li', [], new Text( 'cccccccccc' ) ),
  21. new Element( 'li', [], new Text( 'dddddddddd' ) ),
  22. new Element( 'li', [], new Text( 'eeeeeeeeee' ) ),
  23. new Element( 'li', [], new Text( 'ffffffffff' ) ),
  24. new Element( 'li', [], new Text( 'gggggggggg' ) ),
  25. new Element( 'li', [], new Text( 'hhhhhhhhhh' ) )
  26. ];
  27. ul = new Element( 'ul', [], lis );
  28. p = new Element( 'p', [], new Text( 'qwertyuiop' ) );
  29. root.insertChildren( 0, [ ul, p, new Text( 'xyzxyz' ) ] );
  30. } );
  31. it( 'should be an instance of Range', () => {
  32. const live = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  33. live.detach();
  34. expect( live ).to.be.instanceof( Range );
  35. } );
  36. it( 'should listen to a change event of the document that owns this range', () => {
  37. sinon.spy( LiveRange.prototype, 'listenTo' );
  38. const live = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  39. live.detach();
  40. expect( live.listenTo.calledWith( doc, 'change' ) ).to.be.true;
  41. LiveRange.prototype.listenTo.restore();
  42. } );
  43. it( 'should stop listening when detached', () => {
  44. sinon.spy( LiveRange.prototype, 'stopListening' );
  45. const live = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  46. live.detach();
  47. expect( live.stopListening.called ).to.be.true;
  48. LiveRange.prototype.stopListening.restore();
  49. } );
  50. it( 'createIn should return LiveRange', () => {
  51. const range = LiveRange.createIn( p );
  52. expect( range ).to.be.instanceof( LiveRange );
  53. range.detach();
  54. } );
  55. it( 'createFromParentsAndOffsets should return LiveRange', () => {
  56. const range = LiveRange.createFromParentsAndOffsets( root, 0, p, 2 );
  57. expect( range ).to.be.instanceof( LiveRange );
  58. range.detach();
  59. } );
  60. it( 'createFromPositionAndShift should return LiveRange', () => {
  61. const range = LiveRange.createFromPositionAndShift( new Position( root, [ 0, 1 ] ), 4 );
  62. expect( range ).to.be.instanceof( LiveRange );
  63. range.detach();
  64. } );
  65. it( 'createFromRange should return LiveRange', () => {
  66. const range = LiveRange.createFromRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ) );
  67. expect( range ).to.be.instanceof( LiveRange );
  68. range.detach();
  69. } );
  70. // Examples may seem weird when you compare them with the tree structure generated at the beginning of tests.
  71. // Since change event is fired _after_ operation is executed on tree model, you have to imagine that generated
  72. // structure is representing what is _after_ operation is executed. So live LiveRange properties are describing
  73. // virtual tree that is not existing anymore and event ranges are operating on the tree generated above.
  74. describe( 'should get transformed if', () => {
  75. let live, spy;
  76. beforeEach( () => {
  77. live = new LiveRange( new Position( root, [ 0, 1, 4 ] ), new Position( root, [ 0, 2, 2 ] ) );
  78. spy = sinon.spy();
  79. live.on( 'change', spy );
  80. } );
  81. afterEach( () => {
  82. live.detach();
  83. } );
  84. describe( 'insertion', () => {
  85. it( 'is in the same parent as range start and before it', () => {
  86. const insertRange = new Range( new Position( root, [ 0, 1, 0 ] ), new Position( root, [ 0, 1, 4 ] ) );
  87. doc.fire( 'change', 'insert', { range: insertRange }, null );
  88. expect( live.start.path ).to.deep.equal( [ 0, 1, 8 ] );
  89. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  90. expect( spy.calledOnce ).to.be.true;
  91. } );
  92. it( 'is in the same parent as range end and before it', () => {
  93. const insertRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 3 ] ) );
  94. doc.fire( 'change', 'insert', { range: insertRange }, null );
  95. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  96. expect( live.end.path ).to.deep.equal( [ 0, 2, 5 ] );
  97. expect( spy.calledOnce ).to.be.true;
  98. } );
  99. it( 'is at a position before a node from range start path', () => {
  100. const insertRange = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 2 ] ) );
  101. doc.fire( 'change', 'insert', { range: insertRange }, null );
  102. expect( live.start.path ).to.deep.equal( [ 0, 3, 4 ] );
  103. expect( live.end.path ).to.deep.equal( [ 0, 4, 2 ] );
  104. expect( spy.calledOnce ).to.be.true;
  105. } );
  106. it( 'is at a position before a node from range end path', () => {
  107. const insertRange = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 3 ] ) );
  108. doc.fire( 'change', 'insert', { range: insertRange }, null );
  109. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  110. expect( live.end.path ).to.deep.equal( [ 0, 3, 2 ] );
  111. expect( spy.calledOnce ).to.be.true;
  112. } );
  113. it( 'is at the live range start position and live range is collapsed', () => {
  114. live.end.path = [ 0, 1, 4 ];
  115. const insertRange = new Range( new Position( root, [ 0, 1, 4 ] ), new Position( root, [ 0, 1, 8 ] ) );
  116. doc.fire( 'change', 'insert', { range: insertRange }, null );
  117. expect( live.start.path ).to.deep.equal( [ 0, 1, 8 ] );
  118. expect( live.end.path ).to.deep.equal( [ 0, 1, 8 ] );
  119. expect( spy.calledOnce ).to.be.true;
  120. } );
  121. } );
  122. describe( 'range move', () => {
  123. it( 'is to the same parent as range start and before it', () => {
  124. const moveSource = new Position( root, [ 2 ] );
  125. const moveRange = new Range( new Position( root, [ 0, 1, 0 ] ), new Position( root, [ 0, 1, 4 ] ) );
  126. const changes = {
  127. range: moveRange,
  128. sourcePosition: moveSource
  129. };
  130. doc.fire( 'change', 'move', changes, null );
  131. expect( live.start.path ).to.deep.equal( [ 0, 1, 8 ] );
  132. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  133. expect( spy.calledOnce ).to.be.true;
  134. } );
  135. it( 'is to the same parent as range end and before it', () => {
  136. const moveSource = new Position( root, [ 3 ] );
  137. const moveRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 4 ] ) );
  138. const changes = {
  139. range: moveRange,
  140. sourcePosition: moveSource
  141. };
  142. doc.fire( 'change', 'move', changes, null );
  143. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  144. expect( live.end.path ).to.deep.equal( [ 0, 2, 6 ] );
  145. expect( spy.calledOnce ).to.be.true;
  146. } );
  147. it( 'is to a position before a node from range start path', () => {
  148. const moveSource = new Position( root, [ 2 ] );
  149. const moveRange = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 2 ] ) );
  150. const changes = {
  151. range: moveRange,
  152. sourcePosition: moveSource
  153. };
  154. doc.fire( 'change', 'move', changes, null );
  155. expect( live.start.path ).to.deep.equal( [ 0, 3, 4 ] );
  156. expect( live.end.path ).to.deep.equal( [ 0, 4, 2 ] );
  157. expect( spy.calledOnce ).to.be.true;
  158. } );
  159. it( 'is to a position before a node from range end path', () => {
  160. const moveSource = new Position( root, [ 2 ] );
  161. const moveRange = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 3 ] ) );
  162. const changes = {
  163. range: moveRange,
  164. sourcePosition: moveSource
  165. };
  166. doc.fire( 'change', 'move', changes, null );
  167. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  168. expect( live.end.path ).to.deep.equal( [ 0, 3, 2 ] );
  169. expect( spy.calledOnce ).to.be.true;
  170. } );
  171. it( 'is from the same parent as range start and before it', () => {
  172. const moveSource = new Position( root, [ 0, 1, 0 ] );
  173. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 3 ] ) );
  174. const changes = {
  175. range: moveRange,
  176. sourcePosition: moveSource
  177. };
  178. doc.fire( 'change', 'move', changes, null );
  179. expect( live.start.path ).to.deep.equal( [ 0, 1, 1 ] );
  180. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  181. expect( spy.calledOnce ).to.be.true;
  182. } );
  183. it( 'is from the same parent as range end and before it', () => {
  184. const moveSource = new Position( root, [ 0, 2, 0 ] );
  185. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 2 ] ) );
  186. const changes = {
  187. range: moveRange,
  188. sourcePosition: moveSource
  189. };
  190. doc.fire( 'change', 'move', changes, null );
  191. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  192. expect( live.end.path ).to.deep.equal( [ 0, 2, 0 ] );
  193. expect( spy.calledOnce ).to.be.true;
  194. } );
  195. it( 'is from a position before a node from range start path', () => {
  196. const moveSource = new Position( root, [ 0, 0 ] );
  197. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 1 ] ) );
  198. const changes = {
  199. range: moveRange,
  200. sourcePosition: moveSource
  201. };
  202. doc.fire( 'change', 'move', changes, null );
  203. expect( live.start.path ).to.deep.equal( [ 0, 0, 4 ] );
  204. expect( live.end.path ).to.deep.equal( [ 0, 1, 2 ] );
  205. expect( spy.calledOnce ).to.be.true;
  206. } );
  207. it( 'intersects on live range left side', () => {
  208. const moveSource = new Position( root, [ 0, 1, 2 ] );
  209. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  210. const changes = {
  211. range: moveRange,
  212. sourcePosition: moveSource
  213. };
  214. doc.fire( 'change', 'move', changes, null );
  215. expect( live.start.path ).to.deep.equal( [ 0, 1, 2 ] );
  216. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  217. expect( spy.calledOnce ).to.be.true;
  218. } );
  219. it( 'intersects on live range right side', () => {
  220. const moveSource = new Position( root, [ 0, 2, 1 ] );
  221. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  222. const changes = {
  223. range: moveRange,
  224. sourcePosition: moveSource
  225. };
  226. doc.fire( 'change', 'move', changes, null );
  227. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  228. expect( live.end.path ).to.deep.equal( [ 2, 1 ] ); // Included some nodes.
  229. expect( spy.calledOnce ).to.be.true;
  230. } );
  231. it( 'intersects with live range and is moved into live range', () => {
  232. const moveSource = new Position( root, [ 0, 2, 1 ] );
  233. const moveRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 5 ] ) );
  234. const changes = {
  235. range: moveRange,
  236. sourcePosition: moveSource
  237. };
  238. doc.fire( 'change', 'move', changes, null );
  239. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  240. expect( live.end.path ).to.deep.equal( [ 0, 2, 1 ] );
  241. expect( spy.calledOnce ).to.be.true;
  242. } );
  243. it( 'is equal to live range', () => {
  244. live.end.path = [ 0, 1, 7 ];
  245. const moveSource = new Position( root, [ 0, 1, 4 ] );
  246. const moveRange = new Range( new Position( root, [ 0, 3, 0 ] ), new Position( root, [ 0, 3, 3 ] ) );
  247. const changes = {
  248. range: moveRange,
  249. sourcePosition: moveSource
  250. };
  251. doc.fire( 'change', 'move', changes, null );
  252. expect( live.start.path ).to.deep.equal( [ 0, 3, 0 ] );
  253. expect( live.end.path ).to.deep.equal( [ 0, 3, 3 ] );
  254. expect( spy.calledOnce ).to.be.true;
  255. } );
  256. it( 'contains live range', () => {
  257. live.end.path = [ 0, 1, 7 ];
  258. const moveSource = new Position( root, [ 0, 1, 3 ] );
  259. const moveRange = new Range( new Position( root, [ 0, 3, 0 ] ), new Position( root, [ 0, 3, 9 ] ) );
  260. const changes = {
  261. range: moveRange,
  262. sourcePosition: moveSource
  263. };
  264. doc.fire( 'change', 'move', changes, null );
  265. expect( live.start.path ).to.deep.equal( [ 0, 3, 1 ] );
  266. expect( live.end.path ).to.deep.equal( [ 0, 3, 4 ] );
  267. expect( spy.calledOnce ).to.be.true;
  268. } );
  269. it( 'is intersecting with live range and points to live range', () => {
  270. live.end.path = [ 0, 1, 12 ];
  271. const moveSource = new Position( root, [ 0, 1, 2 ] );
  272. const moveRange = new Range( new Position( root, [ 0, 1, 7 ] ), new Position( root, [ 0, 1, 10 ] ) );
  273. const changes = {
  274. range: moveRange,
  275. sourcePosition: moveSource
  276. };
  277. doc.fire( 'change', 'move', changes, null );
  278. expect( live.start.path ).to.deep.equal( [ 0, 1, 9 ] );
  279. expect( live.end.path ).to.deep.equal( [ 0, 1, 12 ] );
  280. expect( spy.calledOnce ).to.be.true;
  281. } );
  282. } );
  283. describe( 'wrap', () => {
  284. // NOTE: it overrides the variable defined globally in these tests.
  285. // These tests need to be rewritten to use the batch API anyway and then this variable can be removed.
  286. let live;
  287. beforeEach( () => {
  288. doc.schema.registerItem( 'p', '$block' );
  289. doc.schema.registerItem( 'w' );
  290. doc.schema.allow( { name: 'p', inside: 'w' } );
  291. doc.schema.allow( { name: 'w', inside: '$root' } );
  292. } );
  293. afterEach( () => {
  294. live.detach();
  295. } );
  296. it( 'is inside the wrapped range', () => {
  297. setData( doc, '<p>x</p><p>[a]</p><p>x</p>' );
  298. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  299. // [<p>a</p>]
  300. doc.batch().wrap( new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) ), 'w' );
  301. expect( stringify( root, live ) ).to.equal( '<p>x</p><w><p>[a]</p></w><p>x</p>' );
  302. } );
  303. it( 'its start is intersecting with the wrapped range', () => {
  304. setData( doc, '<p>a[b</p><p>x</p><p>c]d</p>' );
  305. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  306. // [<p>ab</p>]
  307. doc.batch().wrap( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ), 'w' );
  308. expect( stringify( root, live ) ).to.equal( '<w><p>a[b</p></w><p>x</p><p>c]d</p>' );
  309. } );
  310. it( 'its end is intersecting with the wrapped range', () => {
  311. setData( doc, '<p>a[b</p><p>x</p><p>c]d</p>' );
  312. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  313. // [<p>cd</p>]
  314. doc.batch().wrap( new Range( new Position( root, [ 2 ] ), new Position( root, [ 3 ] ) ), 'w' );
  315. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><w><p>c]d</p></w>' );
  316. } );
  317. it( 'its start is intersecting with the wrapped range (multilpe elements)', () => {
  318. setData( doc, '<p>a[b</p><p>x</p><p>c]d</p>' );
  319. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  320. // [<p>ab</p><p>x</p>]
  321. doc.batch().wrap( new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ), 'w' );
  322. expect( stringify( root, live ) ).to.equal( '<w><p>a[b</p><p>x</p></w><p>c]d</p>' );
  323. } );
  324. it( 'its end is intersecting with the wrapped range (multiple elements)', () => {
  325. setData( doc, '<p>a[b</p><p>x</p><p>c]d</p>' );
  326. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  327. // [<p>x</p><p>cd</p>]
  328. doc.batch().wrap( new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ), 'w' );
  329. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><w><p>x</p><p>c]d</p></w>' );
  330. } );
  331. it( 'contains element to wrap', () => {
  332. setData( doc, '<p>a[b</p><p>x</p><p>c]d</p>' );
  333. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  334. // [<p>x</p>]
  335. doc.batch().wrap( new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) ), 'w' );
  336. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><w><p>x</p></w><p>c]d</p>' );
  337. } );
  338. } );
  339. describe( 'unwrap', () => {
  340. // NOTE: it overrides the variable defined globally in these tests.
  341. // These tests need to be rewritten to use the batch API anyway and then this variable can be removed.
  342. let live;
  343. beforeEach( () => {
  344. doc.schema.registerItem( 'p', '$block' );
  345. doc.schema.registerItem( 'w' );
  346. doc.schema.allow( { name: 'p', inside: 'w' } );
  347. doc.schema.allow( { name: 'w', inside: '$root' } );
  348. } );
  349. afterEach( () => {
  350. live.detach();
  351. } );
  352. it( 'is inside the wrapper to remove', () => {
  353. setData( doc, '<p>x</p><w><p>[a]</p></w><p>x</p>' );
  354. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  355. doc.batch().unwrap( root.getChild( 1 ) );
  356. expect( stringify( root, live ) ).to.equal( '<p>x</p><p>[a]</p><p>x</p>' );
  357. } );
  358. it( 'its start is intersecting with the wrapper to remove', () => {
  359. setData( doc, '<w><p>a[b</p></w><p>c]d</p>' );
  360. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  361. doc.batch().unwrap( root.getChild( 0 ) );
  362. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>c]d</p>' );
  363. } );
  364. it( 'its end is intersecting with the wrapper to remove', () => {
  365. setData( doc, '<p>a[b</p><w><p>c]d</p></w>' );
  366. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  367. doc.batch().unwrap( root.getChild( 1 ) );
  368. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>c]d</p>' );
  369. } );
  370. it( 'its start is intersecting with the wrapper to remove (multiple elements)', () => {
  371. setData( doc, '<w><p>a[b</p><p>x</p></w><p>c]d</p>' );
  372. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  373. doc.batch().unwrap( root.getChild( 0 ) );
  374. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><p>c]d</p>' );
  375. } );
  376. it( 'its end is intersecting with the wrapper to remove (multiple elements)', () => {
  377. setData( doc, '<p>a[b</p><w><p>x</p><p>c]d</p></w>' );
  378. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  379. doc.batch().unwrap( root.getChild( 1 ) );
  380. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><p>c]d</p>' );
  381. } );
  382. it( 'contains wrapped element', () => {
  383. setData( doc, '<p>a[b</p><w><p>x</p></w><p>c]d</p>' );
  384. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  385. doc.batch().unwrap( root.getChild( 1 ) );
  386. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><p>c]d</p>' );
  387. } );
  388. } );
  389. } );
  390. describe( 'should not get transformed if', () => {
  391. let otherRoot, spy, live, clone;
  392. before( () => {
  393. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  394. } );
  395. beforeEach( () => {
  396. live = new LiveRange( new Position( root, [ 0, 1, 4 ] ), new Position( root, [ 0, 2, 2 ] ) );
  397. clone = Range.createFromRange( live );
  398. spy = sinon.spy();
  399. live.on( 'change', spy );
  400. } );
  401. afterEach( () => {
  402. live.detach();
  403. } );
  404. describe( 'insertion', () => {
  405. it( 'is in the same parent as range start and after it', () => {
  406. const insertRange = new Range( new Position( root, [ 0, 1, 7 ] ), new Position( root, [ 0, 1, 9 ] ) );
  407. doc.fire( 'change', 'insert', { range: insertRange }, null );
  408. expect( live.isEqual( clone ) ).to.be.true;
  409. expect( spy.called ).to.be.false;
  410. } );
  411. it( 'is in the same parent as range end and after it', () => {
  412. const insertRange = new Range( new Position( root, [ 0, 2, 7 ] ), new Position( root, [ 0, 2, 9 ] ) );
  413. doc.fire( 'change', 'insert', { range: insertRange }, null );
  414. expect( live.isEqual( clone ) ).to.be.true;
  415. expect( spy.called ).to.be.false;
  416. } );
  417. it( 'is to a position after a node from range end path', () => {
  418. const insertRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 4 ] ) );
  419. doc.fire( 'change', 'insert', { range: insertRange }, null );
  420. expect( live.isEqual( clone ) ).to.be.true;
  421. expect( spy.called ).to.be.false;
  422. } );
  423. it( 'is in different root', () => {
  424. const insertRange = new Range( new Position( otherRoot, [ 0, 0 ] ), new Position( otherRoot, [ 0, 2 ] ) );
  425. doc.fire( 'change', 'insert', { range: insertRange }, null );
  426. expect( live.isEqual( clone ) ).to.be.true;
  427. expect( spy.called ).to.be.false;
  428. } );
  429. } );
  430. describe( 'range move', () => {
  431. it( 'is to the same parent as range start and after it', () => {
  432. const moveSource = new Position( root, [ 4 ] );
  433. const moveRange = new Range( new Position( root, [ 0, 1, 7 ] ), new Position( root, [ 0, 1, 9 ] ) );
  434. const changes = {
  435. range: moveRange,
  436. sourcePosition: moveSource
  437. };
  438. doc.fire( 'change', 'move', changes, null );
  439. expect( live.isEqual( clone ) ).to.be.true;
  440. expect( spy.called ).to.be.false;
  441. } );
  442. it( 'is to the same parent as range end and after it', () => {
  443. const moveSource = new Position( root, [ 4 ] );
  444. const moveRange = new Range( new Position( root, [ 0, 2, 3 ] ), new Position( root, [ 0, 2, 5 ] ) );
  445. const changes = {
  446. range: moveRange,
  447. sourcePosition: moveSource
  448. };
  449. doc.fire( 'change', 'move', changes, null );
  450. expect( live.isEqual( clone ) ).to.be.true;
  451. expect( spy.called ).to.be.false;
  452. } );
  453. it( 'is to a position after a node from range end path', () => {
  454. const moveSource = new Position( root, [ 4 ] );
  455. const moveRange = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 5 ] ) );
  456. const changes = {
  457. range: moveRange,
  458. sourcePosition: moveSource
  459. };
  460. doc.fire( 'change', 'move', changes, null );
  461. expect( live.isEqual( clone ) ).to.be.true;
  462. expect( spy.called ).to.be.false;
  463. } );
  464. it( 'is from the same parent as range start and after it', () => {
  465. const moveSource = new Position( root, [ 0, 1, 6 ] );
  466. const moveRange = new Range( new Position( root, [ 4, 0 ] ), new Position( root, [ 4, 3 ] ) );
  467. const changes = {
  468. range: moveRange,
  469. sourcePosition: moveSource
  470. };
  471. doc.fire( 'change', 'move', changes, null );
  472. expect( live.isEqual( clone ) ).to.be.true;
  473. expect( spy.called ).to.be.false;
  474. } );
  475. it( 'is from the same parent as range end and after it', () => {
  476. const moveSource = new Position( root, [ 0, 2, 4 ] );
  477. const moveRange = new Range( new Position( root, [ 4, 0 ] ), new Position( root, [ 4, 2 ] ) );
  478. const changes = {
  479. range: moveRange,
  480. sourcePosition: moveSource
  481. };
  482. doc.fire( 'change', 'move', changes, null );
  483. expect( live.isEqual( clone ) ).to.be.true;
  484. expect( spy.called ).to.be.false;
  485. } );
  486. it( 'is from a position after a node from range end path', () => {
  487. const moveSource = new Position( root, [ 0, 3 ] );
  488. const moveRange = new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 5, 1 ] ) );
  489. const changes = {
  490. range: moveRange,
  491. sourcePosition: moveSource
  492. };
  493. doc.fire( 'change', 'move', changes, null );
  494. expect( live.isEqual( clone ) ).to.be.true;
  495. expect( spy.called ).to.be.false;
  496. } );
  497. it( 'is to different root', () => {
  498. const moveSource = new Position( root, [ 2 ] );
  499. const moveRange = new Range( new Position( otherRoot, [ 0, 1, 0 ] ), new Position( otherRoot, [ 0, 1, 4 ] ) );
  500. const changes = {
  501. range: moveRange,
  502. sourcePosition: moveSource
  503. };
  504. doc.fire( 'change', 'move', changes, null );
  505. expect( live.isEqual( clone ) ).to.be.true;
  506. expect( spy.called ).to.be.false;
  507. } );
  508. it( 'is from different root', () => {
  509. const moveSource = new Position( otherRoot, [ 0, 2, 0 ] );
  510. const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 2 ] ) );
  511. const changes = {
  512. range: moveRange,
  513. sourcePosition: moveSource
  514. };
  515. doc.fire( 'change', 'move', changes, null );
  516. expect( live.isEqual( clone ) ).to.be.true;
  517. expect( spy.called ).to.be.false;
  518. } );
  519. it( 'is inside live range and points to live range', () => {
  520. live.end.path = [ 0, 1, 12 ];
  521. const moveSource = new Position( root, [ 0, 1, 6 ] );
  522. const moveRange = new Range( new Position( root, [ 0, 1, 8 ] ), new Position( root, [ 0, 1, 10 ] ) );
  523. const changes = {
  524. range: moveRange,
  525. sourcePosition: moveSource
  526. };
  527. doc.fire( 'change', 'move', changes, null );
  528. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  529. expect( live.end.path ).to.deep.equal( [ 0, 1, 12 ] );
  530. expect( spy.calledOnce ).to.be.false;
  531. } );
  532. } );
  533. } );
  534. } );