moveoperation.js 9.3 KB

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