moveoperation.js 5.9 KB

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