8
0

moveoperation.js 9.0 KB

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