reinsertoperation.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import ReinsertOperation from '/ckeditor5/engine/model/operation/reinsertoperation.js';
  9. import RemoveOperation from '/ckeditor5/engine/model/operation/removeoperation.js';
  10. import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
  11. import Position from '/ckeditor5/engine/model/position.js';
  12. import Element from '/ckeditor5/engine/model/element.js';
  13. import Text from '/ckeditor5/engine/model/text.js';
  14. import { jsonParseStringify, wrapInDelta } from '/tests/engine/model/_utils/utils.js';
  15. describe( 'ReinsertOperation', () => {
  16. let doc, root, graveyard, operation, graveyardPosition, rootPosition;
  17. beforeEach( () => {
  18. doc = new Document();
  19. root = doc.createRoot();
  20. graveyard = doc.graveyard;
  21. graveyardPosition = new Position( graveyard, [ 0, 0 ] );
  22. rootPosition = new Position( root, [ 0 ] );
  23. operation = new ReinsertOperation(
  24. graveyardPosition,
  25. 2,
  26. rootPosition,
  27. doc.version
  28. );
  29. } );
  30. it( 'should have position property equal to the position where node will be reinserted', () => {
  31. expect( operation.position.isEqual( rootPosition ) ).to.be.true;
  32. // Setting also works:
  33. operation.position = new Position( root, [ 1 ] );
  34. expect( operation.position.isEqual( new Position( root, [ 1 ] ) ) ).to.be.true;
  35. } );
  36. it( 'should have proper type', () => {
  37. expect( operation.type ).to.equal( 'reinsert' );
  38. } );
  39. it( 'should not be sticky', () => {
  40. expect( operation.isSticky ).to.be.false;
  41. } );
  42. it( 'should extend MoveOperation class', () => {
  43. expect( operation ).to.be.instanceof( MoveOperation );
  44. } );
  45. it( 'should create ReinsertOperation with same parameters when cloned', () => {
  46. let clone = operation.clone();
  47. expect( clone ).to.be.instanceof( ReinsertOperation );
  48. expect( clone.sourcePosition.isEqual( operation.sourcePosition ) ).to.be.true;
  49. expect( clone.targetPosition.isEqual( operation.targetPosition ) ).to.be.true;
  50. expect( clone.howMany ).to.equal( operation.howMany );
  51. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  52. } );
  53. it( 'should create a RemoveOperation as a reverse', () => {
  54. let 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.root ).to.equal( graveyardPosition.root );
  60. } );
  61. it( 'should undo reinsert set of nodes by applying reverse operation', () => {
  62. let reverse = operation.getReversed();
  63. const element = new Element();
  64. element.insertChildren( 0, new Text( 'xx' ) );
  65. graveyard.insertChildren( 0, element );
  66. doc.applyOperation( wrapInDelta( operation ) );
  67. expect( doc.version ).to.equal( 1 );
  68. expect( root.getMaxOffset() ).to.equal( 2 );
  69. expect( element.getMaxOffset() ).to.equal( 0 );
  70. doc.applyOperation( wrapInDelta( reverse ) );
  71. expect( doc.version ).to.equal( 2 );
  72. expect( root.getMaxOffset() ).to.equal( 0 );
  73. // Don't check `element` - nodes are moved to new holder element.
  74. } );
  75. describe( 'toJSON', () => {
  76. it( 'should create proper json object', () => {
  77. const serialized = jsonParseStringify( operation );
  78. expect( serialized ).to.deep.equal( {
  79. __className: 'engine.model.operation.ReinsertOperation',
  80. baseVersion: 0,
  81. howMany: 2,
  82. isSticky: false,
  83. movedRangeStart: jsonParseStringify( operation.movedRangeStart ),
  84. sourcePosition: jsonParseStringify( operation.sourcePosition ),
  85. targetPosition: jsonParseStringify( operation.targetPosition )
  86. } );
  87. } );
  88. } );
  89. describe( 'fromJSON', () => {
  90. it( 'should create proper ReinsertOperation from json object', () => {
  91. const serialized = jsonParseStringify( operation );
  92. const deserialized = ReinsertOperation.fromJSON( serialized, doc );
  93. expect( deserialized ).to.deep.equal( operation );
  94. } );
  95. } );
  96. } );