liverange.js 28 KB

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