8
0

moveoperation.js 9.1 KB

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