removeoperation.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/insertoperation',
  10. 'document/removeoperation',
  11. 'document/position',
  12. 'document/character' );
  13. describe( 'RemoveOperation', function() {
  14. it( 'should remove node', function() {
  15. var Document = modules[ 'document/document' ];
  16. var RemoveOperation = modules[ 'document/removeoperation' ];
  17. var Position = modules[ 'document/position' ];
  18. var Character = modules[ 'document/character' ];
  19. var doc = new Document();
  20. doc.root.children.push( new Character( doc.root, 'x' ) );
  21. doc.applyOperation( new RemoveOperation(
  22. new Position( [ 0 ], doc.root ),
  23. doc.root.children[ 0 ],
  24. doc.version ) );
  25. expect( doc.version ).to.be.equal( 1 );
  26. expect( doc.root.children.length ).to.be.equal( 0 );
  27. } );
  28. it( 'should remove set of nodes', function() {
  29. var Document = modules[ 'document/document' ];
  30. var RemoveOperation = modules[ 'document/removeoperation' ];
  31. var Position = modules[ 'document/position' ];
  32. var Character = modules[ 'document/character' ];
  33. var doc = new Document();
  34. doc.root.children.push( new Character( doc.root, 'b' ) );
  35. doc.root.children.push( new Character( doc.root, 'a' ) );
  36. doc.root.children.push( new Character( doc.root, 'r' ) );
  37. doc.applyOperation( new RemoveOperation(
  38. new Position( [ 0 ], doc.root ),
  39. [ doc.root.children[ 0 ], doc.root.children[ 1 ], doc.root.children[ 2 ] ],
  40. doc.version ) );
  41. expect( doc.version ).to.be.equal( 1 );
  42. expect( doc.root.children.length ).to.be.equal( 0 );
  43. } );
  44. it( 'should remove from between existing nodes', function() {
  45. var Document = modules[ 'document/document' ];
  46. var RemoveOperation = modules[ 'document/removeoperation' ];
  47. var Position = modules[ 'document/position' ];
  48. var Character = modules[ 'document/character' ];
  49. var doc = new Document();
  50. doc.root.children.push( new Character( doc.root, 'b' ) );
  51. doc.root.children.push( new Character( doc.root, 'a' ) );
  52. doc.root.children.push( new Character( doc.root, 'r' ) );
  53. doc.applyOperation( new RemoveOperation(
  54. new Position( [ 1 ], doc.root ),
  55. [ doc.root.children[ 1 ] ],
  56. doc.version ) );
  57. expect( doc.version ).to.be.equal( 1 );
  58. expect( doc.root.children.length ).to.be.equal( 2 );
  59. expect( doc.root.children[ 0 ].character ).to.be.equal( 'b' );
  60. expect( doc.root.children[ 1 ].character ).to.be.equal( 'r' );
  61. } );
  62. it( 'should create a insert operation as a reverse', function() {
  63. var Document = modules[ 'document/document' ];
  64. var InsertOperation = modules[ 'document/insertoperation' ];
  65. var RemoveOperation = modules[ 'document/removeoperation' ];
  66. var Position = modules[ 'document/position' ];
  67. var Character = modules[ 'document/character' ];
  68. var doc = new Document();
  69. var nodes = [ new Character( null, 'b' ), new Character( null, 'a' ), new Character( null, 'r' ) ];
  70. var position = new Position( [ 0 ], doc.root );
  71. doc.root.children.push( nodes[ 0 ] );
  72. doc.root.children.push( nodes[ 1 ] );
  73. doc.root.children.push( nodes[ 2 ] );
  74. var operation = new RemoveOperation( position, nodes, 0 );
  75. var reverse = operation.reverseOperation();
  76. expect( reverse ).to.be.an.instanceof( InsertOperation );
  77. expect( reverse.baseVersion ).to.be.equals( 1 );
  78. expect( reverse.nodes ).to.be.equals( nodes );
  79. expect( reverse.position ).to.be.equals( position );
  80. } );
  81. it( 'should undo remove set of nodes by applying reverse operation', function() {
  82. var Document = modules[ 'document/document' ];
  83. var RemoveOperation = modules[ 'document/removeoperation' ];
  84. var Position = modules[ 'document/position' ];
  85. var Character = modules[ 'document/character' ];
  86. var doc = new Document();
  87. var nodes = [ new Character( null, 'b' ), new Character( null, 'a' ), new Character( null, 'r' ) ];
  88. var position = new Position( [ 0 ], doc.root );
  89. doc.root.children.push( nodes[ 0 ] );
  90. doc.root.children.push( nodes[ 1 ] );
  91. doc.root.children.push( nodes[ 2 ] );
  92. var operation = new RemoveOperation( position, nodes, 0 );
  93. var reverse = operation.reverseOperation();
  94. doc.applyOperation( operation );
  95. expect( doc.version ).to.be.equal( 1 );
  96. expect( doc.root.children.length ).to.be.equal( 0 );
  97. doc.applyOperation( reverse );
  98. expect( doc.version ).to.be.equal( 2 );
  99. expect( doc.root.children.length ).to.be.equal( 3 );
  100. expect( doc.root.children[ 0 ].character ).to.be.equal( 'b' );
  101. expect( doc.root.children[ 1 ].character ).to.be.equal( 'a' );
  102. expect( doc.root.children[ 2 ].character ).to.be.equal( 'r' );
  103. } );
  104. } );