liverange.js 25 KB

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