8
0

insertoperation.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  6. import NodeList from '../../../src/model/nodelist';
  7. import Element from '../../../src/model/element';
  8. import InsertOperation from '../../../src/model/operation/insertoperation';
  9. import MoveOperation from '../../../src/model/operation/moveoperation';
  10. import Position from '../../../src/model/position';
  11. import Text from '../../../src/model/text';
  12. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  13. describe( 'InsertOperation', () => {
  14. let model, doc, root;
  15. beforeEach( () => {
  16. model = new Model();
  17. doc = model.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. model.applyOperation(
  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. model.applyOperation(
  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. model.applyOperation(
  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._insertChild( 0, new Text( 'xy' ) );
  69. model.applyOperation(
  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. model.applyOperation(
  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 MoveOperation as a reverse', () => {
  93. const position = new Position( root, [ 0 ] );
  94. const operation = new InsertOperation(
  95. position,
  96. [ 'foo', new Text( 'x' ), 'bar' ],
  97. 0
  98. );
  99. const reverse = operation.getReversed();
  100. expect( reverse ).to.be.an.instanceof( MoveOperation );
  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. const operation = new InsertOperation(
  107. new Position( root, [ 0 ] ),
  108. new Text( 'x' ),
  109. doc.version
  110. );
  111. const reverse = operation.getReversed();
  112. model.applyOperation( operation );
  113. expect( doc.version ).to.equal( 1 );
  114. model.applyOperation( 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. const operation = new InsertOperation(
  120. new Position( root, [ 0 ] ),
  121. 'bar',
  122. doc.version
  123. );
  124. const reverse = operation.getReversed();
  125. model.applyOperation( operation );
  126. expect( doc.version ).to.equal( 1 );
  127. model.applyOperation( 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. const position = new Position( root, [ 0 ] );
  133. const nodeA = new Element( 'a' );
  134. const nodeB = new Element( 'b' );
  135. const nodes = [ nodeA, nodeB ];
  136. const baseVersion = doc.version;
  137. const op = new InsertOperation( position, nodes, baseVersion );
  138. const 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. const element = new Element( 'p', { key: 'value' } );
  153. const op = new InsertOperation( new Position( root, [ 0 ] ), element, doc.version );
  154. model.applyOperation( op );
  155. const text = new Text( 'text' );
  156. const op2 = new InsertOperation( new Position( root, [ 0, 0 ] ), text, doc.version );
  157. model.applyOperation( 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( '_validate()', () => {
  166. it( 'should throw an error if target position does not exist', () => {
  167. const element = new Element( 'p' );
  168. const op = new InsertOperation( new Position( root, [ 4 ] ), element, doc.version );
  169. expect( () => op._validate() ).to.throw( CKEditorError, /insert-operation-position-invalid/ );
  170. } );
  171. } );
  172. describe( 'toJSON', () => {
  173. it( 'should create proper json object', () => {
  174. const position = new Position( root, [ 0 ] );
  175. const op = new InsertOperation( position, new Text( 'x' ), doc.version );
  176. op.shouldReceiveAttributes = true;
  177. const serialized = op.toJSON();
  178. expect( serialized ).to.deep.equal( {
  179. __className: 'engine.model.operation.InsertOperation',
  180. baseVersion: 0,
  181. nodes: ( new NodeList( [ new Text( 'x' ) ] ) ).toJSON(),
  182. position: position.toJSON(),
  183. shouldReceiveAttributes: true
  184. } );
  185. } );
  186. } );
  187. describe( 'fromJSON', () => {
  188. it( 'should create proper InsertOperation from json object', () => {
  189. const position = new Position( root, [ 0 ] );
  190. const op = new InsertOperation(
  191. position,
  192. [ new Text( 'x' ), new Element( 'p', [], new Text( 'foo' ) ), 'y' ],
  193. doc.version
  194. );
  195. const serialized = op.toJSON();
  196. const deserialized = InsertOperation.fromJSON( serialized, doc );
  197. expect( deserialized ).to.deep.equal( op );
  198. } );
  199. } );
  200. } );