liverange.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. let 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. let 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. let 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. let 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. let range = LiveRange.createIn( p );
  52. expect( range ).to.be.instanceof( LiveRange );
  53. range.detach();
  54. } );
  55. it( 'createFromParentsAndOffsets should return LiveRange', () => {
  56. let 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. let 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. let 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. let 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. let 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. let 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. let 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. let 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. let moveSource = new Position( root, [ 2 ] );
  125. let moveRange = new Range( new Position( root, [ 0, 1, 0 ] ), new Position( root, [ 0, 1, 4 ] ) );
  126. let 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. let moveSource = new Position( root, [ 3 ] );
  137. let moveRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 4 ] ) );
  138. let 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. let moveSource = new Position( root, [ 2 ] );
  149. let moveRange = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 2 ] ) );
  150. let 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. let moveSource = new Position( root, [ 2 ] );
  161. let moveRange = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 3 ] ) );
  162. let 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. let moveSource = new Position( root, [ 0, 1, 0 ] );
  173. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 3 ] ) );
  174. let 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. let moveSource = new Position( root, [ 0, 2, 0 ] );
  185. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 2 ] ) );
  186. let 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. let moveSource = new Position( root, [ 0, 0 ] );
  197. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 1 ] ) );
  198. let 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. let moveSource = new Position( root, [ 0, 1, 2 ] );
  209. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  210. let 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. let moveSource = new Position( root, [ 0, 2, 1 ] );
  221. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
  222. let 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( [ 0, 2, 1 ] );
  229. expect( spy.calledOnce ).to.be.true;
  230. } );
  231. it( 'intersects with live range and is moved into live range', () => {
  232. let moveSource = new Position( root, [ 0, 2, 1 ] );
  233. let moveRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 5 ] ) );
  234. let 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, 6 ] );
  241. expect( spy.calledOnce ).to.be.true;
  242. } );
  243. it( 'is equal to live range', () => {
  244. live.end.path = [ 0, 1, 7 ];
  245. let moveSource = new Position( root, [ 0, 1, 4 ] );
  246. let moveRange = new Range( new Position( root, [ 0, 3, 0 ] ), new Position( root, [ 0, 3, 3 ] ) );
  247. let 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. let moveSource = new Position( root, [ 0, 1, 3 ] );
  259. let moveRange = new Range( new Position( root, [ 0, 3, 0 ] ), new Position( root, [ 0, 3, 9 ] ) );
  260. let 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. let moveSource = new Position( root, [ 0, 1, 2 ] );
  272. let moveRange = new Range( new Position( root, [ 0, 1, 7 ] ), new Position( root, [ 0, 1, 10 ] ) );
  273. let 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, 2 ] );
  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. } );
  332. describe( 'unwrap', () => {
  333. // NOTE: it overrides the variable defined globally in these tests.
  334. // These tests need to be rewritten to use the batch API anyway and then this variable can be removed.
  335. let live;
  336. beforeEach( () => {
  337. doc.schema.registerItem( 'p', '$block' );
  338. doc.schema.registerItem( 'w' );
  339. doc.schema.allow( { name: 'p', inside: 'w' } );
  340. doc.schema.allow( { name: 'w', inside: '$root' } );
  341. } );
  342. afterEach( () => {
  343. live.detach();
  344. } );
  345. it( 'is inside the wrapper to remove', () => {
  346. setData( doc, '<p>x</p><w><p>[a]</p></w><p>x</p>' );
  347. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  348. doc.batch().unwrap( root.getChild( 1 ) );
  349. expect( stringify( root, live ) ).to.equal( '<p>x</p><p>[a]</p><p>x</p>' );
  350. } );
  351. it( 'its start is intersecting with the wrapper to remove', () => {
  352. setData( doc, '<w><p>a[b</p></w><p>c]d</p>' );
  353. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  354. doc.batch().unwrap( root.getChild( 0 ) );
  355. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>c]d</p>' );
  356. } );
  357. it( 'its end is intersecting with the wrapper to remove', () => {
  358. setData( doc, '<p>a[b</p><w><p>c]d</p></w>' );
  359. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  360. doc.batch().unwrap( root.getChild( 1 ) );
  361. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>c]d</p>' );
  362. } );
  363. it( 'its start is intersecting with the wrapper to remove (multiple elements)', () => {
  364. setData( doc, '<w><p>a[b</p><p>x</p></w><p>c]d</p>' );
  365. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  366. doc.batch().unwrap( root.getChild( 0 ) );
  367. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><p>c]d</p>' );
  368. } );
  369. it( 'its end is intersecting with the wrapper to remove', () => {
  370. setData( doc, '<p>a[b</p><w><p>x</p><p>c]d</p></w>' );
  371. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  372. doc.batch().unwrap( root.getChild( 1 ) );
  373. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><p>c]d</p>' );
  374. } );
  375. } );
  376. } );
  377. describe( 'should not get transformed if', () => {
  378. let otherRoot, spy;
  379. before( () => {
  380. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  381. } );
  382. let live, clone;
  383. beforeEach( () => {
  384. live = new LiveRange( new Position( root, [ 0, 1, 4 ] ), new Position( root, [ 0, 2, 2 ] ) );
  385. clone = Range.createFromRange( live );
  386. spy = sinon.spy();
  387. live.on( 'change', spy );
  388. } );
  389. afterEach( () => {
  390. live.detach();
  391. } );
  392. describe( 'insertion', () => {
  393. it( 'is in the same parent as range start and after it', () => {
  394. let insertRange = new Range( new Position( root, [ 0, 1, 7 ] ), new Position( root, [ 0, 1, 9 ] ) );
  395. doc.fire( 'change', 'insert', { range: insertRange }, null );
  396. expect( live.isEqual( clone ) ).to.be.true;
  397. expect( spy.called ).to.be.false;
  398. } );
  399. it( 'is in the same parent as range end and after it', () => {
  400. let insertRange = new Range( new Position( root, [ 0, 2, 7 ] ), new Position( root, [ 0, 2, 9 ] ) );
  401. doc.fire( 'change', 'insert', { range: insertRange }, null );
  402. expect( live.isEqual( clone ) ).to.be.true;
  403. expect( spy.called ).to.be.false;
  404. } );
  405. it( 'is to a position after a node from range end path', () => {
  406. let insertRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 4 ] ) );
  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 different root', () => {
  412. let insertRange = new Range( new Position( otherRoot, [ 0, 0 ] ), new Position( otherRoot, [ 0, 2 ] ) );
  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. } );
  418. describe( 'range move', () => {
  419. it( 'is to the same parent as range start and after it', () => {
  420. let moveSource = new Position( root, [ 4 ] );
  421. let moveRange = new Range( new Position( root, [ 0, 1, 7 ] ), new Position( root, [ 0, 1, 9 ] ) );
  422. let changes = {
  423. range: moveRange,
  424. sourcePosition: moveSource
  425. };
  426. doc.fire( 'change', 'move', changes, null );
  427. expect( live.isEqual( clone ) ).to.be.true;
  428. expect( spy.called ).to.be.false;
  429. } );
  430. it( 'is to the same parent as range end and after it', () => {
  431. let moveSource = new Position( root, [ 4 ] );
  432. let moveRange = new Range( new Position( root, [ 0, 2, 3 ] ), new Position( root, [ 0, 2, 5 ] ) );
  433. let changes = {
  434. range: moveRange,
  435. sourcePosition: moveSource
  436. };
  437. doc.fire( 'change', 'move', changes, null );
  438. expect( live.isEqual( clone ) ).to.be.true;
  439. expect( spy.called ).to.be.false;
  440. } );
  441. it( 'is to a position after a node from range end path', () => {
  442. let moveSource = new Position( root, [ 4 ] );
  443. let moveRange = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 5 ] ) );
  444. let changes = {
  445. range: moveRange,
  446. sourcePosition: moveSource
  447. };
  448. doc.fire( 'change', 'move', changes, null );
  449. expect( live.isEqual( clone ) ).to.be.true;
  450. expect( spy.called ).to.be.false;
  451. } );
  452. it( 'is from the same parent as range start and after it', () => {
  453. let moveSource = new Position( root, [ 0, 1, 6 ] );
  454. let moveRange = new Range( new Position( root, [ 4, 0 ] ), new Position( root, [ 4, 3 ] ) );
  455. let changes = {
  456. range: moveRange,
  457. sourcePosition: moveSource
  458. };
  459. doc.fire( 'change', 'move', changes, null );
  460. expect( live.isEqual( clone ) ).to.be.true;
  461. expect( spy.called ).to.be.false;
  462. } );
  463. it( 'is from the same parent as range end and after it', () => {
  464. let moveSource = new Position( root, [ 0, 2, 4 ] );
  465. let moveRange = new Range( new Position( root, [ 4, 0 ] ), new Position( root, [ 4, 2 ] ) );
  466. let changes = {
  467. range: moveRange,
  468. sourcePosition: moveSource
  469. };
  470. doc.fire( 'change', 'move', changes, null );
  471. expect( live.isEqual( clone ) ).to.be.true;
  472. expect( spy.called ).to.be.false;
  473. } );
  474. it( 'is from a position after a node from range end path', () => {
  475. let moveSource = new Position( root, [ 0, 3 ] );
  476. let moveRange = new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 5, 1 ] ) );
  477. let changes = {
  478. range: moveRange,
  479. sourcePosition: moveSource
  480. };
  481. doc.fire( 'change', 'move', changes, null );
  482. expect( live.isEqual( clone ) ).to.be.true;
  483. expect( spy.called ).to.be.false;
  484. } );
  485. it( 'is to different root', () => {
  486. let moveSource = new Position( root, [ 2 ] );
  487. let moveRange = new Range( new Position( otherRoot, [ 0, 1, 0 ] ), new Position( otherRoot, [ 0, 1, 4 ] ) );
  488. let changes = {
  489. range: moveRange,
  490. sourcePosition: moveSource
  491. };
  492. doc.fire( 'change', 'move', changes, null );
  493. expect( live.isEqual( clone ) ).to.be.true;
  494. expect( spy.called ).to.be.false;
  495. } );
  496. it( 'is from different root', () => {
  497. let moveSource = new Position( otherRoot, [ 0, 2, 0 ] );
  498. let moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 2 ] ) );
  499. let changes = {
  500. range: moveRange,
  501. sourcePosition: moveSource
  502. };
  503. doc.fire( 'change', 'move', changes, null );
  504. expect( live.isEqual( clone ) ).to.be.true;
  505. expect( spy.called ).to.be.false;
  506. } );
  507. it( 'is inside live range and points to live range', () => {
  508. live.end.path = [ 0, 1, 12 ];
  509. let moveSource = new Position( root, [ 0, 1, 6 ] );
  510. let moveRange = new Range( new Position( root, [ 0, 1, 8 ] ), new Position( root, [ 0, 1, 10 ] ) );
  511. let changes = {
  512. range: moveRange,
  513. sourcePosition: moveSource
  514. };
  515. doc.fire( 'change', 'move', changes, null );
  516. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  517. expect( live.end.path ).to.deep.equal( [ 0, 1, 12 ] );
  518. expect( spy.calledOnce ).to.be.false;
  519. } );
  520. } );
  521. } );
  522. } );