moveoperation.js 9.2 KB

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