removeoperation.js 4.8 KB

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