liverange.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. new Position( gy, [ 0, 0 ] ),
  136. new Position( gy, [ 0 ] ),
  137. model.document.version + 1
  138. );
  139. batch.addOperation( remove );
  140. model.applyOperation( remove );
  141. batch.addOperation( merge );
  142. model.applyOperation( merge );
  143. } );
  144. // Second parameter is deletion position.
  145. expect( spy.args[ 1 ][ 2 ].deletionPosition.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  146. } );
  147. describe( 'should get transformed and fire change:range if', () => {
  148. let live, spy;
  149. beforeEach( () => {
  150. live = new LiveRange( new Position( root, [ 0, 1, 4 ] ), new Position( root, [ 0, 2, 2 ] ) );
  151. spy = sinon.spy();
  152. live.on( 'change:range', spy );
  153. } );
  154. afterEach( () => {
  155. live.detach();
  156. } );
  157. describe( 'insertion', () => {
  158. it( 'is in the same parent as range start and before it', () => {
  159. model.change( writer => {
  160. writer.insertText( 'xxx', new Position( root, [ 0, 1, 0 ] ) );
  161. } );
  162. expect( live.start.path ).to.deep.equal( [ 0, 1, 7 ] );
  163. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  164. expect( spy.calledOnce ).to.be.true;
  165. } );
  166. it( 'is in the same parent as range end and before it', () => {
  167. model.change( writer => {
  168. writer.insertText( 'xxx', new Position( root, [ 0, 2, 0 ] ) );
  169. } );
  170. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  171. expect( live.end.path ).to.deep.equal( [ 0, 2, 5 ] );
  172. expect( spy.calledOnce ).to.be.true;
  173. } );
  174. it( 'is at a position before a node from range start path', () => {
  175. model.change( writer => {
  176. writer.insert( new Element( 'li' ), new Position( root, [ 0, 0 ] ) );
  177. } );
  178. expect( live.start.path ).to.deep.equal( [ 0, 2, 4 ] );
  179. expect( live.end.path ).to.deep.equal( [ 0, 3, 2 ] );
  180. expect( spy.calledOnce ).to.be.true;
  181. } );
  182. it( 'is at a position before a node from range end path', () => {
  183. model.change( writer => {
  184. writer.insert( new Element( 'li' ), new Position( root, [ 0, 2 ] ) );
  185. } );
  186. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  187. expect( live.end.path ).to.deep.equal( [ 0, 3, 2 ] );
  188. expect( spy.calledOnce ).to.be.true;
  189. } );
  190. it( 'is at the live range start position and live range is collapsed', () => {
  191. live.end.path = [ 0, 1, 4 ];
  192. model.change( writer => {
  193. writer.insertText( 'xxx', new Position( root, [ 0, 1, 4 ] ) );
  194. } );
  195. expect( live.start.path ).to.deep.equal( [ 0, 1, 7 ] );
  196. expect( live.end.path ).to.deep.equal( [ 0, 1, 7 ] );
  197. expect( spy.calledOnce ).to.be.true;
  198. } );
  199. } );
  200. describe( 'range move', () => {
  201. it( 'is to the same parent as range start and before it', () => {
  202. model.change( writer => {
  203. const sourcePosition = new Position( root, [ 0, 4, 0 ] );
  204. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 4 );
  205. const targetPosition = new Position( root, [ 0, 1, 0 ] );
  206. writer.move( sourceRange, targetPosition );
  207. } );
  208. expect( live.start.path ).to.deep.equal( [ 0, 1, 8 ] );
  209. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  210. expect( spy.calledOnce ).to.be.true;
  211. } );
  212. it( 'is to the same parent as range end and before it', () => {
  213. model.change( writer => {
  214. const sourcePosition = new Position( root, [ 0, 4, 0 ] );
  215. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 4 );
  216. const targetPosition = new Position( root, [ 0, 2, 0 ] );
  217. writer.move( sourceRange, targetPosition );
  218. } );
  219. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  220. expect( live.end.path ).to.deep.equal( [ 0, 2, 6 ] );
  221. expect( spy.calledOnce ).to.be.true;
  222. } );
  223. it( 'is to a position before a node from range start path', () => {
  224. model.change( writer => {
  225. const sourcePosition = new Position( root, [ 0, 4 ] );
  226. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 2 );
  227. const targetPosition = new Position( root, [ 0, 0 ] );
  228. writer.move( sourceRange, targetPosition );
  229. } );
  230. expect( live.start.path ).to.deep.equal( [ 0, 3, 4 ] );
  231. expect( live.end.path ).to.deep.equal( [ 0, 4, 2 ] );
  232. expect( spy.calledOnce ).to.be.true;
  233. } );
  234. it( 'is to a position before a node from range end path', () => {
  235. model.change( writer => {
  236. const sourcePosition = new Position( root, [ 0, 4 ] );
  237. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  238. const targetPosition = new Position( root, [ 0, 2 ] );
  239. writer.move( sourceRange, targetPosition );
  240. } );
  241. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  242. expect( live.end.path ).to.deep.equal( [ 0, 3, 2 ] );
  243. expect( spy.calledOnce ).to.be.true;
  244. } );
  245. it( 'is from the same parent as range start and before it', () => {
  246. model.change( writer => {
  247. const sourcePosition = new Position( root, [ 0, 1, 0 ] );
  248. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 3 );
  249. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  250. writer.move( sourceRange, targetPosition );
  251. } );
  252. expect( live.start.path ).to.deep.equal( [ 0, 1, 1 ] );
  253. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  254. expect( spy.calledOnce ).to.be.true;
  255. } );
  256. it( 'is from the same parent as range end and before it - #1', () => {
  257. model.change( writer => {
  258. const sourcePosition = new Position( root, [ 0, 2, 0 ] );
  259. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  260. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  261. writer.move( sourceRange, targetPosition );
  262. } );
  263. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  264. expect( live.end.path ).to.deep.equal( [ 0, 2, 1 ] );
  265. expect( spy.calledOnce ).to.be.true;
  266. } );
  267. it( 'is from the same parent as range end and before it - #2', () => {
  268. model.change( writer => {
  269. const sourcePosition = new Position( root, [ 0, 2, 0 ] );
  270. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 2 );
  271. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  272. writer.move( sourceRange, targetPosition );
  273. } );
  274. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  275. expect( live.end.path ).to.deep.equal( [ 0, 2, 0 ] );
  276. expect( spy.calledOnce ).to.be.true;
  277. } );
  278. it( 'is from a position before a node from range start path', () => {
  279. model.change( writer => {
  280. const sourcePosition = new Position( root, [ 0, 0 ] );
  281. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  282. const targetPosition = new Position( root, [ 0, 4 ] );
  283. writer.move( sourceRange, targetPosition );
  284. } );
  285. expect( live.start.path ).to.deep.equal( [ 0, 0, 4 ] );
  286. expect( live.end.path ).to.deep.equal( [ 0, 1, 2 ] );
  287. expect( spy.calledOnce ).to.be.true;
  288. } );
  289. it( 'intersects on live range left side', () => {
  290. model.change( writer => {
  291. const sourcePosition = new Position( root, [ 0, 1, 2 ] );
  292. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 4 );
  293. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  294. writer.move( sourceRange, targetPosition );
  295. } );
  296. expect( live.start.path ).to.deep.equal( [ 0, 1, 2 ] );
  297. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  298. expect( spy.calledOnce ).to.be.true;
  299. } );
  300. it( 'intersects on live range right side', () => {
  301. model.change( writer => {
  302. const sourcePosition = new Position( root, [ 0, 2, 1 ] );
  303. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 4 );
  304. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  305. writer.move( sourceRange, targetPosition );
  306. } );
  307. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  308. expect( live.end.path ).to.deep.equal( [ 0, 2, 1 ] );
  309. expect( spy.calledOnce ).to.be.true;
  310. } );
  311. it( 'is equal to live range', () => {
  312. live.end.path = [ 0, 1, 7 ];
  313. model.change( writer => {
  314. const sourcePosition = new Position( root, [ 0, 1, 4 ] );
  315. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 3 );
  316. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  317. writer.move( sourceRange, targetPosition );
  318. } );
  319. expect( live.start.path ).to.deep.equal( [ 0, 4, 0 ] );
  320. expect( live.end.path ).to.deep.equal( [ 0, 4, 3 ] );
  321. expect( spy.calledOnce ).to.be.true;
  322. } );
  323. it( 'contains live range', () => {
  324. live.end.path = [ 0, 1, 6 ];
  325. model.change( writer => {
  326. const sourcePosition = new Position( root, [ 0, 1, 3 ] );
  327. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 5 );
  328. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  329. writer.move( sourceRange, targetPosition );
  330. } );
  331. expect( live.start.path ).to.deep.equal( [ 0, 4, 1 ] );
  332. expect( live.end.path ).to.deep.equal( [ 0, 4, 3 ] );
  333. expect( spy.calledOnce ).to.be.true;
  334. } );
  335. it( 'is intersecting with live range on left and points to live range', () => {
  336. live.end.path = [ 0, 1, 7 ];
  337. model.change( writer => {
  338. const sourcePosition = new Position( root, [ 0, 1, 2 ] );
  339. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 3 );
  340. const targetPosition = new Position( root, [ 0, 1, 8 ] );
  341. writer.move( sourceRange, targetPosition );
  342. } );
  343. expect( live.start.path ).to.deep.equal( [ 0, 1, 2 ] );
  344. expect( live.end.path ).to.deep.equal( [ 0, 1, 4 ] );
  345. expect( spy.calledOnce ).to.be.true;
  346. } );
  347. it( 'is intersecting with live range on right and is moved into live range', () => {
  348. model.change( writer => {
  349. const sourcePosition = new Position( root, [ 0, 2, 1 ] );
  350. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 5 );
  351. const targetPosition = new Position( root, [ 0, 2, 0 ] );
  352. writer.move( sourceRange, targetPosition );
  353. } );
  354. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  355. expect( live.end.path ).to.deep.equal( [ 0, 2, 1 ] );
  356. expect( spy.calledOnce ).to.be.true;
  357. } );
  358. } );
  359. describe( 'wrap', () => {
  360. // NOTE: it overrides the variable defined globally in these tests.
  361. // These tests need to be rewritten to use the batch API anyway and then this variable can be removed.
  362. let live;
  363. beforeEach( () => {
  364. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  365. model.schema.register( 'w' );
  366. model.schema.extend( 'p', { allowIn: 'w' } );
  367. model.schema.extend( 'w', { allowIn: '$root' } );
  368. } );
  369. afterEach( () => {
  370. live.detach();
  371. } );
  372. it( 'is inside the wrapped range', () => {
  373. setData( model, '<p>x</p><p>[a]</p><p>x</p>' );
  374. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  375. model.change( writer => {
  376. // [<p>a</p>]
  377. writer.wrap( new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) ), 'w' );
  378. } );
  379. expect( stringify( root, live ) ).to.equal( '<p>x</p><w><p>[a]</p></w><p>x</p>' );
  380. } );
  381. it( 'its start is intersecting with the wrapped range', () => {
  382. setData( model, '<p>a[b</p><p>x</p><p>c]d</p>' );
  383. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  384. model.change( writer => {
  385. // [<p>ab</p>]
  386. writer.wrap( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ), 'w' );
  387. } );
  388. expect( stringify( root, live ) ).to.equal( '<w><p>a[b</p></w><p>x</p><p>c]d</p>' );
  389. } );
  390. it( 'its end is intersecting with the wrapped range', () => {
  391. setData( model, '<p>a[b</p><p>x</p><p>c]d</p>' );
  392. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  393. model.change( writer => {
  394. // [<p>cd</p>]
  395. writer.wrap( new Range( new Position( root, [ 2 ] ), new Position( root, [ 3 ] ) ), 'w' );
  396. } );
  397. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><w><p>c]d</p></w>' );
  398. } );
  399. it( 'its start is intersecting with the wrapped range (multilpe elements)', () => {
  400. setData( model, '<p>a[b</p><p>x</p><p>c]d</p>' );
  401. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  402. model.change( writer => {
  403. // [<p>ab</p><p>x</p>]
  404. writer.wrap( new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ), 'w' );
  405. } );
  406. expect( stringify( root, live ) ).to.equal( '<w><p>a[b</p><p>x</p></w><p>c]d</p>' );
  407. } );
  408. it( 'its end is intersecting with the wrapped range (multiple elements)', () => {
  409. setData( model, '<p>a[b</p><p>x</p><p>c]d</p>' );
  410. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  411. model.change( writer => {
  412. // [<p>x</p><p>cd</p>]
  413. writer.wrap( new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ), 'w' );
  414. } );
  415. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><w><p>x</p><p>c]d</p></w>' );
  416. } );
  417. it( 'contains element to wrap', () => {
  418. setData( model, '<p>a[b</p><p>x</p><p>c]d</p>' );
  419. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  420. model.change( writer => {
  421. // [<p>x</p>]
  422. writer.wrap( new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) ), 'w' );
  423. } );
  424. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><w><p>x</p></w><p>c]d</p>' );
  425. } );
  426. } );
  427. describe( 'unwrap', () => {
  428. // NOTE: it overrides the variable defined globally in these tests.
  429. // These tests need to be rewritten to use the batch API anyway and then this variable can be removed.
  430. let live;
  431. beforeEach( () => {
  432. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  433. model.schema.register( 'w' );
  434. model.schema.extend( 'p', { allowIn: 'w' } );
  435. model.schema.extend( 'w', { allowIn: '$root' } );
  436. } );
  437. afterEach( () => {
  438. live.detach();
  439. } );
  440. it( 'is inside the wrapper to remove', () => {
  441. setData( model, '<p>x</p><w><p>[a]</p></w><p>x</p>' );
  442. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  443. model.change( writer => {
  444. writer.unwrap( root.getChild( 1 ) );
  445. } );
  446. expect( stringify( root, live ) ).to.equal( '<p>x</p><p>[a]</p><p>x</p>' );
  447. } );
  448. it( 'its start is intersecting with the wrapper to remove', () => {
  449. setData( model, '<w><p>a[b</p></w><p>c]d</p>' );
  450. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  451. model.change( writer => {
  452. writer.unwrap( root.getChild( 0 ) );
  453. } );
  454. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>c]d</p>' );
  455. } );
  456. it( 'its end is intersecting with the wrapper to remove', () => {
  457. setData( model, '<p>a[b</p><w><p>c]d</p></w>' );
  458. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  459. model.change( writer => {
  460. writer.unwrap( root.getChild( 1 ) );
  461. } );
  462. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>c]d</p>' );
  463. } );
  464. it( 'its start is intersecting with the wrapper to remove (multiple elements)', () => {
  465. setData( model, '<w><p>a[b</p><p>x</p></w><p>c]d</p>' );
  466. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  467. model.change( writer => {
  468. writer.unwrap( root.getChild( 0 ) );
  469. } );
  470. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><p>c]d</p>' );
  471. } );
  472. it( 'its end is intersecting with the wrapper to remove (multiple elements)', () => {
  473. setData( model, '<p>a[b</p><w><p>x</p><p>c]d</p></w>' );
  474. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  475. model.change( writer => {
  476. writer.unwrap( root.getChild( 1 ) );
  477. } );
  478. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><p>c]d</p>' );
  479. } );
  480. it( 'contains wrapped element', () => {
  481. setData( model, '<p>a[b</p><w><p>x</p></w><p>c]d</p>' );
  482. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  483. model.change( writer => {
  484. writer.unwrap( root.getChild( 1 ) );
  485. } );
  486. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><p>c]d</p>' );
  487. } );
  488. } );
  489. } );
  490. describe( 'should not get transformed but fire change:content', () => {
  491. let spy, live, clone;
  492. beforeEach( () => {
  493. live = new LiveRange( new Position( root, [ 0, 1, 4 ] ), new Position( root, [ 0, 2, 2 ] ) );
  494. clone = Range.createFromRange( live );
  495. spy = sinon.spy();
  496. live.on( 'change:content', spy );
  497. } );
  498. afterEach( () => {
  499. live.detach();
  500. } );
  501. describe( 'insertion', () => {
  502. it( 'inside the range', () => {
  503. model.change( writer => {
  504. writer.insertText( 'xxx', new Position( root, [ 0, 1, 7 ] ) );
  505. } );
  506. expect( live.isEqual( clone ) ).to.be.true;
  507. expect( spy.calledOnce ).to.be.true;
  508. } );
  509. } );
  510. describe( 'range move', () => {
  511. it( 'inside the range', () => {
  512. model.change( writer => {
  513. const sourcePosition = new Position( root, [ 0, 4, 0 ] );
  514. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 3 );
  515. const targetPosition = new Position( root, [ 0, 1, 5 ] );
  516. writer.move( sourceRange, targetPosition );
  517. } );
  518. expect( live.isEqual( clone ) ).to.be.true;
  519. expect( spy.calledOnce ).to.be.true;
  520. } );
  521. it( 'from the range', () => {
  522. model.change( writer => {
  523. const sourcePosition = new Position( root, [ 0, 1, 5 ] );
  524. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 2 );
  525. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  526. writer.move( sourceRange, targetPosition );
  527. } );
  528. expect( live.isEqual( clone ) ).to.be.true;
  529. expect( spy.calledOnce ).to.be.true;
  530. } );
  531. it( 'from the beginning of range', () => {
  532. model.change( writer => {
  533. const sourcePosition = new Position( root, [ 0, 1, 4 ] );
  534. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 2 );
  535. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  536. writer.move( sourceRange, targetPosition );
  537. } );
  538. expect( live.isEqual( clone ) ).to.be.true;
  539. expect( spy.calledOnce ).to.be.true;
  540. } );
  541. it( 'from the range to the range', () => {
  542. live.end.path = [ 0, 1, 8 ];
  543. model.change( writer => {
  544. const sourcePosition = new Position( root, [ 0, 1, 5 ] );
  545. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  546. const targetPosition = new Position( root, [ 0, 1, 7 ] );
  547. writer.move( sourceRange, targetPosition );
  548. } );
  549. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  550. expect( live.end.path ).to.deep.equal( [ 0, 1, 8 ] );
  551. expect( spy.calledOnce ).to.be.true;
  552. } );
  553. } );
  554. } );
  555. describe( 'should not get transformed and not fire change event if', () => {
  556. let otherRoot, spy, live, clone;
  557. beforeEach( () => {
  558. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  559. live = new LiveRange( new Position( root, [ 0, 1, 4 ] ), new Position( root, [ 0, 2, 2 ] ) );
  560. clone = Range.createFromRange( live );
  561. spy = sinon.spy();
  562. live.on( 'change', spy );
  563. } );
  564. afterEach( () => {
  565. live.detach();
  566. } );
  567. describe( 'insertion', () => {
  568. it( 'is in the same parent as range end and after it', () => {
  569. model.change( writer => {
  570. writer.insertText( 'foo', new Position( root, [ 0, 2, 7 ] ) );
  571. } );
  572. expect( live.isEqual( clone ) ).to.be.true;
  573. expect( spy.called ).to.be.false;
  574. } );
  575. it( 'is to a position after a node from range end path', () => {
  576. model.change( writer => {
  577. writer.insert( new Element( 'li' ), new Position( root, [ 3 ] ) );
  578. } );
  579. expect( live.isEqual( clone ) ).to.be.true;
  580. expect( spy.called ).to.be.false;
  581. } );
  582. it( 'is in different root', () => {
  583. model.change( writer => {
  584. writer.insert( new Element( 'li' ), new Position( otherRoot, [ 0 ] ) );
  585. } );
  586. expect( live.isEqual( clone ) ).to.be.true;
  587. expect( spy.called ).to.be.false;
  588. } );
  589. } );
  590. describe( 'range move', () => {
  591. it( 'is to the same parent as range end and after it', () => {
  592. model.change( writer => {
  593. const sourcePosition = new Position( root, [ 0, 4, 0 ] );
  594. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 3 );
  595. const targetPosition = new Position( root, [ 0, 2, 4 ] );
  596. writer.move( sourceRange, targetPosition );
  597. } );
  598. expect( live.isEqual( clone ) ).to.be.true;
  599. expect( spy.called ).to.be.false;
  600. } );
  601. it( 'is to a position after a node from range end path', () => {
  602. model.change( writer => {
  603. const sourcePosition = new Position( root, [ 0, 5 ] );
  604. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  605. const targetPosition = new Position( root, [ 0, 4 ] );
  606. writer.move( sourceRange, targetPosition );
  607. } );
  608. expect( live.isEqual( clone ) ).to.be.true;
  609. expect( spy.called ).to.be.false;
  610. } );
  611. it( 'is from the same parent as range end and after it', () => {
  612. model.change( writer => {
  613. const sourcePosition = new Position( root, [ 0, 2, 4 ] );
  614. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 3 );
  615. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  616. writer.move( sourceRange, targetPosition );
  617. } );
  618. expect( live.isEqual( clone ) ).to.be.true;
  619. expect( spy.called ).to.be.false;
  620. } );
  621. it( 'is from a position after a node from range end path', () => {
  622. model.change( writer => {
  623. const sourcePosition = new Position( root, [ 0, 4 ] );
  624. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  625. const targetPosition = new Position( root, [ 0, 5 ] );
  626. writer.move( sourceRange, targetPosition );
  627. } );
  628. expect( live.isEqual( clone ) ).to.be.true;
  629. expect( spy.called ).to.be.false;
  630. } );
  631. it( 'is to different root', () => {
  632. model.change( writer => {
  633. const sourcePosition = new Position( root, [ 0, 4 ] );
  634. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  635. const targetPosition = new Position( otherRoot, [ 0 ] );
  636. writer.move( sourceRange, targetPosition );
  637. } );
  638. expect( live.isEqual( clone ) ).to.be.true;
  639. expect( spy.called ).to.be.false;
  640. } );
  641. it( 'is from different root', () => {
  642. model.change( writer => {
  643. writer.insertText( 'foo', new Position( otherRoot, [ 0 ] ) );
  644. const sourcePosition = new Position( otherRoot, [ 0 ] );
  645. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  646. const targetPosition = new Position( root, [ 0, 4 ] );
  647. writer.move( sourceRange, targetPosition );
  648. } );
  649. expect( live.isEqual( clone ) ).to.be.true;
  650. expect( spy.called ).to.be.false;
  651. } );
  652. } );
  653. } );
  654. } );