8
0

insertoperation.js 6.1 KB

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