moveoperation.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. 'use strict';
  7. const modules = bender.amd.require(
  8. 'document/document',
  9. 'document/operation/moveoperation',
  10. 'document/position',
  11. 'document/element',
  12. 'document/nodelist',
  13. 'ckeditorerror'
  14. );
  15. describe( 'MoveOperation', function() {
  16. let Document, MoveOperation, Position, Element, NodeList, CKEditorError;
  17. before( function() {
  18. Document = modules[ 'document/document' ];
  19. MoveOperation = modules[ 'document/operation/moveoperation' ];
  20. Position = modules[ 'document/position' ];
  21. Element = modules[ 'document/element' ];
  22. NodeList = modules[ 'document/nodelist' ];
  23. CKEditorError = modules.ckeditorerror;
  24. } );
  25. let doc, root;
  26. beforeEach( function() {
  27. doc = new Document();
  28. root = doc.createRoot( 'root' );
  29. } );
  30. it( 'should move from one node to another', function() {
  31. let p1 = new Element( 'p1', [], new Element( 'x' ) );
  32. let p2 = new Element( 'p2' );
  33. root.insertChildren( 0, [ p1, p2 ] );
  34. doc.applyOperation(
  35. new MoveOperation(
  36. new Position( [ 0, 0 ], root ),
  37. new Position( [ 1, 0 ], root ),
  38. 1,
  39. doc.version
  40. )
  41. );
  42. expect( doc.version ).to.equal( 1 );
  43. expect( root.getChildCount() ).to.equal( 2 );
  44. expect( root.getChild( 0 ).name ).to.equal( 'p1' );
  45. expect( root.getChild( 1 ).name ).to.equal( 'p2' );
  46. expect( p1.getChildCount() ).to.equal( 0 );
  47. expect( p2.getChildCount() ).to.equal( 1 );
  48. expect( p2.getChild( 0 ).name ).to.equal( 'x' );
  49. } );
  50. it( 'should move position of children in one node backward', function() {
  51. root.insertChildren( 0, 'xbarx' );
  52. doc.applyOperation(
  53. new MoveOperation(
  54. new Position( [ 2 ], root ),
  55. new Position( [ 1 ], root ),
  56. 2,
  57. doc.version
  58. )
  59. );
  60. expect( doc.version ).to.equal( 1 );
  61. expect( root.getChildCount() ).to.equal( 5 );
  62. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  63. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  64. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  65. expect( root.getChild( 3 ).character ).to.equal( 'b' );
  66. expect( root.getChild( 4 ).character ).to.equal( 'x' );
  67. } );
  68. it( 'should move position of children in one node forward', function() {
  69. root.insertChildren( 0, 'xbarx' );
  70. doc.applyOperation(
  71. new MoveOperation(
  72. new Position( [ 1 ], root ),
  73. new Position( [ 4 ], root ),
  74. 2,
  75. doc.version
  76. )
  77. );
  78. expect( doc.version ).to.equal( 1 );
  79. expect( root.getChildCount() ).to.equal( 5 );
  80. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  81. expect( root.getChild( 1 ).character ).to.equal( 'r' );
  82. expect( root.getChild( 2 ).character ).to.equal( 'b' );
  83. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  84. expect( root.getChild( 4 ).character ).to.equal( 'x' );
  85. } );
  86. it( 'should create a move operation as a reverse', function() {
  87. let nodeList = new NodeList( 'bar' );
  88. let sourcePosition = new Position( [ 0 ], root );
  89. let targetPosition = new Position( [ 4 ], root );
  90. let operation = new MoveOperation( sourcePosition, targetPosition, nodeList.length, doc.version );
  91. let reverse = operation.getReversed();
  92. expect( reverse ).to.be.an.instanceof( MoveOperation );
  93. expect( reverse.baseVersion ).to.equal( 1 );
  94. expect( reverse.howMany ).to.equal( nodeList.length );
  95. expect( reverse.sourcePosition ).to.equal( targetPosition );
  96. expect( reverse.targetPosition ).to.equal( sourcePosition );
  97. } );
  98. it( 'should undo move node by applying reverse operation', function() {
  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( [ 0, 0 ], root ),
  104. new Position( [ 1, 0 ], root ),
  105. 1,
  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', function() {
  122. root.insertChildren( 0, 'xbarx' );
  123. expect(
  124. function() {
  125. doc.applyOperation(
  126. new MoveOperation(
  127. new Position( [ 3 ], root ),
  128. new Position( [ 1 ], root ),
  129. 3,
  130. doc.version
  131. )
  132. );
  133. }
  134. ).to.throw( CKEditorError, /operation-move-nodes-do-not-exist/ );
  135. } );
  136. it( 'should throw an error if target or source parent-element specified by position does not exist', function() {
  137. let p = new Element( 'p' );
  138. p.insertChildren( 0, 'foo' );
  139. root.insertChildren( 0, [ 'ab', p ] );
  140. let operation = new MoveOperation(
  141. new Position( [ 2, 0 ], root ),
  142. new Position( [ 1 ], root ),
  143. 3,
  144. doc.version
  145. );
  146. root.removeChildren( 2, 1 );
  147. expect(
  148. function() {
  149. doc.applyOperation( operation );
  150. }
  151. ).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', function() {
  154. root.insertChildren( 0, 'xbarx' );
  155. let operation = new MoveOperation(
  156. new Position( [ 1 ], root ),
  157. new Position( [ 2 ], root ),
  158. 3,
  159. doc.version
  160. );
  161. expect(
  162. function() {
  163. doc.applyOperation( operation );
  164. }
  165. ).to.throw( CKEditorError, /operation-move-range-into-itself/ );
  166. } );
  167. it( 'should throw an error if operation tries to move a range into a sub-tree of a node that is in that range', function() {
  168. let p = new Element( 'p', [], [ new Element( 'p' ) ] );
  169. root.insertChildren( 0, [ 'ab', p, 'xy' ] );
  170. let operation = new MoveOperation(
  171. new Position( [ 1 ], root ),
  172. new Position( [ 2, 0, 0 ], root ),
  173. 3,
  174. doc.version
  175. );
  176. expect(
  177. function() {
  178. doc.applyOperation( operation );
  179. }
  180. ).to.throw( CKEditorError, /operation-move-node-into-itself/ );
  181. } );
  182. it( 'should not throw an error if operation move a range into a sibling', function() {
  183. let p = new Element( 'p' );
  184. root.insertChildren( 0, [ 'ab', p, 'xy' ] );
  185. let operation = new MoveOperation(
  186. new Position( [ 1 ], root ),
  187. new Position( [ 2, 0 ], root ),
  188. 1,
  189. doc.version
  190. );
  191. expect(
  192. function() {
  193. doc.applyOperation( operation );
  194. }
  195. ).not.to.throw();
  196. expect( root.getChildCount() ).to.equal( 4 );
  197. expect( p.getChildCount() ).to.equal( 1 );
  198. expect( p.getChild( 0 ).character ).to.equal( 'b' );
  199. } );
  200. } );