8
0

liverange.js 27 KB

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