insertoperation.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model, operation */
  6. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import Node from '/ckeditor5/engine/model/node.js';
  9. import NodeList from '/ckeditor5/engine/model/nodelist.js';
  10. import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
  11. import RemoveOperation from '/ckeditor5/engine/model/operation/removeoperation.js';
  12. import Position from '/ckeditor5/engine/model/position.js';
  13. import Text from '/ckeditor5/engine/model/text.js';
  14. import { jsonParseStringify, wrapInDelta } from '/tests/engine/model/_utils/utils.js';
  15. describe( 'InsertOperation', () => {
  16. let doc, root;
  17. beforeEach( () => {
  18. doc = new Document();
  19. root = doc.createRoot();
  20. } );
  21. it( 'should have proper type', () => {
  22. const op = new InsertOperation(
  23. new Position( root, [ 0 ] ),
  24. new Text( 'x' ),
  25. doc.version
  26. );
  27. expect( op.type ).to.equal( 'insert' );
  28. } );
  29. it( 'should insert node', () => {
  30. doc.applyOperation( wrapInDelta(
  31. new InsertOperation(
  32. new Position( root, [ 0 ] ),
  33. new Text( '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', () => {
  42. doc.applyOperation( wrapInDelta(
  43. new InsertOperation(
  44. new Position( root, [ 0 ] ),
  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', () => {
  56. root.insertChildren( 0, 'xy' );
  57. doc.applyOperation( wrapInDelta(
  58. new InsertOperation(
  59. new Position( root, [ 1 ] ),
  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', () => {
  73. doc.applyOperation( wrapInDelta(
  74. new InsertOperation(
  75. new Position( root, [ 0 ] ),
  76. [ 'foo', new Text( '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 RemoveOperation as a reverse', () => {
  91. let position = new Position( root, [ 0 ] );
  92. let operation = new InsertOperation(
  93. position,
  94. [ 'foo', new Text( 'x' ), 'bar' ],
  95. 0
  96. );
  97. let reverse = operation.getReversed();
  98. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  99. expect( reverse.baseVersion ).to.equal( 1 );
  100. expect( reverse.sourcePosition.isEqual( position ) ).to.be.true;
  101. expect( reverse.howMany ).to.equal( 7 );
  102. } );
  103. it( 'should undo insert node by applying reverse operation', () => {
  104. let operation = new InsertOperation(
  105. new Position( root, [ 0 ] ),
  106. new Text( 'x' ),
  107. doc.version
  108. );
  109. let reverse = operation.getReversed();
  110. doc.applyOperation( wrapInDelta( operation ) );
  111. expect( doc.version ).to.equal( 1 );
  112. doc.applyOperation( wrapInDelta( 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', () => {
  117. let operation = new InsertOperation(
  118. new Position( root, [ 0 ] ),
  119. 'bar',
  120. doc.version
  121. );
  122. let reverse = operation.getReversed();
  123. doc.applyOperation( wrapInDelta( operation ) );
  124. expect( doc.version ).to.equal( 1 );
  125. doc.applyOperation( wrapInDelta( reverse ) );
  126. expect( doc.version ).to.equal( 2 );
  127. expect( root.getChildCount() ).to.equal( 0 );
  128. } );
  129. it( 'should create operation with the same parameters when cloned', () => {
  130. let position = new Position( root, [ 0 ] );
  131. let nodeA = new Node();
  132. let nodeB = new Node();
  133. let nodes = new NodeList( [ nodeA, nodeB ] );
  134. let baseVersion = doc.version;
  135. let op = new InsertOperation( position, nodes, baseVersion );
  136. let clone = op.clone();
  137. // New instance rather than a pointer to the old instance.
  138. expect( clone ).not.to.be.equal( op );
  139. expect( clone ).to.be.instanceof( InsertOperation );
  140. expect( clone.position.isEqual( position ) ).to.be.true;
  141. expect( clone.nodeList.get( 0 ) ).to.equal( nodeA );
  142. expect( clone.nodeList.get( 1 ) ).to.equal( nodeB );
  143. expect( clone.nodeList.length ).to.equal( 2 );
  144. expect( clone.baseVersion ).to.equal( baseVersion );
  145. } );
  146. describe( 'toJSON', () => {
  147. it( 'should create proper json object', () => {
  148. const position = new Position( root, [ 0 ] );
  149. const op = new InsertOperation( position, new Text( 'x' ), doc.version );
  150. const serialized = jsonParseStringify( op );
  151. expect( serialized ).to.deep.equal( {
  152. __className: 'engine.model.operation.InsertOperation',
  153. baseVersion: 0,
  154. nodeList: jsonParseStringify( new NodeList( 'x' ) ),
  155. position: jsonParseStringify( position )
  156. } );
  157. } );
  158. } );
  159. describe( 'fromJSON', () => {
  160. it( 'should create proper InsertOperation from json object', () => {
  161. const position = new Position( root, [ 0 ] );
  162. const op = new InsertOperation( position, new Text( 'x' ), doc.version );
  163. const serialized = jsonParseStringify( op );
  164. const deserialized = InsertOperation.fromJSON( serialized, doc );
  165. expect( deserialized ).to.deep.equal( op );
  166. } );
  167. } );
  168. } );