liverange.js 29 KB

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