8
0

moveoperation.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, operation */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import MoveOperation from '/ckeditor5/core/treemodel/operation/moveoperation.js';
  9. import Position from '/ckeditor5/core/treemodel/position.js';
  10. import Element from '/ckeditor5/core/treemodel/element.js';
  11. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  12. describe( 'MoveOperation', () => {
  13. let doc, root;
  14. beforeEach( () => {
  15. doc = new Document();
  16. root = doc.createRoot( 'root' );
  17. } );
  18. it( 'should have proper type', () => {
  19. const op = new MoveOperation(
  20. new Position( root, [ 0, 0 ] ),
  21. 1,
  22. new Position( root, [ 1, 0 ] ),
  23. doc.version
  24. );
  25. expect( op.type ).to.equal( 'move' );
  26. } );
  27. it( 'should be sticky', () => {
  28. const op = new MoveOperation(
  29. new Position( root, [ 0, 0 ] ),
  30. 1,
  31. new Position( root, [ 1, 0 ] ),
  32. doc.version
  33. );
  34. expect( op.isSticky ).to.be.true;
  35. } );
  36. it( 'should move from one node to another', () => {
  37. let p1 = new Element( 'p1', [], new Element( 'x' ) );
  38. let p2 = new Element( 'p2' );
  39. root.insertChildren( 0, [ p1, p2 ] );
  40. doc.applyOperation(
  41. new MoveOperation(
  42. new Position( root, [ 0, 0 ] ),
  43. 1,
  44. new Position( root, [ 1, 0 ] ),
  45. doc.version
  46. )
  47. );
  48. expect( doc.version ).to.equal( 1 );
  49. expect( root.getChildCount() ).to.equal( 2 );
  50. expect( root.getChild( 0 ).name ).to.equal( 'p1' );
  51. expect( root.getChild( 1 ).name ).to.equal( 'p2' );
  52. expect( p1.getChildCount() ).to.equal( 0 );
  53. expect( p2.getChildCount() ).to.equal( 1 );
  54. expect( p2.getChild( 0 ).name ).to.equal( 'x' );
  55. } );
  56. it( 'should move position of children in one node backward', () => {
  57. root.insertChildren( 0, 'xbarx' );
  58. doc.applyOperation(
  59. new MoveOperation(
  60. new Position( root, [ 2 ] ),
  61. 2,
  62. new Position( root, [ 1 ] ),
  63. doc.version
  64. )
  65. );
  66. expect( doc.version ).to.equal( 1 );
  67. expect( root.getChildCount() ).to.equal( 5 );
  68. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  69. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  70. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  71. expect( root.getChild( 3 ).character ).to.equal( 'b' );
  72. expect( root.getChild( 4 ).character ).to.equal( 'x' );
  73. } );
  74. it( 'should move position of children in one node forward', () => {
  75. root.insertChildren( 0, 'xbarx' );
  76. doc.applyOperation(
  77. new MoveOperation(
  78. new Position( root, [ 1 ] ),
  79. 2,
  80. new Position( root, [ 4 ] ),
  81. doc.version
  82. )
  83. );
  84. expect( doc.version ).to.equal( 1 );
  85. expect( root.getChildCount() ).to.equal( 5 );
  86. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  87. expect( root.getChild( 1 ).character ).to.equal( 'r' );
  88. expect( root.getChild( 2 ).character ).to.equal( 'b' );
  89. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  90. expect( root.getChild( 4 ).character ).to.equal( 'x' );
  91. } );
  92. it( 'should create a proper MoveOperation as a reverse', () => {
  93. let sourcePosition = new Position( root, [ 0 ] );
  94. let targetPosition = new Position( root, [ 4 ] );
  95. let operation = new MoveOperation( sourcePosition, 3, targetPosition, doc.version );
  96. let reverse = operation.getReversed();
  97. expect( reverse ).to.be.an.instanceof( MoveOperation );
  98. expect( reverse.baseVersion ).to.equal( 1 );
  99. expect( reverse.howMany ).to.equal( 3 );
  100. expect( reverse.sourcePosition.path ).is.deep.equal( [ 1 ] );
  101. expect( reverse.targetPosition.path ).is.deep.equal( [ 0 ] );
  102. operation = new MoveOperation( targetPosition, 3, sourcePosition, doc.version );
  103. reverse = operation.getReversed();
  104. expect( reverse.sourcePosition.path ).is.deep.equal( [ 0 ] );
  105. expect( reverse.targetPosition.path ).is.deep.equal( [ 7 ] );
  106. } );
  107. it( 'should undo move node by applying reverse operation', () => {
  108. let p1 = new Element( 'p1', [], new Element( 'x' ) );
  109. let p2 = new Element( 'p2' );
  110. root.insertChildren( 0, [ p1, p2 ] );
  111. let operation = new MoveOperation(
  112. new Position( root, [ 0, 0 ] ),
  113. 1,
  114. new Position( root, [ 1, 0 ] ),
  115. doc.version
  116. );
  117. doc.applyOperation( operation );
  118. expect( doc.version ).to.equal( 1 );
  119. expect( root.getChildCount() ).to.equal( 2 );
  120. expect( p1.getChildCount() ).to.equal( 0 );
  121. expect( p2.getChildCount() ).to.equal( 1 );
  122. expect( p2.getChild( 0 ).name ).to.equal( 'x' );
  123. doc.applyOperation( operation.getReversed() );
  124. expect( doc.version ).to.equal( 2 );
  125. expect( root.getChildCount() ).to.equal( 2 );
  126. expect( p1.getChildCount() ).to.equal( 1 );
  127. expect( p1.getChild( 0 ).name ).to.equal( 'x' );
  128. expect( p2.getChildCount() ).to.equal( 0 );
  129. } );
  130. it( 'should throw an error if number of nodes to move exceeds the number of existing nodes in given element', () => {
  131. root.insertChildren( 0, 'xbarx' );
  132. let operation = new MoveOperation(
  133. new Position( root, [ 3 ] ),
  134. 3,
  135. new Position( root, [ 1 ] ),
  136. doc.version
  137. );
  138. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-nodes-do-not-exist/ );
  139. } );
  140. it( 'should throw an error if target or source parent-element specified by position does not exist', () => {
  141. let p = new Element( 'p' );
  142. p.insertChildren( 0, 'foo' );
  143. root.insertChildren( 0, [ 'ab', p ] );
  144. let operation = new MoveOperation(
  145. new Position( root, [ 2, 0 ] ),
  146. 3,
  147. new Position( root, [ 1 ] ),
  148. doc.version
  149. );
  150. root.removeChildren( 2, 1 );
  151. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-position-invalid/ );
  152. } );
  153. it( 'should throw an error if operation tries to move a range between the beginning and the end of that range', () => {
  154. root.insertChildren( 0, 'xbarx' );
  155. let operation = new MoveOperation(
  156. new Position( root, [ 1 ] ),
  157. 3,
  158. new Position( root, [ 2 ] ),
  159. doc.version
  160. );
  161. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-range-into-itself/ );
  162. } );
  163. it( 'should throw an error if operation tries to move a range into a sub-tree of a node that is in that range', () => {
  164. let p = new Element( 'p', [], [ new Element( 'p' ) ] );
  165. root.insertChildren( 0, [ 'ab', p, 'xy' ] );
  166. let operation = new MoveOperation(
  167. new Position( root, [ 1 ] ),
  168. 3,
  169. new Position( root, [ 2, 0, 0 ] ),
  170. doc.version
  171. );
  172. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-node-into-itself/ );
  173. } );
  174. it( 'should not throw an error if operation move a range into a sibling', () => {
  175. let p = new Element( 'p' );
  176. root.insertChildren( 0, [ 'ab', p, 'xy' ] );
  177. let operation = new MoveOperation(
  178. new Position( root, [ 1 ] ),
  179. 1,
  180. new Position( root, [ 2, 0 ] ),
  181. doc.version
  182. );
  183. expect(
  184. () => {
  185. doc.applyOperation( operation );
  186. }
  187. ).not.to.throw();
  188. expect( root.getChildCount() ).to.equal( 4 );
  189. expect( p.getChildCount() ).to.equal( 1 );
  190. expect( p.getChild( 0 ).character ).to.equal( 'b' );
  191. } );
  192. it( 'should create MoveOperation with the same parameters when cloned', () => {
  193. let sourcePosition = new Position( root, [ 0 ] );
  194. let targetPosition = new Position( root, [ 1 ] );
  195. let howMany = 4;
  196. let baseVersion = doc.version;
  197. let op = new MoveOperation( sourcePosition, howMany, targetPosition, baseVersion );
  198. let clone = op.clone();
  199. // New instance rather than a pointer to the old instance.
  200. expect( clone ).not.to.be.equal( op );
  201. expect( clone ).to.be.instanceof( MoveOperation );
  202. expect( clone.sourcePosition.isEqual( sourcePosition ) ).to.be.true;
  203. expect( clone.targetPosition.isEqual( targetPosition ) ).to.be.true;
  204. expect( clone.howMany ).to.equal( howMany );
  205. expect( clone.baseVersion ).to.equal( baseVersion );
  206. } );
  207. } );