moveoperation.js 9.5 KB

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