reinsertoperation.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model, operation */
  6. import Document from 'ckeditor5/engine/model/document.js';
  7. import ReinsertOperation from 'ckeditor5/engine/model/operation/reinsertoperation.js';
  8. import RemoveOperation from 'ckeditor5/engine/model/operation/removeoperation.js';
  9. import MoveOperation from 'ckeditor5/engine/model/operation/moveoperation.js';
  10. import Position from 'ckeditor5/engine/model/position.js';
  11. import Element from 'ckeditor5/engine/model/element.js';
  12. import Text from 'ckeditor5/engine/model/text.js';
  13. import { jsonParseStringify, wrapInDelta } from 'tests/engine/model/_utils/utils.js';
  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, 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. let 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 a RemoveOperation as a reverse', () => {
  53. let 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.root ).to.equal( graveyardPosition.root );
  59. } );
  60. it( 'should undo reinsert set of nodes by applying reverse operation', () => {
  61. let reverse = operation.getReversed();
  62. const element = new Element();
  63. element.insertChildren( 0, new Text( 'xx' ) );
  64. graveyard.insertChildren( 0, element );
  65. doc.applyOperation( wrapInDelta( operation ) );
  66. expect( doc.version ).to.equal( 1 );
  67. expect( root.maxOffset ).to.equal( 2 );
  68. expect( element.maxOffset ).to.equal( 0 );
  69. doc.applyOperation( wrapInDelta( reverse ) );
  70. expect( doc.version ).to.equal( 2 );
  71. expect( root.maxOffset ).to.equal( 0 );
  72. // Don't check `element` - nodes are moved to new holder element.
  73. } );
  74. describe( 'toJSON', () => {
  75. it( 'should create proper json object', () => {
  76. const serialized = jsonParseStringify( operation );
  77. expect( serialized ).to.deep.equal( {
  78. __className: 'engine.model.operation.ReinsertOperation',
  79. baseVersion: 0,
  80. howMany: 2,
  81. isSticky: false,
  82. sourcePosition: jsonParseStringify( operation.sourcePosition ),
  83. targetPosition: jsonParseStringify( operation.targetPosition )
  84. } );
  85. } );
  86. } );
  87. describe( 'fromJSON', () => {
  88. it( 'should create proper ReinsertOperation from json object', () => {
  89. const serialized = jsonParseStringify( operation );
  90. const deserialized = ReinsertOperation.fromJSON( serialized, doc );
  91. expect( deserialized ).to.deep.equal( operation );
  92. } );
  93. } );
  94. } );