moveoperation.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/character',
  12. 'document/element',
  13. 'document/nodelist',
  14. 'ckeditorerror' );
  15. describe( 'MoveOperation', function() {
  16. it( 'should move from one node to another', function() {
  17. var Document = modules[ 'document/document' ];
  18. var MoveOperation = modules[ 'document/moveoperation' ];
  19. var Position = modules[ 'document/position' ];
  20. var Element = modules[ 'document/element' ];
  21. var doc = new Document();
  22. var p1 = new Element( 'p1', [], new Element( 'x' ) );
  23. var p2 = new Element( 'p2' );
  24. doc.root.insertChildren( 0, [ p1, p2 ] );
  25. doc.applyOperation( new MoveOperation(
  26. new Position( [ 0, 0 ], doc.root ),
  27. new Position( [ 1, 0 ], doc.root ),
  28. p1.getChild( 0 ),
  29. doc.version ) );
  30. expect( doc.version ).to.be.equal( 1 );
  31. expect( doc.root.getChildCount() ).to.be.equal( 2 );
  32. expect( doc.root.getChild( 0 ).name ).to.be.equal( 'p1' );
  33. expect( doc.root.getChild( 1 ).name ).to.be.equal( 'p2' );
  34. expect( p1.getChildCount() ).to.be.equal( 0 );
  35. expect( p2.getChildCount() ).to.be.equal( 1 );
  36. expect( p2.getChild( 0 ).name ).to.be.equal( 'x' );
  37. } );
  38. it( 'should move position of children in one node backward', function() {
  39. var Document = modules[ 'document/document' ];
  40. var MoveOperation = modules[ 'document/moveoperation' ];
  41. var Position = modules[ 'document/position' ];
  42. var doc = new Document();
  43. doc.root.insertChildren( 0, 'xbarx' );
  44. doc.applyOperation( new MoveOperation(
  45. new Position( [ 2 ], doc.root ),
  46. new Position( [ 1 ], doc.root ),
  47. [ doc.root.getChild( 2 ), doc.root.getChild( 3 ) ],
  48. doc.version ) );
  49. expect( doc.version ).to.be.equal( 1 );
  50. expect( doc.root.getChildCount() ).to.be.equal( 5 );
  51. expect( doc.root.getChild( 0 ).character ).to.be.equal( 'x' );
  52. expect( doc.root.getChild( 1 ).character ).to.be.equal( 'a' );
  53. expect( doc.root.getChild( 2 ).character ).to.be.equal( 'r' );
  54. expect( doc.root.getChild( 3 ).character ).to.be.equal( 'b' );
  55. expect( doc.root.getChild( 4 ).character ).to.be.equal( 'x' );
  56. } );
  57. it( 'should move position of children in one node forward', function() {
  58. var Document = modules[ 'document/document' ];
  59. var MoveOperation = modules[ 'document/moveoperation' ];
  60. var Position = modules[ 'document/position' ];
  61. var doc = new Document();
  62. doc.root.insertChildren( 0, 'xbarx' );
  63. doc.applyOperation( new MoveOperation(
  64. new Position( [ 1 ], doc.root ),
  65. new Position( [ 4 ], doc.root ),
  66. [ doc.root.getChild( 1 ), doc.root.getChild( 2 ) ],
  67. doc.version ) );
  68. expect( doc.version ).to.be.equal( 1 );
  69. expect( doc.root.getChildCount() ).to.be.equal( 5 );
  70. expect( doc.root.getChild( 0 ).character ).to.be.equal( 'x' );
  71. expect( doc.root.getChild( 1 ).character ).to.be.equal( 'r' );
  72. expect( doc.root.getChild( 2 ).character ).to.be.equal( 'b' );
  73. expect( doc.root.getChild( 3 ).character ).to.be.equal( 'a' );
  74. expect( doc.root.getChild( 4 ).character ).to.be.equal( 'x' );
  75. } );
  76. it( 'should create a move operation as a reverse', function() {
  77. var Document = modules[ 'document/document' ];
  78. var MoveOperation = modules[ 'document/moveoperation' ];
  79. var Position = modules[ 'document/position' ];
  80. var NodeList = modules[ 'document/nodelist' ];
  81. var doc = new Document();
  82. var nodeList = new NodeList( 'bar' );
  83. var sourcePosition = new Position( [ 0 ], doc.root );
  84. var targetPosition = new Position( [ 4 ], doc.root );
  85. var operation = new MoveOperation( sourcePosition, targetPosition, nodeList, doc.version );
  86. var reverse = operation.reverseOperation();
  87. expect( reverse ).to.be.an.instanceof( MoveOperation );
  88. expect( reverse.baseVersion ).to.be.equals( 1 );
  89. expect( reverse.nodeList ).to.be.equals( nodeList );
  90. expect( reverse.sourcePosition ).to.be.equals( targetPosition );
  91. expect( reverse.targetPosition ).to.be.equals( sourcePosition );
  92. } );
  93. it( 'should move node by applying reverse operation', function() {
  94. var Document = modules[ 'document/document' ];
  95. var MoveOperation = modules[ 'document/moveoperation' ];
  96. var Position = modules[ 'document/position' ];
  97. var Element = modules[ 'document/element' ];
  98. var doc = new Document();
  99. var p1 = new Element( 'p1', [], new Element( 'x' ) );
  100. var p2 = new Element( 'p2' );
  101. doc.root.insertChildren( 0, [ p1, p2 ] );
  102. var operation = new MoveOperation(
  103. new Position( [ 0, 0 ], doc.root ),
  104. new Position( [ 1, 0 ], doc.root ),
  105. p1.getChild( 0 ),
  106. doc.version );
  107. doc.applyOperation( operation );
  108. expect( doc.version ).to.be.equal( 1 );
  109. expect( doc.root.getChildCount() ).to.be.equal( 2 );
  110. expect( p1.getChildCount() ).to.be.equal( 0 );
  111. expect( p2.getChildCount() ).to.be.equal( 1 );
  112. expect( p2.getChild( 0 ).name ).to.be.equal( 'x' );
  113. doc.applyOperation( operation.reverseOperation() );
  114. expect( doc.version ).to.be.equal( 2 );
  115. expect( doc.root.getChildCount() ).to.be.equal( 2 );
  116. expect( p1.getChildCount() ).to.be.equal( 1 );
  117. expect( p1.getChild( 0 ).name ).to.be.equal( 'x' );
  118. expect( p2.getChildCount() ).to.be.equal( 0 );
  119. } );
  120. if ( CKEDITOR.isDebug ) {
  121. it( 'should throw error if nodes to move are different then actual nodes', function() {
  122. var Document = modules[ 'document/document' ];
  123. var MoveOperation = modules[ 'document/moveoperation' ];
  124. var Position = modules[ 'document/position' ];
  125. var CKEditorError = modules.ckeditorerror;
  126. var doc = new Document();
  127. doc.root.insertChildren( 0, 'xbarx' );
  128. expect( function() {
  129. doc.applyOperation( new MoveOperation(
  130. new Position( [ 1 ], doc.root ),
  131. new Position( [ 4 ], doc.root ),
  132. 'y',
  133. doc.version ) );
  134. } ).to.throw( CKEditorError, /operation-move-node-does-not-exists/ );
  135. } );
  136. }
  137. } );