reinsertoperation.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 } 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 extend MoveOperation class', () => {
  41. expect( operation ).to.be.instanceof( MoveOperation );
  42. } );
  43. it( 'should create ReinsertOperation with same parameters when cloned', () => {
  44. const clone = operation.clone();
  45. expect( clone ).to.be.instanceof( ReinsertOperation );
  46. expect( clone.sourcePosition.isEqual( operation.sourcePosition ) ).to.be.true;
  47. expect( clone.targetPosition.isEqual( operation.targetPosition ) ).to.be.true;
  48. expect( clone.howMany ).to.equal( operation.howMany );
  49. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  50. } );
  51. it( 'should create RemoveOperation as a reverse', () => {
  52. graveyard._appendChild( new Element( 'x' ) );
  53. const reverse = operation.getReversed();
  54. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  55. expect( reverse.baseVersion ).to.equal( 1 );
  56. expect( reverse.howMany ).to.equal( 2 );
  57. expect( reverse.sourcePosition.isEqual( rootPosition ) ).to.be.true;
  58. expect( reverse.targetPosition.isEqual( graveyardPosition ) ).to.be.true;
  59. } );
  60. it( 'should create correct RemoveOperation when reversed if target position was in graveyard', () => {
  61. const operation = new ReinsertOperation( new Position( doc.graveyard, [ 0 ] ), 1, new Position( doc.graveyard, [ 3 ] ), 0 );
  62. const reverse = operation.getReversed();
  63. expect( reverse.sourcePosition.path ).to.deep.equal( [ 2 ] );
  64. expect( reverse.targetPosition.path ).to.deep.equal( [ 0 ] );
  65. } );
  66. it( 'should undo reinsert set of nodes by applying reverse operation', () => {
  67. const reverse = operation.getReversed();
  68. graveyard._insertChild( 0, new Text( 'xx' ) );
  69. model.applyOperation( operation );
  70. expect( doc.version ).to.equal( 1 );
  71. expect( root.maxOffset ).to.equal( 2 );
  72. expect( graveyard.maxOffset ).to.equal( 0 );
  73. model.applyOperation( reverse );
  74. expect( doc.version ).to.equal( 2 );
  75. expect( root.maxOffset ).to.equal( 0 );
  76. expect( graveyard.maxOffset ).to.equal( 2 );
  77. } );
  78. describe( '_validate()', () => {
  79. it( 'should throw when target position is not in the document', () => {
  80. const docFrag = new DocumentFragment();
  81. graveyard._insertChild( 0, new Text( 'xx' ) );
  82. operation = new ReinsertOperation(
  83. graveyardPosition,
  84. 1,
  85. Position.createAt( docFrag ),
  86. doc.version
  87. );
  88. expect( () => {
  89. operation._validate();
  90. } ).to.throw( CKEditorError, /^reinsert-operation-to-detached-parent/ );
  91. } );
  92. it( 'should throw when source position is not in the document', () => {
  93. const docFrag = new DocumentFragment( new Text( 'xx' ) );
  94. operation = new ReinsertOperation(
  95. Position.createAt( docFrag ),
  96. 1,
  97. rootPosition,
  98. doc.version
  99. );
  100. expect( () => {
  101. operation._validate();
  102. } ).to.throw( CKEditorError, /^reinsert-operation-on-detached-item/ );
  103. } );
  104. } );
  105. describe( 'toJSON', () => {
  106. it( 'should create proper json object', () => {
  107. const serialized = jsonParseStringify( operation );
  108. expect( serialized ).to.deep.equal( {
  109. __className: 'engine.model.operation.ReinsertOperation',
  110. baseVersion: 0,
  111. howMany: 2,
  112. sourcePosition: jsonParseStringify( operation.sourcePosition ),
  113. targetPosition: jsonParseStringify( operation.targetPosition )
  114. } );
  115. } );
  116. } );
  117. describe( 'fromJSON', () => {
  118. it( 'should create proper ReinsertOperation from json object', () => {
  119. const serialized = jsonParseStringify( operation );
  120. const deserialized = ReinsertOperation.fromJSON( serialized, doc );
  121. expect( deserialized ).to.deep.equal( operation );
  122. } );
  123. } );
  124. } );