insertoperation.js 7.9 KB

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