liverange.js 29 KB

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