8
0

moveoperation.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. var modules = bender.amd.require(
  8. 'document/document',
  9. 'document/moveoperation',
  10. 'document/position',
  11. 'document/element',
  12. 'document/nodelist',
  13. 'ckeditorerror'
  14. );
  15. describe( 'MoveOperation', function() {
  16. var Document, MoveOperation, Position, Element, NodeList, CKEditorError;
  17. before( function() {
  18. Document = modules[ 'document/document' ];
  19. MoveOperation = modules[ 'document/moveoperation' ];
  20. Position = modules[ 'document/position' ];
  21. Element = modules[ 'document/element' ];
  22. NodeList = modules[ 'document/nodelist' ];
  23. CKEditorError = modules.ckeditorerror;
  24. } );
  25. var 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. var p1 = new Element( 'p1', [], new Element( 'x' ) );
  32. var 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. var nodeList = new NodeList( 'bar' );
  88. var sourcePosition = new Position( [ 0 ], root );
  89. var targetPosition = new Position( [ 4 ], root );
  90. var operation = new MoveOperation( sourcePosition, targetPosition, nodeList.length, doc.version );
  91. var reverse = operation.reverseOperation();
  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. var p1 = new Element( 'p1', [], new Element( 'x' ) );
  100. var p2 = new Element( 'p2' );
  101. root.insertChildren( 0, [ p1, p2 ] );
  102. var 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.reverseOperation() );
  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( function() {
  124. doc.applyOperation(
  125. new MoveOperation(
  126. new Position( [ 3 ], root ),
  127. new Position( [ 1 ], root ),
  128. 3,
  129. doc.version
  130. )
  131. );
  132. } ).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', function() {
  135. var p = new Element( 'p' );
  136. p.insertChildren( 0, 'foo' );
  137. root.insertChildren( 0, [ 'ab', p ] );
  138. var operation = new MoveOperation(
  139. new Position( [ 2, 0 ], root ),
  140. new Position( [ 1 ], root ),
  141. 3,
  142. doc.version
  143. );
  144. root.removeChildren( 2, 1 );
  145. expect( function() {
  146. doc.applyOperation( operation );
  147. } ).to.throw( CKEditorError, /operation-move-position-invalid/ );
  148. } );
  149. it( 'should throw an error if operation tries to move a range between the beginning and the end of that range', function() {
  150. root.insertChildren( 0, 'xbarx' );
  151. var operation = new MoveOperation(
  152. new Position( [ 1 ], root ),
  153. new Position( [ 2 ], root ),
  154. 3,
  155. doc.version
  156. );
  157. expect( function() {
  158. doc.applyOperation( operation );
  159. } ).to.throw( CKEditorError, /operation-move-range-into-itself/ );
  160. } );
  161. 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() {
  162. var p = new Element( 'p', [], [ new Element( 'p' ) ] );
  163. root.insertChildren( 0, [ 'ab', p, 'xy' ] );
  164. var operation = new MoveOperation(
  165. new Position( [ 1 ], root ),
  166. new Position( [ 2, 0, 0 ], root ),
  167. 3,
  168. doc.version
  169. );
  170. expect( function() {
  171. doc.applyOperation( operation );
  172. } ).to.throw( CKEditorError, /operation-move-node-into-itself/ );
  173. } );
  174. } );