markeroperation.js 5.6 KB

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