moveoperation.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 move from one node to another', () => {
  28. let p1 = new Element( 'p1', [], new Element( 'x' ) );
  29. let p2 = new Element( 'p2' );
  30. root.insertChildren( 0, [ p1, p2 ] );
  31. doc.applyOperation(
  32. new MoveOperation(
  33. new Position( root, [ 0, 0 ] ),
  34. 1,
  35. new Position( root, [ 1, 0 ] ),
  36. doc.version
  37. )
  38. );
  39. expect( doc.version ).to.equal( 1 );
  40. expect( root.getChildCount() ).to.equal( 2 );
  41. expect( root.getChild( 0 ).name ).to.equal( 'p1' );
  42. expect( root.getChild( 1 ).name ).to.equal( 'p2' );
  43. expect( p1.getChildCount() ).to.equal( 0 );
  44. expect( p2.getChildCount() ).to.equal( 1 );
  45. expect( p2.getChild( 0 ).name ).to.equal( 'x' );
  46. } );
  47. it( 'should move position of children in one node backward', () => {
  48. root.insertChildren( 0, 'xbarx' );
  49. doc.applyOperation(
  50. new MoveOperation(
  51. new Position( root, [ 2 ] ),
  52. 2,
  53. new Position( root, [ 1 ] ),
  54. doc.version
  55. )
  56. );
  57. expect( doc.version ).to.equal( 1 );
  58. expect( root.getChildCount() ).to.equal( 5 );
  59. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  60. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  61. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  62. expect( root.getChild( 3 ).character ).to.equal( 'b' );
  63. expect( root.getChild( 4 ).character ).to.equal( 'x' );
  64. } );
  65. it( 'should move position of children in one node forward', () => {
  66. root.insertChildren( 0, 'xbarx' );
  67. doc.applyOperation(
  68. new MoveOperation(
  69. new Position( root, [ 1 ] ),
  70. 2,
  71. new Position( root, [ 4 ] ),
  72. doc.version
  73. )
  74. );
  75. expect( doc.version ).to.equal( 1 );
  76. expect( root.getChildCount() ).to.equal( 5 );
  77. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  78. expect( root.getChild( 1 ).character ).to.equal( 'r' );
  79. expect( root.getChild( 2 ).character ).to.equal( 'b' );
  80. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  81. expect( root.getChild( 4 ).character ).to.equal( 'x' );
  82. } );
  83. it( 'should create a proper MoveOperation as a reverse', () => {
  84. let sourcePosition = new Position( root, [ 0 ] );
  85. let targetPosition = new Position( root, [ 4 ] );
  86. let operation = new MoveOperation( sourcePosition, 3, targetPosition, doc.version );
  87. let reverse = operation.getReversed();
  88. expect( reverse ).to.be.an.instanceof( MoveOperation );
  89. expect( reverse.baseVersion ).to.equal( 1 );
  90. expect( reverse.howMany ).to.equal( 3 );
  91. expect( reverse.sourcePosition.path ).is.deep.equal( [ 1 ] );
  92. expect( reverse.targetPosition.path ).is.deep.equal( [ 0 ] );
  93. operation = new MoveOperation( targetPosition, 3, sourcePosition, doc.version );
  94. reverse = operation.getReversed();
  95. expect( reverse.sourcePosition.path ).is.deep.equal( [ 0 ] );
  96. expect( reverse.targetPosition.path ).is.deep.equal( [ 7 ] );
  97. } );
  98. it( 'should undo move node by applying reverse operation', () => {
  99. let p1 = new Element( 'p1', [], new Element( 'x' ) );
  100. let p2 = new Element( 'p2' );
  101. root.insertChildren( 0, [ p1, p2 ] );
  102. let operation = new MoveOperation(
  103. new Position( root, [ 0, 0 ] ),
  104. 1,
  105. new Position( root, [ 1, 0 ] ),
  106. doc.version
  107. );
  108. doc.applyOperation( operation );
  109. expect( doc.version ).to.equal( 1 );
  110. expect( root.getChildCount() ).to.equal( 2 );
  111. expect( p1.getChildCount() ).to.equal( 0 );
  112. expect( p2.getChildCount() ).to.equal( 1 );
  113. expect( p2.getChild( 0 ).name ).to.equal( 'x' );
  114. doc.applyOperation( operation.getReversed() );
  115. expect( doc.version ).to.equal( 2 );
  116. expect( root.getChildCount() ).to.equal( 2 );
  117. expect( p1.getChildCount() ).to.equal( 1 );
  118. expect( p1.getChild( 0 ).name ).to.equal( 'x' );
  119. expect( p2.getChildCount() ).to.equal( 0 );
  120. } );
  121. it( 'should throw an error if number of nodes to move exceeds the number of existing nodes in given element', () => {
  122. root.insertChildren( 0, 'xbarx' );
  123. let operation = new MoveOperation(
  124. new Position( root, [ 3 ] ),
  125. 3,
  126. new Position( root, [ 1 ] ),
  127. doc.version
  128. );
  129. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-nodes-do-not-exist/ );
  130. } );
  131. it( 'should throw an error if target or source parent-element specified by position does not exist', () => {
  132. let p = new Element( 'p' );
  133. p.insertChildren( 0, 'foo' );
  134. root.insertChildren( 0, [ 'ab', p ] );
  135. let operation = new MoveOperation(
  136. new Position( root, [ 2, 0 ] ),
  137. 3,
  138. new Position( root, [ 1 ] ),
  139. doc.version
  140. );
  141. root.removeChildren( 2, 1 );
  142. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-position-invalid/ );
  143. } );
  144. it( 'should throw an error if operation tries to move a range between the beginning and the end of that range', () => {
  145. root.insertChildren( 0, 'xbarx' );
  146. let operation = new MoveOperation(
  147. new Position( root, [ 1 ] ),
  148. 3,
  149. new Position( root, [ 2 ] ),
  150. doc.version
  151. );
  152. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-range-into-itself/ );
  153. } );
  154. it( 'should throw an error if operation tries to move a range into a sub-tree of a node that is in that range', () => {
  155. let p = new Element( 'p', [], [ new Element( 'p' ) ] );
  156. root.insertChildren( 0, [ 'ab', p, 'xy' ] );
  157. let operation = new MoveOperation(
  158. new Position( root, [ 1 ] ),
  159. 3,
  160. new Position( root, [ 2, 0, 0 ] ),
  161. doc.version
  162. );
  163. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-node-into-itself/ );
  164. } );
  165. it( 'should not throw an error if operation move a range into a sibling', () => {
  166. let p = new Element( 'p' );
  167. root.insertChildren( 0, [ 'ab', p, 'xy' ] );
  168. let operation = new MoveOperation(
  169. new Position( root, [ 1 ] ),
  170. 1,
  171. new Position( root, [ 2, 0 ] ),
  172. doc.version
  173. );
  174. expect(
  175. () => {
  176. doc.applyOperation( operation );
  177. }
  178. ).not.to.throw();
  179. expect( root.getChildCount() ).to.equal( 4 );
  180. expect( p.getChildCount() ).to.equal( 1 );
  181. expect( p.getChild( 0 ).character ).to.equal( 'b' );
  182. } );
  183. it( 'should create MoveOperation with the same parameters when cloned', () => {
  184. let sourcePosition = new Position( root, [ 0 ] );
  185. let targetPosition = new Position( root, [ 1 ] );
  186. let howMany = 4;
  187. let baseVersion = doc.version;
  188. let op = new MoveOperation( sourcePosition, howMany, targetPosition, baseVersion );
  189. let 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. } );