insertoperation.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. const modules = bender.amd.require(
  8. 'document/document',
  9. 'document/operation/insertoperation',
  10. 'document/operation/removeoperation',
  11. 'document/position',
  12. 'document/character',
  13. 'document/nodelist'
  14. );
  15. describe( 'InsertOperation', function() {
  16. let Document, InsertOperation, RemoveOperation, Position, Character;
  17. before( function() {
  18. Document = modules[ 'document/document' ];
  19. InsertOperation = modules[ 'document/operation/insertoperation' ];
  20. RemoveOperation = modules[ 'document/operation/removeoperation' ];
  21. Position = modules[ 'document/position' ];
  22. Character = modules[ 'document/character' ];
  23. } );
  24. var doc, root;
  25. beforeEach( function() {
  26. doc = new Document();
  27. root = doc.createRoot( 'root' );
  28. } );
  29. it( 'should insert node', function() {
  30. doc.applyOperation(
  31. new InsertOperation(
  32. new Position( [ 0 ], root ),
  33. new Character( 'x' ),
  34. doc.version
  35. )
  36. );
  37. expect( doc.version ).to.equal( 1 );
  38. expect( root.getChildCount() ).to.equal( 1 );
  39. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  40. } );
  41. it( 'should insert set of nodes', function() {
  42. doc.applyOperation(
  43. new InsertOperation(
  44. new Position( [ 0 ], root ),
  45. 'bar',
  46. doc.version
  47. )
  48. );
  49. expect( doc.version ).to.equal( 1 );
  50. expect( root.getChildCount() ).to.equal( 3 );
  51. expect( root.getChild( 0 ).character ).to.equal( 'b' );
  52. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  53. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  54. } );
  55. it( 'should insert between existing nodes', function() {
  56. root.insertChildren( 0, 'xy' );
  57. doc.applyOperation(
  58. new InsertOperation(
  59. new Position( [ 1 ], root ),
  60. 'bar',
  61. doc.version
  62. )
  63. );
  64. expect( doc.version ).to.equal( 1 );
  65. expect( root.getChildCount() ).to.equal( 5 );
  66. expect( root.getChild( 0 ).character ).to.equal( 'x' );
  67. expect( root.getChild( 1 ).character ).to.equal( 'b' );
  68. expect( root.getChild( 2 ).character ).to.equal( 'a' );
  69. expect( root.getChild( 3 ).character ).to.equal( 'r' );
  70. expect( root.getChild( 4 ).character ).to.equal( 'y' );
  71. } );
  72. it( 'should insert text', function() {
  73. doc.applyOperation(
  74. new InsertOperation(
  75. new Position( [ 0 ], root ),
  76. [ 'foo', new Character( 'x' ), 'bar' ],
  77. doc.version
  78. )
  79. );
  80. expect( doc.version ).to.equal( 1 );
  81. expect( root.getChildCount() ).to.equal( 7 );
  82. expect( root.getChild( 0 ).character ).to.equal( 'f' );
  83. expect( root.getChild( 1 ).character ).to.equal( 'o' );
  84. expect( root.getChild( 2 ).character ).to.equal( 'o' );
  85. expect( root.getChild( 3 ).character ).to.equal( 'x' );
  86. expect( root.getChild( 4 ).character ).to.equal( 'b' );
  87. expect( root.getChild( 5 ).character ).to.equal( 'a' );
  88. expect( root.getChild( 6 ).character ).to.equal( 'r' );
  89. } );
  90. it( 'should create a remove operation as a reverse', function() {
  91. var position = new Position( [ 0 ], root );
  92. var operation = new InsertOperation(
  93. position,
  94. [ 'foo', new Character( 'x' ), 'bar' ],
  95. 0
  96. );
  97. var reverse = operation.getReversed();
  98. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  99. expect( reverse.baseVersion ).to.equal( 1 );
  100. expect( reverse.sourcePosition ).to.equal( position );
  101. expect( reverse.howMany ).to.equal( 7 );
  102. } );
  103. it( 'should undo insert node by applying reverse operation', function() {
  104. var operation = new InsertOperation(
  105. new Position( [ 0 ], root ),
  106. new Character( 'x' ),
  107. doc.version
  108. );
  109. var reverse = operation.getReversed();
  110. doc.applyOperation( operation );
  111. expect( doc.version ).to.equal( 1 );
  112. doc.applyOperation( reverse );
  113. expect( doc.version ).to.equal( 2 );
  114. expect( root.getChildCount() ).to.equal( 0 );
  115. } );
  116. it( 'should undo insert set of nodes by applying reverse operation', function() {
  117. var operation = new InsertOperation(
  118. new Position( [ 0 ], root ),
  119. 'bar',
  120. doc.version
  121. );
  122. var reverse = operation.getReversed();
  123. doc.applyOperation( operation );
  124. expect( doc.version ).to.equal( 1 );
  125. doc.applyOperation( reverse );
  126. expect( doc.version ).to.equal( 2 );
  127. expect( root.getChildCount() ).to.equal( 0 );
  128. } );
  129. } );