8
0

liverange.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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( 'createFromRange should return LiveRange', () => {
  64. const range = LiveRange._createFromRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ) );
  65. expect( range ).to.be.instanceof( LiveRange );
  66. range.detach();
  67. } );
  68. it( 'should fire change:range event with when its boundaries are changed', () => {
  69. const live = new LiveRange( new Position( root, [ 0, 1, 4 ] ), new Position( root, [ 0, 2, 2 ] ) );
  70. const copy = Range._createFromRange( live );
  71. const spy = sinon.spy();
  72. live.on( 'change:range', spy );
  73. const sourcePosition = new Position( root, [ 2 ] );
  74. const targetPosition = new Position( root, [ 0 ] );
  75. model.change( writer => {
  76. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  77. writer.move( sourceRange, targetPosition );
  78. } );
  79. expect( spy.calledOnce ).to.be.true;
  80. // First parameter available in event should be a range that is equal to the live range before the live range changed.
  81. expect( spy.args[ 0 ][ 1 ].isEqual( copy ) ).to.be.true;
  82. // Second parameter is null for operations that did not move the range into graveyard.
  83. expect( spy.args[ 0 ][ 2 ].deletionPosition ).to.be.null;
  84. } );
  85. it( 'should fire change:content event when content inside the range has changed', () => {
  86. const live = new LiveRange( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 3 ] ) );
  87. const spy = sinon.spy();
  88. live.on( 'change:content', spy );
  89. const sourcePosition = new Position( root, [ 0, 2, 0 ] );
  90. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  91. model.change( writer => {
  92. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 2 );
  93. writer.move( sourceRange, targetPosition );
  94. } );
  95. expect( spy.calledOnce ).to.be.true;
  96. // First parameter available in event should be a range that is equal to the live range before the live range changed.
  97. expect( spy.args[ 0 ][ 1 ].isEqual( live ) ).to.be.true;
  98. // Second parameter is null for operations that did not move the range into graveyard.
  99. expect( spy.args[ 0 ][ 2 ].deletionPosition ).to.be.null;
  100. } );
  101. it( 'should pass deletion position if range was removed (remove)', () => {
  102. const live = new LiveRange( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 4 ] ) );
  103. const spy = sinon.spy();
  104. live.on( 'change:range', spy );
  105. const sourcePosition = new Position( root, [ 0, 0 ] );
  106. model.change( writer => {
  107. writer.remove( Range.createFromPositionAndShift( sourcePosition, 6 ) );
  108. } );
  109. // Second parameter is deletion position.
  110. expect( spy.args[ 0 ][ 2 ].deletionPosition.isEqual( sourcePosition ) ).to.be.true;
  111. } );
  112. // This scenario is hypothetically possible during OT if the element to merge-into was removed.
  113. // In that case a live range inside the merged element will be merged into an element which is in graveyard.
  114. // Because it may happen only in OT, in the test below we will generate operations by hand.
  115. it( 'should pass deletion position if range was removed (merge)', () => {
  116. const live = new LiveRange( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 1 ] ) );
  117. const spy = sinon.spy();
  118. live.on( 'change:range', spy );
  119. model.change( writer => {
  120. const batch = writer.batch;
  121. const gy = model.document.graveyard;
  122. const remove = new MoveOperation(
  123. new Position( root, [ 0 ] ),
  124. 1,
  125. new Position( gy, [ 0 ] ),
  126. model.document.version
  127. );
  128. const merge = new MergeOperation(
  129. new Position( root, [ 0, 0 ] ),
  130. 10,
  131. new Position( gy, [ 0, 0 ] ),
  132. new Position( gy, [ 0 ] ),
  133. model.document.version + 1
  134. );
  135. batch.addOperation( remove );
  136. model.applyOperation( remove );
  137. batch.addOperation( merge );
  138. model.applyOperation( merge );
  139. } );
  140. // Second parameter is deletion position.
  141. expect( spy.args[ 1 ][ 2 ].deletionPosition.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  142. } );
  143. describe( 'should get transformed and fire change:range if', () => {
  144. let live, spy;
  145. beforeEach( () => {
  146. live = new LiveRange( new Position( root, [ 0, 1, 4 ] ), new Position( root, [ 0, 2, 2 ] ) );
  147. spy = sinon.spy();
  148. live.on( 'change:range', spy );
  149. } );
  150. afterEach( () => {
  151. live.detach();
  152. } );
  153. describe( 'insertion', () => {
  154. it( 'is in the same parent as range start and before it', () => {
  155. model.change( writer => {
  156. writer.insertText( 'xxx', new Position( root, [ 0, 1, 0 ] ) );
  157. } );
  158. expect( live.start.path ).to.deep.equal( [ 0, 1, 7 ] );
  159. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  160. expect( spy.calledOnce ).to.be.true;
  161. } );
  162. it( 'is in the same parent as range end and before it', () => {
  163. model.change( writer => {
  164. writer.insertText( 'xxx', new Position( root, [ 0, 2, 0 ] ) );
  165. } );
  166. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  167. expect( live.end.path ).to.deep.equal( [ 0, 2, 5 ] );
  168. expect( spy.calledOnce ).to.be.true;
  169. } );
  170. it( 'is at a position before a node from range start path', () => {
  171. model.change( writer => {
  172. writer.insert( new Element( 'li' ), new Position( root, [ 0, 0 ] ) );
  173. } );
  174. expect( live.start.path ).to.deep.equal( [ 0, 2, 4 ] );
  175. expect( live.end.path ).to.deep.equal( [ 0, 3, 2 ] );
  176. expect( spy.calledOnce ).to.be.true;
  177. } );
  178. it( 'is at a position before a node from range end path', () => {
  179. model.change( writer => {
  180. writer.insert( new Element( 'li' ), new Position( root, [ 0, 2 ] ) );
  181. } );
  182. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  183. expect( live.end.path ).to.deep.equal( [ 0, 3, 2 ] );
  184. expect( spy.calledOnce ).to.be.true;
  185. } );
  186. it( 'is at the live range start position and live range is collapsed', () => {
  187. live.end.path = [ 0, 1, 4 ];
  188. model.change( writer => {
  189. writer.insertText( 'xxx', new Position( root, [ 0, 1, 4 ] ) );
  190. } );
  191. expect( live.start.path ).to.deep.equal( [ 0, 1, 7 ] );
  192. expect( live.end.path ).to.deep.equal( [ 0, 1, 7 ] );
  193. expect( spy.calledOnce ).to.be.true;
  194. } );
  195. } );
  196. describe( 'range move', () => {
  197. it( 'is to the same parent as range start and before it', () => {
  198. model.change( writer => {
  199. const sourcePosition = new Position( root, [ 0, 4, 0 ] );
  200. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 4 );
  201. const targetPosition = new Position( root, [ 0, 1, 0 ] );
  202. writer.move( sourceRange, targetPosition );
  203. } );
  204. expect( live.start.path ).to.deep.equal( [ 0, 1, 8 ] );
  205. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  206. expect( spy.calledOnce ).to.be.true;
  207. } );
  208. it( 'is to the same parent as range end and before it', () => {
  209. model.change( writer => {
  210. const sourcePosition = new Position( root, [ 0, 4, 0 ] );
  211. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 4 );
  212. const targetPosition = new Position( root, [ 0, 2, 0 ] );
  213. writer.move( sourceRange, targetPosition );
  214. } );
  215. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  216. expect( live.end.path ).to.deep.equal( [ 0, 2, 6 ] );
  217. expect( spy.calledOnce ).to.be.true;
  218. } );
  219. it( 'is to a position before a node from range start path', () => {
  220. model.change( writer => {
  221. const sourcePosition = new Position( root, [ 0, 4 ] );
  222. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 2 );
  223. const targetPosition = new Position( root, [ 0, 0 ] );
  224. writer.move( sourceRange, targetPosition );
  225. } );
  226. expect( live.start.path ).to.deep.equal( [ 0, 3, 4 ] );
  227. expect( live.end.path ).to.deep.equal( [ 0, 4, 2 ] );
  228. expect( spy.calledOnce ).to.be.true;
  229. } );
  230. it( 'is to a position before a node from range end path', () => {
  231. model.change( writer => {
  232. const sourcePosition = new Position( root, [ 0, 4 ] );
  233. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  234. const targetPosition = new Position( root, [ 0, 2 ] );
  235. writer.move( sourceRange, targetPosition );
  236. } );
  237. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  238. expect( live.end.path ).to.deep.equal( [ 0, 3, 2 ] );
  239. expect( spy.calledOnce ).to.be.true;
  240. } );
  241. it( 'is from the same parent as range start and before it', () => {
  242. model.change( writer => {
  243. const sourcePosition = new Position( root, [ 0, 1, 0 ] );
  244. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 3 );
  245. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  246. writer.move( sourceRange, targetPosition );
  247. } );
  248. expect( live.start.path ).to.deep.equal( [ 0, 1, 1 ] );
  249. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  250. expect( spy.calledOnce ).to.be.true;
  251. } );
  252. it( 'is from the same parent as range end and before it - #1', () => {
  253. model.change( writer => {
  254. const sourcePosition = new Position( root, [ 0, 2, 0 ] );
  255. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  256. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  257. writer.move( sourceRange, targetPosition );
  258. } );
  259. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  260. expect( live.end.path ).to.deep.equal( [ 0, 2, 1 ] );
  261. expect( spy.calledOnce ).to.be.true;
  262. } );
  263. it( 'is from the same parent as range end and before it - #2', () => {
  264. model.change( writer => {
  265. const sourcePosition = new Position( root, [ 0, 2, 0 ] );
  266. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 2 );
  267. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  268. writer.move( sourceRange, targetPosition );
  269. } );
  270. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  271. expect( live.end.path ).to.deep.equal( [ 0, 2, 0 ] );
  272. expect( spy.calledOnce ).to.be.true;
  273. } );
  274. it( 'is from a position before a node from range start path', () => {
  275. model.change( writer => {
  276. const sourcePosition = new Position( root, [ 0, 0 ] );
  277. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 1 );
  278. const targetPosition = new Position( root, [ 0, 4 ] );
  279. writer.move( sourceRange, targetPosition );
  280. } );
  281. expect( live.start.path ).to.deep.equal( [ 0, 0, 4 ] );
  282. expect( live.end.path ).to.deep.equal( [ 0, 1, 2 ] );
  283. expect( spy.calledOnce ).to.be.true;
  284. } );
  285. it( 'intersects on live range left side', () => {
  286. model.change( writer => {
  287. const sourcePosition = new Position( root, [ 0, 1, 2 ] );
  288. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 4 );
  289. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  290. writer.move( sourceRange, targetPosition );
  291. } );
  292. expect( live.start.path ).to.deep.equal( [ 0, 1, 2 ] );
  293. expect( live.end.path ).to.deep.equal( [ 0, 2, 2 ] );
  294. expect( spy.calledOnce ).to.be.true;
  295. } );
  296. it( 'intersects on live range right side', () => {
  297. model.change( writer => {
  298. const sourcePosition = new Position( root, [ 0, 2, 1 ] );
  299. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 4 );
  300. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  301. writer.move( sourceRange, targetPosition );
  302. } );
  303. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  304. expect( live.end.path ).to.deep.equal( [ 0, 2, 1 ] );
  305. expect( spy.calledOnce ).to.be.true;
  306. } );
  307. it( 'is equal to live range', () => {
  308. live.end.path = [ 0, 1, 7 ];
  309. model.change( writer => {
  310. const sourcePosition = new Position( root, [ 0, 1, 4 ] );
  311. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 3 );
  312. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  313. writer.move( sourceRange, targetPosition );
  314. } );
  315. expect( live.start.path ).to.deep.equal( [ 0, 4, 0 ] );
  316. expect( live.end.path ).to.deep.equal( [ 0, 4, 3 ] );
  317. expect( spy.calledOnce ).to.be.true;
  318. } );
  319. it( 'contains live range', () => {
  320. live.end.path = [ 0, 1, 6 ];
  321. model.change( writer => {
  322. const sourcePosition = new Position( root, [ 0, 1, 3 ] );
  323. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 5 );
  324. const targetPosition = new Position( root, [ 0, 4, 0 ] );
  325. writer.move( sourceRange, targetPosition );
  326. } );
  327. expect( live.start.path ).to.deep.equal( [ 0, 4, 1 ] );
  328. expect( live.end.path ).to.deep.equal( [ 0, 4, 3 ] );
  329. expect( spy.calledOnce ).to.be.true;
  330. } );
  331. it( 'is intersecting with live range on left and points to live range', () => {
  332. live.end.path = [ 0, 1, 7 ];
  333. model.change( writer => {
  334. const sourcePosition = new Position( root, [ 0, 1, 2 ] );
  335. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 3 );
  336. const targetPosition = new Position( root, [ 0, 1, 8 ] );
  337. writer.move( sourceRange, targetPosition );
  338. } );
  339. expect( live.start.path ).to.deep.equal( [ 0, 1, 2 ] );
  340. expect( live.end.path ).to.deep.equal( [ 0, 1, 4 ] );
  341. expect( spy.calledOnce ).to.be.true;
  342. } );
  343. it( 'is intersecting with live range on right and is moved into live range', () => {
  344. model.change( writer => {
  345. const sourcePosition = new Position( root, [ 0, 2, 1 ] );
  346. const sourceRange = Range.createFromPositionAndShift( sourcePosition, 5 );
  347. const targetPosition = new Position( root, [ 0, 2, 0 ] );
  348. writer.move( sourceRange, targetPosition );
  349. } );
  350. expect( live.start.path ).to.deep.equal( [ 0, 1, 4 ] );
  351. expect( live.end.path ).to.deep.equal( [ 0, 2, 1 ] );
  352. expect( spy.calledOnce ).to.be.true;
  353. } );
  354. } );
  355. describe( 'wrap', () => {
  356. // NOTE: it overrides the variable defined globally in these tests.
  357. // These tests need to be rewritten to use the batch API anyway and then this variable can be removed.
  358. let live;
  359. beforeEach( () => {
  360. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  361. model.schema.register( 'w' );
  362. model.schema.extend( 'p', { allowIn: 'w' } );
  363. model.schema.extend( 'w', { allowIn: '$root' } );
  364. } );
  365. afterEach( () => {
  366. live.detach();
  367. } );
  368. it( 'is inside the wrapped range', () => {
  369. setData( model, '<p>x</p><p>[a]</p><p>x</p>' );
  370. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  371. model.change( writer => {
  372. // [<p>a</p>]
  373. writer.wrap( new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) ), 'w' );
  374. } );
  375. expect( stringify( root, live ) ).to.equal( '<p>x</p><w><p>[a]</p></w><p>x</p>' );
  376. } );
  377. it( 'its start is intersecting with the wrapped range', () => {
  378. setData( model, '<p>a[b</p><p>x</p><p>c]d</p>' );
  379. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  380. model.change( writer => {
  381. // [<p>ab</p>]
  382. writer.wrap( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ), 'w' );
  383. } );
  384. // Should be '<w><p>a[b</p></w><p>x</p><p>c]d</p>' but the range is trimmed.
  385. expect( stringify( root, live ) ).to.equal( '<w><p>ab</p></w>[<p>x</p><p>c]d</p>' );
  386. } );
  387. it( 'its end is intersecting with the wrapped range', () => {
  388. setData( model, '<p>a[b</p><p>x</p><p>c]d</p>' );
  389. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  390. model.change( writer => {
  391. // [<p>cd</p>]
  392. writer.wrap( new Range( new Position( root, [ 2 ] ), new Position( root, [ 3 ] ) ), 'w' );
  393. } );
  394. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><w><p>c]d</p></w>' );
  395. } );
  396. it( 'its start is intersecting with the wrapped range (multilpe elements)', () => {
  397. setData( model, '<p>a[b</p><p>x</p><p>c]d</p>' );
  398. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  399. model.change( writer => {
  400. // [<p>ab</p><p>x</p>]
  401. writer.wrap( new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ), 'w' );
  402. } );
  403. // Should be '<w><p>a[b</p><p>x</p></w><p>c]d</p>' but the range is trimmed.
  404. expect( stringify( root, live ) ).to.equal( '<w><p>ab</p><p>x</p></w>[<p>c]d</p>' );
  405. } );
  406. it( 'its end is intersecting with the wrapped range (multiple elements)', () => {
  407. setData( model, '<p>a[b</p><p>x</p><p>c]d</p>' );
  408. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  409. model.change( writer => {
  410. // [<p>x</p><p>cd</p>]
  411. writer.wrap( new Range( new Position( root, [ 1 ] ), new Position( root, [ 3 ] ) ), 'w' );
  412. } );
  413. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><w><p>x</p><p>c]d</p></w>' );
  414. } );
  415. it( 'contains element to wrap', () => {
  416. setData( model, '<p>a[b</p><p>x</p><p>c]d</p>' );
  417. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  418. model.change( writer => {
  419. // [<p>x</p>]
  420. writer.wrap( new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) ), 'w' );
  421. } );
  422. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><w><p>x</p></w><p>c]d</p>' );
  423. } );
  424. } );
  425. describe( 'unwrap', () => {
  426. // NOTE: it overrides the variable defined globally in these tests.
  427. // These tests need to be rewritten to use the batch API anyway and then this variable can be removed.
  428. let live;
  429. beforeEach( () => {
  430. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  431. model.schema.register( 'w' );
  432. model.schema.extend( 'p', { allowIn: 'w' } );
  433. model.schema.extend( 'w', { allowIn: '$root' } );
  434. } );
  435. afterEach( () => {
  436. live.detach();
  437. } );
  438. it( 'is inside the wrapper to remove', () => {
  439. setData( model, '<p>x</p><w><p>[a]</p></w><p>x</p>' );
  440. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  441. model.change( writer => {
  442. writer.unwrap( root.getChild( 1 ) );
  443. } );
  444. expect( stringify( root, live ) ).to.equal( '<p>x</p><p>[a]</p><p>x</p>' );
  445. } );
  446. it( 'its start is intersecting with the wrapper to remove', () => {
  447. setData( model, '<w><p>a[b</p></w><p>c]d</p>' );
  448. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  449. model.change( writer => {
  450. writer.unwrap( root.getChild( 0 ) );
  451. } );
  452. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>c]d</p>' );
  453. } );
  454. it( 'its end is intersecting with the wrapper to remove', () => {
  455. setData( model, '<p>a[b</p><w><p>c]d</p></w>' );
  456. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  457. model.change( writer => {
  458. writer.unwrap( root.getChild( 1 ) );
  459. } );
  460. // Should be '<p>a[b</p><p>c]d</p>' but the range is trimmed.
  461. expect( stringify( root, live ) ).to.equal( '<p>a[b</p>]<p>cd</p>' );
  462. } );
  463. it( 'its start is intersecting with the wrapper to remove (multiple elements)', () => {
  464. setData( model, '<w><p>a[b</p><p>x</p></w><p>c]d</p>' );
  465. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  466. model.change( writer => {
  467. writer.unwrap( root.getChild( 0 ) );
  468. } );
  469. expect( stringify( root, live ) ).to.equal( '<p>a[b</p><p>x</p><p>c]d</p>' );
  470. } );
  471. it( 'its end is intersecting with the wrapper to remove (multiple elements)', () => {
  472. setData( model, '<p>a[b</p><w><p>x</p><p>c]d</p></w>' );
  473. live = new LiveRange( doc.selection.getFirstPosition(), doc.selection.getLastPosition() );
  474. model.change( writer => {
  475. writer.unwrap( root.getChild( 1 ) );
  476. } );
  477. // Should be '<p>a[b</p><p>x</p><p>c]d</p>' but the range is trimmed.
  478. expect( stringify( root, live ) ).to.equal( '<p>a[b</p>]<p>x</p><p>cd</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. } );