liverange.js 28 KB

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