8
0

moveoperation.js 11 KB

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