8
0

insertoperation.js 5.1 KB

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