reinsertoperation.js 5.1 KB

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