liverange.js 28 KB

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