insertoperation.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import NodeList from '../../../src/model/nodelist';
  7. import Element from '../../../src/model/element';
  8. import InsertOperation from '../../../src/model/operation/insertoperation';
  9. import RemoveOperation from '../../../src/model/operation/removeoperation';
  10. import Position from '../../../src/model/position';
  11. import Text from '../../../src/model/text';
  12. import { jsonParseStringify, wrapInDelta } from '../../../tests/model/_utils/utils';
  13. describe( 'InsertOperation', () => {
  14. let doc, root;
  15. beforeEach( () => {
  16. doc = new Document();
  17. root = doc.createRoot();
  18. } );
  19. it( 'should have proper type', () => {
  20. const op = new InsertOperation(
  21. new Position( root, [ 0 ] ),
  22. new Text( 'x' ),
  23. doc.version
  24. );
  25. expect( op.type ).to.equal( 'insert' );
  26. } );
  27. it( 'should insert text node', () => {
  28. doc.applyOperation( wrapInDelta(
  29. new InsertOperation(
  30. new Position( root, [ 0 ] ),
  31. new Text( 'x' ),
  32. doc.version
  33. )
  34. ) );
  35. expect( doc.version ).to.equal( 1 );
  36. expect( root.maxOffset ).to.equal( 1 );
  37. expect( root.getChild( 0 ).data ).to.equal( 'x' );
  38. } );
  39. it( 'should insert element', () => {
  40. doc.applyOperation( wrapInDelta(
  41. new InsertOperation(
  42. new Position( root, [ 0 ] ),
  43. new Element( 'p' ),
  44. doc.version
  45. )
  46. ) );
  47. expect( doc.version ).to.equal( 1 );
  48. expect( root.maxOffset ).to.equal( 1 );
  49. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  50. } );
  51. it( 'should insert set of nodes', () => {
  52. doc.applyOperation( wrapInDelta(
  53. new InsertOperation(
  54. new Position( root, [ 0 ] ),
  55. [ 'bar', new Element( 'p' ), 'foo' ],
  56. doc.version
  57. )
  58. ) );
  59. expect( doc.version ).to.equal( 1 );
  60. expect( root.maxOffset ).to.equal( 7 );
  61. expect( root.childCount ).to.equal( 3 );
  62. expect( root.getChild( 0 ).data ).to.equal( 'bar' );
  63. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  64. expect( root.getChild( 2 ).data ).to.equal( 'foo' );
  65. } );
  66. it( 'should insert between existing nodes', () => {
  67. root.insertChildren( 0, new Text( 'xy' ) );
  68. doc.applyOperation( wrapInDelta(
  69. new InsertOperation(
  70. new Position( root, [ 1 ] ),
  71. 'bar',
  72. doc.version
  73. )
  74. ) );
  75. expect( doc.version ).to.equal( 1 );
  76. expect( root.maxOffset ).to.equal( 5 );
  77. expect( root.getChild( 0 ).data ).to.equal( 'xbary' );
  78. } );
  79. it( 'should insert text', () => {
  80. doc.applyOperation( wrapInDelta(
  81. new InsertOperation(
  82. new Position( root, [ 0 ] ),
  83. [ 'foo', new Text( 'x' ), 'bar' ],
  84. doc.version
  85. )
  86. ) );
  87. expect( doc.version ).to.equal( 1 );
  88. expect( root.maxOffset ).to.equal( 7 );
  89. expect( root.getChild( 0 ).data ).to.equal( 'fooxbar' );
  90. } );
  91. it( 'should create a RemoveOperation as a reverse', () => {
  92. const position = new Position( root, [ 0 ] );
  93. const operation = new InsertOperation(
  94. position,
  95. [ 'foo', new Text( 'x' ), 'bar' ],
  96. 0
  97. );
  98. const reverse = operation.getReversed();
  99. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  100. expect( reverse.baseVersion ).to.equal( 1 );
  101. expect( reverse.sourcePosition.isEqual( position ) ).to.be.true;
  102. expect( reverse.howMany ).to.equal( 7 );
  103. } );
  104. it( 'should undo insert node by applying reverse operation', () => {
  105. const operation = new InsertOperation(
  106. new Position( root, [ 0 ] ),
  107. new Text( 'x' ),
  108. doc.version
  109. );
  110. const reverse = operation.getReversed();
  111. doc.applyOperation( wrapInDelta( operation ) );
  112. expect( doc.version ).to.equal( 1 );
  113. doc.applyOperation( wrapInDelta( reverse ) );
  114. expect( doc.version ).to.equal( 2 );
  115. expect( root.maxOffset ).to.equal( 0 );
  116. } );
  117. it( 'should undo insert set of nodes by applying reverse operation', () => {
  118. const operation = new InsertOperation(
  119. new Position( root, [ 0 ] ),
  120. 'bar',
  121. doc.version
  122. );
  123. const reverse = operation.getReversed();
  124. doc.applyOperation( wrapInDelta( operation ) );
  125. expect( doc.version ).to.equal( 1 );
  126. doc.applyOperation( wrapInDelta( reverse ) );
  127. expect( doc.version ).to.equal( 2 );
  128. expect( root.maxOffset ).to.equal( 0 );
  129. } );
  130. it( 'should create operation with the same parameters when cloned', () => {
  131. const position = new Position( root, [ 0 ] );
  132. const nodeA = new Element( 'a' );
  133. const nodeB = new Element( 'b' );
  134. const nodes = [ nodeA, nodeB ];
  135. const baseVersion = doc.version;
  136. const op = new InsertOperation( position, nodes, baseVersion );
  137. const clone = op.clone();
  138. // New instance rather than a pointer to the old instance.
  139. expect( clone ).not.to.be.equal( op );
  140. expect( clone ).to.be.instanceof( InsertOperation );
  141. expect( clone.position.isEqual( position ) ).to.be.true;
  142. // New node, not pointer to the old instance.
  143. expect( clone.nodes.getNode( 0 ) ).not.to.equal( nodeA );
  144. expect( clone.nodes.getNode( 1 ) ).not.to.equal( nodeB );
  145. expect( clone.nodes.getNode( 0 ) ).to.deep.equal( nodeA );
  146. expect( clone.nodes.getNode( 1 ) ).to.deep.equal( nodeB );
  147. expect( clone.nodes.length ).to.equal( 2 );
  148. expect( clone.baseVersion ).to.equal( baseVersion );
  149. } );
  150. it( 'should save copies of inserted nodes after it is executed', () => {
  151. const element = new Element( 'p', { key: 'value' } );
  152. const op = new InsertOperation( new Position( root, [ 0 ] ), element, doc.version );
  153. doc.applyOperation( wrapInDelta( op ) );
  154. const text = new Text( 'text' );
  155. const op2 = new InsertOperation( new Position( root, [ 0, 0 ] ), text, doc.version );
  156. doc.applyOperation( wrapInDelta( op2 ) );
  157. expect( op.nodes.getNode( 0 ) ).not.to.equal( element );
  158. expect( op.nodes.getNode( 0 ).name ).to.equal( 'p' );
  159. expect( Array.from( op.nodes.getNode( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'key', 'value' ] ] );
  160. expect( op.nodes.getNode( 0 ).childCount ).to.equal( 0 );
  161. expect( element.childCount ).to.equal( 1 );
  162. expect( op2.nodes.getNode( 0 ) ).not.to.equal( text );
  163. } );
  164. describe( 'isDocumentOperation', () => {
  165. it( 'should return true when element is inserted to the document', () => {
  166. const op = new InsertOperation(
  167. new Position( root, [ 0 ] ),
  168. new Text( 'x' ),
  169. doc.version
  170. );
  171. expect( op.isDocumentOperation ).to.true;
  172. } );
  173. it( 'should return false when element is inserted to document fragment', () => {
  174. const docFrag = doc.batch().createDocumentFragment();
  175. const op = new InsertOperation(
  176. new Position( docFrag, [ 0 ] ),
  177. new Text( 'x' ),
  178. doc.version
  179. );
  180. expect( op.isDocumentOperation ).to.false;
  181. } );
  182. } );
  183. describe( 'toJSON', () => {
  184. it( 'should create proper json object', () => {
  185. const position = new Position( root, [ 0 ] );
  186. const op = new InsertOperation( position, new Text( 'x' ), doc.version );
  187. const serialized = jsonParseStringify( op );
  188. expect( serialized ).to.deep.equal( {
  189. __className: 'engine.model.operation.InsertOperation',
  190. baseVersion: 0,
  191. nodes: jsonParseStringify( new NodeList( [ new Text( 'x' ) ] ) ),
  192. position: jsonParseStringify( position )
  193. } );
  194. } );
  195. } );
  196. describe( 'fromJSON', () => {
  197. it( 'should create proper InsertOperation from json object', () => {
  198. const position = new Position( root, [ 0 ] );
  199. const op = new InsertOperation(
  200. position,
  201. [ new Text( 'x' ), new Element( 'p', [], new Text( 'foo' ) ), 'y' ],
  202. doc.version
  203. );
  204. const serialized = jsonParseStringify( op );
  205. const deserialized = InsertOperation.fromJSON( serialized, doc );
  206. expect( deserialized ).to.deep.equal( op );
  207. } );
  208. } );
  209. } );