liverange.js 27 KB

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