liverange.js 25 KB

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