8
0

reinsertoperation.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 { jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
  13. describe( 'ReinsertOperation', () => {
  14. let doc, root, graveyard, operation, graveyardPosition, rootPosition;
  15. beforeEach( () => {
  16. doc = new Document();
  17. root = doc.createRoot( '$root', 'root' );
  18. graveyard = doc.graveyard;
  19. graveyardPosition = new Position( graveyard, [ 0 ] );
  20. rootPosition = new Position( root, [ 0 ] );
  21. operation = new ReinsertOperation(
  22. graveyardPosition,
  23. 2,
  24. rootPosition,
  25. doc.version
  26. );
  27. } );
  28. it( 'should have position property equal to the position where node will be reinserted', () => {
  29. expect( operation.position.isEqual( rootPosition ) ).to.be.true;
  30. // Setting also works:
  31. operation.position = new Position( root, [ 1 ] );
  32. expect( operation.position.isEqual( new Position( root, [ 1 ] ) ) ).to.be.true;
  33. } );
  34. it( 'should have proper type', () => {
  35. expect( operation.type ).to.equal( 'reinsert' );
  36. } );
  37. it( 'should not be sticky', () => {
  38. expect( operation.isSticky ).to.be.false;
  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. let 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 a RemoveOperation as a reverse', () => {
  52. let reverse = operation.getReversed();
  53. expect( reverse ).to.be.an.instanceof( RemoveOperation );
  54. expect( reverse.baseVersion ).to.equal( 1 );
  55. expect( reverse.howMany ).to.equal( 2 );
  56. expect( reverse.sourcePosition.isEqual( rootPosition ) ).to.be.true;
  57. expect( reverse.targetPosition.isEqual( graveyardPosition ) ).to.be.true;
  58. } );
  59. it( 'should undo reinsert set of nodes by applying reverse operation', () => {
  60. let reverse = operation.getReversed();
  61. graveyard.insertChildren( 0, 'bar' );
  62. doc.applyOperation( operation );
  63. expect( doc.version ).to.equal( 1 );
  64. expect( root.getChildCount() ).to.equal( 2 );
  65. doc.applyOperation( reverse );
  66. expect( doc.version ).to.equal( 2 );
  67. expect( root.getChildCount() ).to.equal( 0 );
  68. expect( graveyard.getChildCount() ).to.equal( 3 );
  69. expect( graveyard.getChild( 0 ).character ).to.equal( 'b' );
  70. expect( graveyard.getChild( 1 ).character ).to.equal( 'a' );
  71. expect( graveyard.getChild( 2 ).character ).to.equal( 'r' );
  72. } );
  73. describe( 'toJSON', () => {
  74. it( 'should create proper json object', () => {
  75. const serialized = jsonParseStringify( operation );
  76. expect( serialized ).to.deep.equal( {
  77. __className: 'engine.model.operation.ReinsertOperation',
  78. baseVersion: 0,
  79. howMany: 2,
  80. isSticky: false,
  81. movedRangeStart: jsonParseStringify( operation.movedRangeStart ),
  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. } );