moveoperation.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, operation */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import MoveOperation from '/ckeditor5/core/treemodel/operation/moveoperation.js';
  9. import Position from '/ckeditor5/core/treemodel/position.js';
  10. import Element from '/ckeditor5/core/treemodel/element.js';
  11. import NodeList from '/ckeditor5/core/treemodel/nodelist.js';
  12. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  13. describe( 'MoveOperation', () => {
  14. let doc, root;
  15. beforeEach( () => {
  16. doc = new Document();
  17. root = doc.createRoot( 'root' );
  18. } );
  19. it( 'should have proper type', () => {
  20. const op = new MoveOperation(
  21. new Position( root, [ 0, 0 ] ),
  22. 1,
  23. new Position( root, [ 1, 0 ] ),
  24. doc.version
  25. );
  26. expect( op.type ).to.equal( 'move' );
  27. } );
  28. it( 'should move from one node to another', () => {
  29. let p1 = new Element( 'p1', [], new Element( 'x' ) );
  30. let p2 = new Element( 'p2' );
  31. root.insertChildren( 0, [ p1, p2 ] );
  32. doc.applyOperation(
  33. new MoveOperation(
  34. new Position( root, [ 0, 0 ] ),
  35. 1,
  36. new Position( root, [ 1, 0 ] ),
  37. doc.version
  38. )
  39. );
  40. expect( doc.version ).to.equal( 1 );
  41. expect( root.getChildCount() ).to.equal( 2 );
  42. expect( root.getChild( 0 ).name ).to.equal( 'p1' );
  43. expect( root.getChild( 1 ).name ).to.equal( 'p2' );
  44. expect( p1.getChildCount() ).to.equal( 0 );
  45. expect( p2.getChildCount() ).to.equal( 1 );
  46. expect( p2.getChild( 0 ).name ).to.equal( 'x' );
  47. } );
  48. it( 'should move position of children in one node backward', () => {
  49. root.insertChildren( 0, 'xbarx' );
  50. doc.applyOperation(
  51. new MoveOperation(
  52. new Position( root, [ 2 ] ),
  53. 2,
  54. new Position( root, [ 1 ] ),
  55. doc.version
  56. )
  57. );
  58. expect( doc.version ).to.equal( 1 );
  59. expect( root.getChildCount() ).to.equal( 5 );
  60. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  61. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  62. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  63. expect( root.getChild( 3 ).character ).to.equal( 'b' );
  64. expect( root.getChild( 4 ).character ).to.equal( 'x' );
  65. } );
  66. it( 'should move position of children in one node forward', () => {
  67. root.insertChildren( 0, 'xbarx' );
  68. doc.applyOperation(
  69. new MoveOperation(
  70. new Position( root, [ 1 ] ),
  71. 2,
  72. new Position( root, [ 4 ] ),
  73. doc.version
  74. )
  75. );
  76. expect( doc.version ).to.equal( 1 );
  77. expect( root.getChildCount() ).to.equal( 5 );
  78. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  79. expect( root.getChild( 1 ).character ).to.equal( 'r' );
  80. expect( root.getChild( 2 ).character ).to.equal( 'b' );
  81. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  82. expect( root.getChild( 4 ).character ).to.equal( 'x' );
  83. } );
  84. it( 'should create a MoveOperation as a reverse', () => {
  85. let nodeList = new NodeList( 'bar' );
  86. let sourcePosition = new Position( root, [ 0 ] );
  87. let targetPosition = new Position( root, [ 4 ] );
  88. let operation = new MoveOperation( sourcePosition, nodeList.length, targetPosition, doc.version );
  89. let reverse = operation.getReversed();
  90. expect( reverse ).to.be.an.instanceof( MoveOperation );
  91. expect( reverse.baseVersion ).to.equal( 1 );
  92. expect( reverse.howMany ).to.equal( nodeList.length );
  93. expect( reverse.sourcePosition.isEqual( targetPosition ) ).to.be.true;
  94. expect( reverse.targetPosition.isEqual( sourcePosition ) ).to.be.true;
  95. } );
  96. it( 'should undo move node by applying reverse operation', () => {
  97. let p1 = new Element( 'p1', [], new Element( 'x' ) );
  98. let p2 = new Element( 'p2' );
  99. root.insertChildren( 0, [ p1, p2 ] );
  100. let operation = new MoveOperation(
  101. new Position( root, [ 0, 0 ] ),
  102. 1,
  103. new Position( root, [ 1, 0 ] ),
  104. doc.version
  105. );
  106. doc.applyOperation( operation );
  107. expect( doc.version ).to.equal( 1 );
  108. expect( root.getChildCount() ).to.equal( 2 );
  109. expect( p1.getChildCount() ).to.equal( 0 );
  110. expect( p2.getChildCount() ).to.equal( 1 );
  111. expect( p2.getChild( 0 ).name ).to.equal( 'x' );
  112. doc.applyOperation( operation.getReversed() );
  113. expect( doc.version ).to.equal( 2 );
  114. expect( root.getChildCount() ).to.equal( 2 );
  115. expect( p1.getChildCount() ).to.equal( 1 );
  116. expect( p1.getChild( 0 ).name ).to.equal( 'x' );
  117. expect( p2.getChildCount() ).to.equal( 0 );
  118. } );
  119. it( 'should throw an error if number of nodes to move exceeds the number of existing nodes in given element', () => {
  120. root.insertChildren( 0, 'xbarx' );
  121. let operation = new MoveOperation(
  122. new Position( root, [ 3 ] ),
  123. 3,
  124. new Position( root, [ 1 ] ),
  125. doc.version
  126. );
  127. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-nodes-do-not-exist/ );
  128. } );
  129. it( 'should throw an error if target or source parent-element specified by position does not exist', () => {
  130. let p = new Element( 'p' );
  131. p.insertChildren( 0, 'foo' );
  132. root.insertChildren( 0, [ 'ab', p ] );
  133. let operation = new MoveOperation(
  134. new Position( root, [ 2, 0 ] ),
  135. 3,
  136. new Position( root, [ 1 ] ),
  137. doc.version
  138. );
  139. root.removeChildren( 2, 1 );
  140. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-position-invalid/ );
  141. } );
  142. it( 'should throw an error if operation tries to move a range between the beginning and the end of that range', () => {
  143. root.insertChildren( 0, 'xbarx' );
  144. let operation = new MoveOperation(
  145. new Position( root, [ 1 ] ),
  146. 3,
  147. new Position( root, [ 2 ] ),
  148. doc.version
  149. );
  150. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-range-into-itself/ );
  151. } );
  152. it( 'should throw an error if operation tries to move a range into a sub-tree of a node that is in that range', () => {
  153. let p = new Element( 'p', [], [ new Element( 'p' ) ] );
  154. root.insertChildren( 0, [ 'ab', p, 'xy' ] );
  155. let operation = new MoveOperation(
  156. new Position( root, [ 1 ] ),
  157. 3,
  158. new Position( root, [ 2, 0, 0 ] ),
  159. doc.version
  160. );
  161. expect( () => doc.applyOperation( operation ) ).to.throw( CKEditorError, /operation-move-node-into-itself/ );
  162. } );
  163. it( 'should not throw an error if operation move a range into a sibling', () => {
  164. let p = new Element( 'p' );
  165. root.insertChildren( 0, [ 'ab', p, 'xy' ] );
  166. let operation = new MoveOperation(
  167. new Position( root, [ 1 ] ),
  168. 1,
  169. new Position( root, [ 2, 0 ] ),
  170. doc.version
  171. );
  172. expect(
  173. () => {
  174. doc.applyOperation( operation );
  175. }
  176. ).not.to.throw();
  177. expect( root.getChildCount() ).to.equal( 4 );
  178. expect( p.getChildCount() ).to.equal( 1 );
  179. expect( p.getChild( 0 ).character ).to.equal( 'b' );
  180. } );
  181. it( 'should create MoveOperation with the same parameters when cloned', () => {
  182. let sourcePosition = new Position( root, [ 0 ] );
  183. let targetPosition = new Position( root, [ 1 ] );
  184. let howMany = 4;
  185. let baseVersion = doc.version;
  186. let op = new MoveOperation( sourcePosition, howMany, targetPosition, baseVersion );
  187. let clone = op.clone();
  188. // New instance rather than a pointer to the old instance.
  189. expect( clone ).not.to.be.equal( op );
  190. expect( clone ).to.be.instanceof( MoveOperation );
  191. expect( clone.sourcePosition.isEqual( sourcePosition ) ).to.be.true;
  192. expect( clone.targetPosition.isEqual( targetPosition ) ).to.be.true;
  193. expect( clone.howMany ).to.equal( howMany );
  194. expect( clone.baseVersion ).to.equal( baseVersion );
  195. } );
  196. } );