markeroperation.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 Text from '../../../src/model/text';
  7. import Range from '../../../src/model/range';
  8. import MarkerOperation from '../../../src/model/operation/markeroperation';
  9. import { jsonParseStringify, wrapInDelta } from '../../model/_utils/utils';
  10. function matchRange( range ) {
  11. return sinon.match( rangeToMatch => rangeToMatch.isEqual( range ) );
  12. }
  13. describe( 'MarkerOperation', () => {
  14. let model, doc, root, range;
  15. beforeEach( () => {
  16. model = new Model();
  17. doc = model.document;
  18. root = doc.createRoot();
  19. root._appendChild( new Text( 'foo' ) );
  20. range = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  21. } );
  22. it( 'should have property type equal to "marker"', () => {
  23. const op = new MarkerOperation( 'name', null, range, model.markers, 0, true );
  24. expect( op.type ).to.equal( 'marker' );
  25. } );
  26. it( 'should add marker to document marker collection', () => {
  27. sinon.spy( model.markers, '_set' );
  28. model.applyOperation( wrapInDelta(
  29. new MarkerOperation( 'name', null, range, model.markers, doc.version, true )
  30. ) );
  31. expect( doc.version ).to.equal( 1 );
  32. expect( model.markers._set.calledWith( 'name', matchRange( range ) ) );
  33. expect( model.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  34. } );
  35. it( 'should update marker in document marker collection', () => {
  36. model.applyOperation( wrapInDelta(
  37. new MarkerOperation( 'name', null, range, model.markers, doc.version, true )
  38. ) );
  39. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  40. sinon.spy( model.markers, '_set' );
  41. model.applyOperation( wrapInDelta(
  42. new MarkerOperation( 'name', range, range2, model.markers, doc.version, true )
  43. ) );
  44. expect( doc.version ).to.equal( 2 );
  45. expect( model.markers._set.calledWith( 'name', matchRange( range2 ) ) );
  46. expect( model.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  47. } );
  48. it( 'should remove marker from document marker collection', () => {
  49. model.applyOperation( wrapInDelta(
  50. new MarkerOperation( 'name', null, range, model.markers, doc.version, true )
  51. ) );
  52. sinon.spy( model.markers, '_remove' );
  53. model.applyOperation( wrapInDelta(
  54. new MarkerOperation( 'name', range, null, model.markers, doc.version, true )
  55. ) );
  56. expect( doc.version ).to.equal( 2 );
  57. expect( model.markers._remove.calledWith( 'name' ) );
  58. expect( model.markers.get( 'name' ) ).to.be.null;
  59. } );
  60. it( 'should not fire document markers remove event if removing non-existing range', () => {
  61. sinon.spy( model.markers, 'fire' );
  62. model.applyOperation( wrapInDelta(
  63. new MarkerOperation( 'name', null, null, model.markers, doc.version, true )
  64. ) );
  65. expect( model.markers.fire.notCalled ).to.be.true;
  66. } );
  67. it( 'should not fire document markers set event if newRange is same as current marker range', () => {
  68. model.change( writer => {
  69. writer.addMarker( 'name', { range, usingOperation: true } );
  70. } );
  71. sinon.spy( model.markers, 'fire' );
  72. model.applyOperation( wrapInDelta(
  73. new MarkerOperation( 'name', range, range, model.markers, doc.version, false )
  74. ) );
  75. expect( model.markers.fire.notCalled ).to.be.true;
  76. } );
  77. it( 'should return MarkerOperation with swapped ranges as reverse operation', () => {
  78. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  79. const op1 = new MarkerOperation( 'name', null, range, model.markers, doc.version, true );
  80. const reversed1 = op1.getReversed();
  81. const op2 = new MarkerOperation( 'name', range, range2, model.markers, doc.version, true );
  82. const reversed2 = op2.getReversed();
  83. expect( reversed1 ).to.be.an.instanceof( MarkerOperation );
  84. expect( reversed2 ).to.be.an.instanceof( MarkerOperation );
  85. expect( reversed1.name ).to.equal( 'name' );
  86. expect( reversed1.oldRange.isEqual( range ) ).to.be.true;
  87. expect( reversed1.newRange ).to.be.null;
  88. expect( reversed1.baseVersion ).to.equal( 1 );
  89. expect( reversed1.affectsData ).to.be.true;
  90. expect( reversed2.name ).to.equal( 'name' );
  91. expect( reversed2.oldRange.isEqual( range2 ) ).to.be.true;
  92. expect( reversed2.newRange.isEqual( range ) ).to.be.true;
  93. expect( reversed2.baseVersion ).to.equal( 1 );
  94. expect( reversed2.affectsData ).to.be.true;
  95. } );
  96. it( 'should create a MarkerOperation with the same parameters when cloned', () => {
  97. const op = new MarkerOperation( 'name', null, range, model.markers, 0, true );
  98. const clone = op.clone();
  99. expect( clone ).to.be.an.instanceof( MarkerOperation );
  100. expect( clone ).to.deep.equal( op );
  101. } );
  102. describe( 'toJSON', () => {
  103. it( 'should create proper serialized object', () => {
  104. const op = new MarkerOperation( 'name', null, range, model.markers, doc.version, true );
  105. const serialized = jsonParseStringify( op );
  106. expect( serialized ).to.deep.equal( {
  107. __className: 'engine.model.operation.MarkerOperation',
  108. baseVersion: 0,
  109. name: 'name',
  110. oldRange: null,
  111. newRange: jsonParseStringify( range ),
  112. affectsData: true
  113. } );
  114. } );
  115. } );
  116. describe( 'fromJSON', () => {
  117. it( 'should create proper MarkerOperation from json object #1', () => {
  118. const op = new MarkerOperation( 'name', null, range, model.markers, doc.version, true );
  119. const serialized = jsonParseStringify( op );
  120. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  121. expect( deserialized ).to.deep.equal( op );
  122. } );
  123. it( 'should create proper MarkerOperation from json object #2', () => {
  124. // Gotta love 100% CC.
  125. const op = new MarkerOperation( 'name', range, null, model.markers, doc.version, true );
  126. const serialized = jsonParseStringify( op );
  127. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  128. expect( deserialized ).to.deep.equal( op );
  129. } );
  130. } );
  131. } );