moveoperation.js 11 KB

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