moveoperation.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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', () => {
  16. let Document, MoveOperation, Position, Element, NodeList, CKEditorError;
  17. before( () => {
  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( () => {
  27. doc = new Document();
  28. root = doc.createRoot( 'root' );
  29. } );
  30. it( 'should move from one node to another', () => {
  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', () => {
  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', () => {
  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', () => {
  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.isEqual( targetPosition ) ).to.be.true;
  96. expect( reverse.targetPosition.isEqual( sourcePosition ) ).to.be.true;
  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( [ 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', () => {
  122. root.insertChildren( 0, 'xbarx' );
  123. let operation = new MoveOperation(
  124. new Position( [ 3 ], root ),
  125. new Position( [ 1 ], root ),
  126. 3,
  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( [ 2, 0 ], root ),
  137. new Position( [ 1 ], root ),
  138. 3,
  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( [ 1 ], root ),
  148. new Position( [ 2 ], root ),
  149. 3,
  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( [ 1 ], root ),
  159. new Position( [ 2, 0, 0 ], root ),
  160. 3,
  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( [ 1 ], root ),
  170. new Position( [ 2, 0 ], root ),
  171. 1,
  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. } );