8
0

liverange.js 29 KB

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