8
0

moveoperation.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../../src/model/model';
  6. import MoveOperation from '../../../src/model/operation/moveoperation';
  7. import Position from '../../../src/model/position';
  8. import Element from '../../../src/model/element';
  9. import Text from '../../../src/model/text';
  10. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'MoveOperation', () => {
  12. let model, doc, root, gy;
  13. beforeEach( () => {
  14. model = new Model();
  15. doc = model.document;
  16. root = doc.createRoot();
  17. gy = doc.graveyard;
  18. } );
  19. it( 'should have proper type', () => {
  20. const move = new MoveOperation( new Position( root, [ 0, 0 ] ), 1, new Position( root, [ 1, 0 ] ), 0 );
  21. expect( move.type ).to.equal( 'move' );
  22. const remove1 = new MoveOperation( new Position( root, [ 0, 0 ] ), 1, new Position( gy, [ 0 ] ), 0 );
  23. expect( remove1.type ).to.equal( 'remove' );
  24. const remove2 = new MoveOperation( new Position( gy, [ 0 ] ), 1, new Position( gy, [ 1 ] ), 0 );
  25. expect( remove2.type ).to.equal( 'remove' );
  26. const reinsert = new MoveOperation( new Position( gy, [ 0 ] ), 1, new Position( root, [ 0, 0 ] ), 0 );
  27. expect( reinsert.type ).to.equal( 'reinsert' );
  28. } );
  29. it( 'should move from one node to another', () => {
  30. const p1 = new Element( 'p1', [], new Element( 'x' ) );
  31. const p2 = new Element( 'p2' );
  32. root._insertChild( 0, [ p1, p2 ] );
  33. model.applyOperation(
  34. new MoveOperation(
  35. new Position( root, [ 0, 0 ] ),
  36. 1,
  37. new Position( root, [ 1, 0 ] ),
  38. doc.version
  39. )
  40. );
  41. expect( doc.version ).to.equal( 1 );
  42. expect( root.maxOffset ).to.equal( 2 );
  43. expect( root.getChild( 0 ).name ).to.equal( 'p1' );
  44. expect( root.getChild( 1 ).name ).to.equal( 'p2' );
  45. expect( p1.maxOffset ).to.equal( 0 );
  46. expect( p2.maxOffset ).to.equal( 1 );
  47. expect( p2.getChild( 0 ).name ).to.equal( 'x' );
  48. } );
  49. it( 'should move position of children in one node backward', () => {
  50. root._insertChild( 0, new Text( 'xbarx' ) );
  51. model.applyOperation(
  52. new MoveOperation(
  53. new Position( root, [ 2 ] ),
  54. 2,
  55. new Position( root, [ 1 ] ),
  56. doc.version
  57. )
  58. );
  59. expect( doc.version ).to.equal( 1 );
  60. expect( root.maxOffset ).to.equal( 5 );
  61. expect( root.getChild( 0 ).data ).to.equal( 'xarbx' );
  62. } );
  63. it( 'should move position of children in one node forward', () => {
  64. root._insertChild( 0, new Text( 'xbarx' ) );
  65. model.applyOperation(
  66. new MoveOperation(
  67. new Position( root, [ 1 ] ),
  68. 2,
  69. new Position( root, [ 4 ] ),
  70. doc.version
  71. )
  72. );
  73. expect( doc.version ).to.equal( 1 );
  74. expect( root.maxOffset ).to.equal( 5 );
  75. expect( root.getChild( 0 ).data ).to.equal( 'xrbax' );
  76. } );
  77. it( 'should create a proper MoveOperation as a reverse', () => {
  78. const sourcePosition = new Position( root, [ 0 ] );
  79. const targetPosition = new Position( root, [ 4 ] );
  80. let operation = new MoveOperation( sourcePosition, 3, targetPosition, doc.version );
  81. let reverse = operation.getReversed();
  82. expect( reverse ).to.be.an.instanceof( MoveOperation );
  83. expect( reverse.baseVersion ).to.equal( 1 );
  84. expect( reverse.howMany ).to.equal( 3 );
  85. expect( reverse.sourcePosition.path ).is.deep.equal( [ 1 ] );
  86. expect( reverse.targetPosition.path ).is.deep.equal( [ 0 ] );
  87. operation = new MoveOperation( targetPosition, 3, sourcePosition, doc.version );
  88. reverse = operation.getReversed();
  89. expect( reverse.sourcePosition.path ).is.deep.equal( [ 0 ] );
  90. expect( reverse.targetPosition.path ).is.deep.equal( [ 7 ] );
  91. } );
  92. it( 'should undo move node by applying reverse operation', () => {
  93. const p1 = new Element( 'p1', [], new Element( 'x' ) );
  94. const p2 = new Element( 'p2' );
  95. root._insertChild( 0, [ p1, p2 ] );
  96. const operation = new MoveOperation(
  97. new Position( root, [ 0, 0 ] ),
  98. 1,
  99. new Position( root, [ 1, 0 ] ),
  100. doc.version
  101. );
  102. model.applyOperation( operation );
  103. expect( doc.version ).to.equal( 1 );
  104. expect( root.maxOffset ).to.equal( 2 );
  105. expect( p1.maxOffset ).to.equal( 0 );
  106. expect( p2.maxOffset ).to.equal( 1 );
  107. expect( p2.getChild( 0 ).name ).to.equal( 'x' );
  108. model.applyOperation( operation.getReversed() );
  109. expect( doc.version ).to.equal( 2 );
  110. expect( root.maxOffset ).to.equal( 2 );
  111. expect( p1.maxOffset ).to.equal( 1 );
  112. expect( p1.getChild( 0 ).name ).to.equal( 'x' );
  113. expect( p2.maxOffset ).to.equal( 0 );
  114. } );
  115. describe( '_validate()', () => {
  116. it( 'should throw an error if number of nodes to move exceeds the number of existing nodes in given element', () => {
  117. root._insertChild( 0, new Text( 'xbarx' ) );
  118. const operation = new MoveOperation(
  119. new Position( root, [ 3 ] ),
  120. 3,
  121. new Position( root, [ 1 ] ),
  122. doc.version
  123. );
  124. expectToThrowCKEditorError( () => operation._validate(), /move-operation-nodes-do-not-exist/, model );
  125. } );
  126. it( 'should throw an error if target or source parent-element specified by position does not exist', () => {
  127. const p = new Element( 'p' );
  128. p._insertChild( 0, new Text( 'foo' ) );
  129. root._insertChild( 0, [ new Text( 'ab' ), p ] );
  130. const operation = new MoveOperation(
  131. new Position( root, [ 2, 0 ] ),
  132. 3,
  133. new Position( root, [ 1 ] ),
  134. doc.version
  135. );
  136. root._removeChildren( 1 );
  137. expectToThrowCKEditorError( () => operation._validate(), /model-position-path-incorrect/, model );
  138. } );
  139. it( 'should throw an error if operation tries to move a range between the beginning and the end of that range', () => {
  140. root._insertChild( 0, new Text( 'xbarx' ) );
  141. const operation = new MoveOperation(
  142. new Position( root, [ 1 ] ),
  143. 3,
  144. new Position( root, [ 2 ] ),
  145. doc.version
  146. );
  147. expectToThrowCKEditorError( () => operation._validate(), /move-operation-range-into-itself/, model );
  148. } );
  149. it( 'should throw an error if operation tries to move a range into a sub-tree of a node that is in that range', () => {
  150. const p = new Element( 'p', [], [ new Element( 'p' ) ] );
  151. root._insertChild( 0, [ new Text( 'ab' ), p, new Text( 'xy' ) ] );
  152. const operation = new MoveOperation(
  153. new Position( root, [ 1 ] ),
  154. 3,
  155. new Position( root, [ 2, 0, 0 ] ),
  156. doc.version
  157. );
  158. expectToThrowCKEditorError( () => operation._validate(), /move-operation-node-into-itself/, model );
  159. } );
  160. it( 'should not throw an error if operation move a range into a sibling', () => {
  161. const p = new Element( 'p' );
  162. root._insertChild( 0, [ new Text( 'ab' ), p, new Text( 'xy' ) ] );
  163. const operation = new MoveOperation(
  164. new Position( root, [ 1 ] ),
  165. 1,
  166. new Position( root, [ 2, 0 ] ),
  167. doc.version
  168. );
  169. expect( () => operation._validate() ).not.to.throw();
  170. } );
  171. it( 'should not throw when operation paths looks like incorrect but move is between different roots', () => {
  172. const p = new Element( 'p' );
  173. root._insertChild( 0, [ new Text( 'a' ), p, new Text( 'b' ) ] );
  174. doc.graveyard._insertChild( 0, new Text( 'abc' ) );
  175. const operation = new MoveOperation(
  176. new Position( doc.graveyard, [ 0 ] ),
  177. 2,
  178. new Position( root, [ 1, 0 ] ),
  179. doc.version
  180. );
  181. expect( () => operation._validate() ).not.to.throw();
  182. } );
  183. } );
  184. it( 'should create MoveOperation with the same parameters when cloned', () => {
  185. const sourcePosition = new Position( root, [ 0 ] );
  186. const targetPosition = new Position( root, [ 1 ] );
  187. const howMany = 4;
  188. const baseVersion = doc.version;
  189. const op = new MoveOperation( sourcePosition, howMany, targetPosition, baseVersion );
  190. const clone = op.clone();
  191. // New instance rather than a pointer to the old instance.
  192. expect( clone ).not.to.equal( op );
  193. expect( clone ).to.be.instanceof( MoveOperation );
  194. expect( clone.sourcePosition.isEqual( sourcePosition ) ).to.be.true;
  195. expect( clone.targetPosition.isEqual( targetPosition ) ).to.be.true;
  196. expect( clone.howMany ).to.equal( howMany );
  197. expect( clone.baseVersion ).to.equal( baseVersion );
  198. } );
  199. describe( 'getMovedRangeStart', () => {
  200. it( 'should return move operation target position transformed by removing move operation source range', () => {
  201. const sourcePosition = new Position( root, [ 0, 2 ] );
  202. const targetPosition = new Position( root, [ 0, 6 ] );
  203. const howMany = 3;
  204. const baseVersion = doc.version;
  205. const op = new MoveOperation( sourcePosition, howMany, targetPosition, baseVersion );
  206. expect( op.getMovedRangeStart().path ).to.deep.equal( [ 0, 3 ] );
  207. } );
  208. } );
  209. describe( 'toJSON', () => {
  210. it( 'should create proper json object', () => {
  211. const sourcePosition = new Position( root, [ 0, 0 ] );
  212. const targetPosition = new Position( root, [ 1, 0 ] );
  213. const op = new MoveOperation( sourcePosition, 1, targetPosition, doc.version );
  214. const serialized = op.toJSON();
  215. expect( serialized ).to.deep.equal( {
  216. __className: 'MoveOperation',
  217. baseVersion: 0,
  218. howMany: 1,
  219. sourcePosition: op.sourcePosition.toJSON(),
  220. targetPosition: op.targetPosition.toJSON()
  221. } );
  222. } );
  223. } );
  224. describe( 'fromJSON', () => {
  225. it( 'should create proper MoveOperation from json object', () => {
  226. const sourcePosition = new Position( root, [ 0, 0 ] );
  227. const targetPosition = new Position( root, [ 1, 0 ] );
  228. const op = new MoveOperation( sourcePosition, 1, targetPosition, doc.version );
  229. const serialized = op.toJSON();
  230. const deserialized = MoveOperation.fromJSON( serialized, doc );
  231. expect( deserialized ).to.deep.equal( op );
  232. } );
  233. } );
  234. } );