reinsertoperation.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 ReinsertOperation from '../../../src/model/operation/reinsertoperation';
  7. import RemoveOperation from '../../../src/model/operation/removeoperation';
  8. import MoveOperation from '../../../src/model/operation/moveoperation';
  9. import Position from '../../../src/model/position';
  10. import DocumentFragment from '../../../src/model/documentfragment';
  11. import Element from '../../../src/model/element';
  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( 'ReinsertOperation', () => {
  16. let model, doc, root, graveyard, operation, graveyardPosition, rootPosition;
  17. beforeEach( () => {
  18. model = new Model();
  19. doc = model.document;
  20. root = doc.createRoot();
  21. graveyard = doc.graveyard;
  22. graveyardPosition = new Position( graveyard, [ 0 ] );
  23. rootPosition = new Position( root, [ 0 ] );
  24. operation = new ReinsertOperation(
  25. graveyardPosition,
  26. 2,
  27. rootPosition,
  28. doc.version
  29. );
  30. } );
  31. it( 'should have position property equal to the position where node will be reinserted', () => {
  32. expect( operation.position.isEqual( rootPosition ) ).to.be.true;
  33. // Setting also works:
  34. operation.position = new Position( root, [ 1 ] );
  35. expect( operation.position.isEqual( new Position( root, [ 1 ] ) ) ).to.be.true;
  36. } );
  37. it( 'should have proper type', () => {
  38. expect( operation.type ).to.equal( 'reinsert' );
  39. } );
  40. it( 'should not be sticky', () => {
  41. expect( operation.isSticky ).to.be.false;
  42. } );
  43. it( 'should extend MoveOperation class', () => {
  44. expect( operation ).to.be.instanceof( MoveOperation );
  45. } );
  46. it( 'should create ReinsertOperation with same parameters when cloned', () => {
  47. const clone = operation.clone();
  48. expect( clone ).to.be.instanceof( ReinsertOperation );
  49. expect( clone.sourcePosition.isEqual( operation.sourcePosition ) ).to.be.true;
  50. expect( clone.targetPosition.isEqual( operation.targetPosition ) ).to.be.true;
  51. expect( clone.howMany ).to.equal( operation.howMany );
  52. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  53. } );
  54. it( 'should create RemoveOperation as a reverse', () => {
  55. graveyard.appendChildren( new Element( 'x' ) );
  56. const reverse = operation.getReversed();
  57. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  58. expect( reverse.baseVersion ).to.equal( 1 );
  59. expect( reverse.howMany ).to.equal( 2 );
  60. expect( reverse.sourcePosition.isEqual( rootPosition ) ).to.be.true;
  61. expect( reverse.targetPosition.isEqual( graveyardPosition ) ).to.be.true;
  62. } );
  63. it( 'should create correct RemoveOperation when reversed if target position was in graveyard', () => {
  64. const operation = new ReinsertOperation( new Position( doc.graveyard, [ 0 ] ), 1, new Position( doc.graveyard, [ 3 ] ), 0 );
  65. const reverse = operation.getReversed();
  66. expect( reverse.sourcePosition.path ).to.deep.equal( [ 2 ] );
  67. expect( reverse.targetPosition.path ).to.deep.equal( [ 0 ] );
  68. } );
  69. it( 'should undo reinsert set of nodes by applying reverse operation', () => {
  70. const reverse = operation.getReversed();
  71. graveyard.insertChildren( 0, new Text( 'xx' ) );
  72. model.applyOperation( wrapInDelta( operation ) );
  73. expect( doc.version ).to.equal( 1 );
  74. expect( root.maxOffset ).to.equal( 2 );
  75. expect( graveyard.maxOffset ).to.equal( 0 );
  76. model.applyOperation( wrapInDelta( reverse ) );
  77. expect( doc.version ).to.equal( 2 );
  78. expect( root.maxOffset ).to.equal( 0 );
  79. expect( graveyard.maxOffset ).to.equal( 2 );
  80. } );
  81. it( 'should be a document operation', () => {
  82. expect( operation.isDocumentOperation ).to.true;
  83. } );
  84. describe( '_validate()', () => {
  85. it( 'should throw when target position is not in the document', () => {
  86. const docFrag = new DocumentFragment();
  87. graveyard.insertChildren( 0, new Text( 'xx' ) );
  88. operation = new ReinsertOperation(
  89. graveyardPosition,
  90. 1,
  91. Position.createAt( docFrag ),
  92. doc.version
  93. );
  94. expect( () => {
  95. operation._validate();
  96. } ).to.throw( CKEditorError, /^reinsert-operation-to-detached-parent/ );
  97. } );
  98. it( 'should throw when source position is not in the document', () => {
  99. const docFrag = new DocumentFragment( new Text( 'xx' ) );
  100. operation = new ReinsertOperation(
  101. Position.createAt( docFrag ),
  102. 1,
  103. rootPosition,
  104. doc.version
  105. );
  106. expect( () => {
  107. operation._validate();
  108. } ).to.throw( CKEditorError, /^reinsert-operation-on-detached-item/ );
  109. } );
  110. } );
  111. describe( 'toJSON', () => {
  112. it( 'should create proper json object', () => {
  113. const serialized = jsonParseStringify( operation );
  114. expect( serialized ).to.deep.equal( {
  115. __className: 'engine.model.operation.ReinsertOperation',
  116. baseVersion: 0,
  117. howMany: 2,
  118. isSticky: false,
  119. sourcePosition: jsonParseStringify( operation.sourcePosition ),
  120. targetPosition: jsonParseStringify( operation.targetPosition )
  121. } );
  122. } );
  123. } );
  124. describe( 'fromJSON', () => {
  125. it( 'should create proper ReinsertOperation from json object', () => {
  126. const serialized = jsonParseStringify( operation );
  127. const deserialized = ReinsertOperation.fromJSON( serialized, doc );
  128. expect( deserialized ).to.deep.equal( operation );
  129. } );
  130. } );
  131. } );