moveoperation.js 7.3 KB

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