8
0

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