insertoperation.js 7.2 KB

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