reinsertoperation.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 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._appendChild( 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._insertChild( 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. describe( '_validate()', () => {
  82. it( 'should throw when target position is not in the document', () => {
  83. const docFrag = new DocumentFragment();
  84. graveyard._insertChild( 0, new Text( 'xx' ) );
  85. operation = new ReinsertOperation(
  86. graveyardPosition,
  87. 1,
  88. Position.createAt( docFrag ),
  89. doc.version
  90. );
  91. expect( () => {
  92. operation._validate();
  93. } ).to.throw( CKEditorError, /^reinsert-operation-to-detached-parent/ );
  94. } );
  95. it( 'should throw when source position is not in the document', () => {
  96. const docFrag = new DocumentFragment( new Text( 'xx' ) );
  97. operation = new ReinsertOperation(
  98. Position.createAt( docFrag ),
  99. 1,
  100. rootPosition,
  101. doc.version
  102. );
  103. expect( () => {
  104. operation._validate();
  105. } ).to.throw( CKEditorError, /^reinsert-operation-on-detached-item/ );
  106. } );
  107. } );
  108. describe( 'toJSON', () => {
  109. it( 'should create proper json object', () => {
  110. const serialized = jsonParseStringify( operation );
  111. expect( serialized ).to.deep.equal( {
  112. __className: 'engine.model.operation.ReinsertOperation',
  113. baseVersion: 0,
  114. howMany: 2,
  115. isSticky: false,
  116. sourcePosition: jsonParseStringify( operation.sourcePosition ),
  117. targetPosition: jsonParseStringify( operation.targetPosition )
  118. } );
  119. } );
  120. } );
  121. describe( 'fromJSON', () => {
  122. it( 'should create proper ReinsertOperation from json object', () => {
  123. const serialized = jsonParseStringify( operation );
  124. const deserialized = ReinsertOperation.fromJSON( serialized, doc );
  125. expect( deserialized ).to.deep.equal( operation );
  126. } );
  127. } );
  128. } );