insertoperation.js 6.9 KB

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