8
0

insertoperation.js 7.4 KB

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