liverange.js 28 KB

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