moveoperation.js 9.8 KB

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