moveoperation.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document */
  6. /* global describe, before, beforeEach, it, expect */
  7. 'use strict';
  8. const modules = bender.amd.require(
  9. 'document/document',
  10. 'document/operation/moveoperation',
  11. 'document/position',
  12. 'document/element',
  13. 'document/nodelist',
  14. 'ckeditorerror'
  15. );
  16. describe( 'MoveOperation', () => {
  17. let Document, MoveOperation, Position, Element, NodeList, CKEditorError;
  18. before( () => {
  19. Document = modules[ 'document/document' ];
  20. MoveOperation = modules[ 'document/operation/moveoperation' ];
  21. Position = modules[ 'document/position' ];
  22. Element = modules[ 'document/element' ];
  23. NodeList = modules[ 'document/nodelist' ];
  24. CKEditorError = modules.ckeditorerror;
  25. } );
  26. let doc, root;
  27. beforeEach( () => {
  28. doc = new Document();
  29. root = doc.createRoot( 'root' );
  30. } );
  31. it( 'should have proper type', () => {
  32. const opp = new MoveOperation(
  33. new Position( [ 0, 0 ], root ),
  34. new Position( [ 1, 0 ], root ),
  35. 1,
  36. doc.version
  37. );
  38. expect( opp.type ).to.equal( 'move' );
  39. } );
  40. it( 'should move from one node to another', () => {
  41. let p1 = new Element( 'p1', [], new Element( 'x' ) );
  42. let p2 = new Element( 'p2' );
  43. root.insertChildren( 0, [ p1, p2 ] );
  44. doc.applyOperation(
  45. new MoveOperation(
  46. new Position( [ 0, 0 ], root ),
  47. new Position( [ 1, 0 ], root ),
  48. 1,
  49. doc.version
  50. )
  51. );
  52. expect( doc.version ).to.equal( 1 );
  53. expect( root.getChildCount() ).to.equal( 2 );
  54. expect( root.getChild( 0 ).name ).to.equal( 'p1' );
  55. expect( root.getChild( 1 ).name ).to.equal( 'p2' );
  56. expect( p1.getChildCount() ).to.equal( 0 );
  57. expect( p2.getChildCount() ).to.equal( 1 );
  58. expect( p2.getChild( 0 ).name ).to.equal( 'x' );
  59. } );
  60. it( 'should move position of children in one node backward', () => {
  61. root.insertChildren( 0, 'xbarx' );
  62. doc.applyOperation(
  63. new MoveOperation(
  64. new Position( [ 2 ], root ),
  65. new Position( [ 1 ], root ),
  66. 2,
  67. doc.version
  68. )
  69. );
  70. expect( doc.version ).to.equal( 1 );
  71. expect( root.getChildCount() ).to.equal( 5 );
  72. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  73. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  74. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  75. expect( root.getChild( 3 ).character ).to.equal( 'b' );
  76. expect( root.getChild( 4 ).character ).to.equal( 'x' );
  77. } );
  78. it( 'should move position of children in one node forward', () => {
  79. root.insertChildren( 0, 'xbarx' );
  80. doc.applyOperation(
  81. new MoveOperation(
  82. new Position( [ 1 ], root ),
  83. new Position( [ 4 ], root ),
  84. 2,
  85. doc.version
  86. )
  87. );
  88. expect( doc.version ).to.equal( 1 );
  89. expect( root.getChildCount() ).to.equal( 5 );
  90. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  91. expect( root.getChild( 1 ).character ).to.equal( 'r' );
  92. expect( root.getChild( 2 ).character ).to.equal( 'b' );
  93. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  94. expect( root.getChild( 4 ).character ).to.equal( 'x' );
  95. } );
  96. it( 'should create a move operation as a reverse', () => {
  97. let nodeList = new NodeList( 'bar' );
  98. let sourcePosition = new Position( [ 0 ], root );
  99. let targetPosition = new Position( [ 4 ], root );
  100. let operation = new MoveOperation( sourcePosition, targetPosition, nodeList.length, doc.version );
  101. let reverse = operation.getReversed();
  102. expect( reverse ).to.be.an.instanceof( MoveOperation );
  103. expect( reverse.baseVersion ).to.equal( 1 );
  104. expect( reverse.howMany ).to.equal( nodeList.length );
  105. expect( reverse.sourcePosition.isEqual( targetPosition ) ).to.be.true;
  106. expect( reverse.targetPosition.isEqual( sourcePosition ) ).to.be.true;
  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( [ 0, 0 ], root ),
  114. new Position( [ 1, 0 ], root ),
  115. 1,
  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( [ 3 ], root ),
  135. new Position( [ 1 ], root ),
  136. 3,
  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( [ 2, 0 ], root ),
  147. new Position( [ 1 ], root ),
  148. 3,
  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( [ 1 ], root ),
  158. new Position( [ 2 ], root ),
  159. 3,
  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( [ 1 ], root ),
  169. new Position( [ 2, 0, 0 ], root ),
  170. 3,
  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( [ 1 ], root ),
  180. new Position( [ 2, 0 ], root ),
  181. 1,
  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 create operation with the same parameters when cloned', () => {
  194. let sourcePosition = new Position( [ 0 ], root );
  195. let targetPosition = new Position( [ 1 ], root );
  196. let howMany = 4;
  197. let baseVersion = doc.version;
  198. let op = new MoveOperation( sourcePosition, targetPosition, howMany, baseVersion );
  199. let clone = op.clone();
  200. // New instance rather than a pointer to the old instance.
  201. expect( clone ).not.to.be.equal( op );
  202. expect( clone ).to.be.instanceof( MoveOperation );
  203. expect( clone.sourcePosition.isEqual( sourcePosition ) ).to.be.true;
  204. expect( clone.targetPosition.isEqual( targetPosition ) ).to.be.true;
  205. expect( clone.howMany ).to.equal( howMany );
  206. expect( clone.baseVersion ).to.equal( baseVersion );
  207. } );
  208. } );