markeroperation.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import Text from '../../../src/model/text';
  7. import DocumentFragment from '../../../src/model/documentfragment';
  8. import Range from '../../../src/model/range';
  9. import MarkerOperation from '../../../src/model/operation/markeroperation';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import { jsonParseStringify, wrapInDelta } from '../../model/_utils/utils';
  12. function matchRange( range ) {
  13. return sinon.match( ( rangeToMatch ) => rangeToMatch.isEqual( range ) );
  14. }
  15. describe( 'MarkerOperation', () => {
  16. let doc, root, range;
  17. beforeEach( () => {
  18. doc = new Document();
  19. root = doc.createRoot();
  20. root.appendChildren( new Text( 'foo' ) );
  21. range = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  22. } );
  23. it( 'should have property type equal to "marker"', () => {
  24. const op = new MarkerOperation( 'name', null, range, 0 );
  25. expect( op.type ).to.equal( 'marker' );
  26. } );
  27. it( 'should add marker to document marker collection', () => {
  28. sinon.spy( doc.markers, 'set' );
  29. sinon.spy( doc, 'fire' );
  30. doc.on( 'change', ( evt, type, changes ) => {
  31. expect( type ).to.equal( 'marker' );
  32. expect( changes.name ).to.equal( 'name' );
  33. expect( changes.type ).to.equal( 'set' );
  34. } );
  35. doc.applyOperation( wrapInDelta(
  36. new MarkerOperation( 'name', null, range, doc.version )
  37. ) );
  38. expect( doc.version ).to.equal( 1 );
  39. expect( doc.markers.set.calledWith( 'name', matchRange( range ) ) );
  40. expect( doc.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  41. expect( doc.fire.called ).to.be.true;
  42. } );
  43. it( 'should update marker in document marker collection', () => {
  44. doc.applyOperation( wrapInDelta(
  45. new MarkerOperation( 'name', null, range, doc.version )
  46. ) );
  47. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  48. sinon.spy( doc.markers, 'set' );
  49. sinon.spy( doc, 'fire' );
  50. doc.applyOperation( wrapInDelta(
  51. new MarkerOperation( 'name', range, range2, doc.version )
  52. ) );
  53. expect( doc.version ).to.equal( 2 );
  54. expect( doc.markers.set.calledWith( 'name', matchRange( range2 ) ) );
  55. expect( doc.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  56. expect( doc.fire.called ).to.be.true;
  57. } );
  58. it( 'should remove marker from document marker collection', () => {
  59. doc.applyOperation( wrapInDelta(
  60. new MarkerOperation( 'name', null, range, doc.version )
  61. ) );
  62. sinon.spy( doc.markers, 'remove' );
  63. sinon.spy( doc, 'fire' );
  64. doc.on( 'change', ( evt, type, changes ) => {
  65. expect( type ).to.equal( 'marker' );
  66. expect( changes.name ).to.equal( 'name' );
  67. expect( changes.type ).to.equal( 'remove' );
  68. } );
  69. doc.applyOperation( wrapInDelta(
  70. new MarkerOperation( 'name', range, null, doc.version )
  71. ) );
  72. expect( doc.version ).to.equal( 2 );
  73. expect( doc.markers.remove.calledWith( 'name' ) );
  74. expect( doc.markers.get( 'name' ) ).to.be.null;
  75. expect( doc.fire.called ).to.be.true;
  76. } );
  77. it( 'should not cause change event if operation does not change marker', () => {
  78. const range = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  79. doc.markers.set( 'name', range );
  80. sinon.spy( doc, 'fire' );
  81. doc.applyOperation( wrapInDelta(
  82. new MarkerOperation( 'name', range, range, doc.version )
  83. ) );
  84. doc.applyOperation( wrapInDelta(
  85. new MarkerOperation( 'otherName', null, null, doc.version )
  86. ) );
  87. expect( doc.fire.notCalled ).to.be.true;
  88. } );
  89. it( 'should return MarkerOperation with swapped ranges as reverse operation', () => {
  90. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  91. const op1 = new MarkerOperation( 'name', null, range, doc.version );
  92. const reversed1 = op1.getReversed();
  93. const op2 = new MarkerOperation( 'name', range, range2, doc.version );
  94. const reversed2 = op2.getReversed();
  95. expect( reversed1 ).to.be.an.instanceof( MarkerOperation );
  96. expect( reversed2 ).to.be.an.instanceof( MarkerOperation );
  97. expect( reversed1.name ).to.equal( 'name' );
  98. expect( reversed1.oldRange.isEqual( range ) ).to.be.true;
  99. expect( reversed1.newRange ).to.be.null;
  100. expect( reversed1.baseVersion ).to.equal( 1 );
  101. expect( reversed2.name ).to.equal( 'name' );
  102. expect( reversed2.oldRange.isEqual( range2 ) ).to.be.true;
  103. expect( reversed2.newRange.isEqual( range ) ).to.be.true;
  104. expect( reversed2.baseVersion ).to.equal( 1 );
  105. } );
  106. it( 'should create a MarkerOperation with the same parameters when cloned', () => {
  107. const op = new MarkerOperation( 'name', null, range, 0 );
  108. const clone = op.clone();
  109. expect( clone ).to.be.an.instanceof( MarkerOperation );
  110. expect( clone ).to.deep.equal( op );
  111. } );
  112. it( 'should throw if oldRange is not in a document', () => {
  113. const docFrag = new DocumentFragment();
  114. const rangeInDocFrag = Range.createIn( docFrag );
  115. expect( () => {
  116. new MarkerOperation( 'name', rangeInDocFrag, null, 0 );
  117. } ).to.throw( CKEditorError, /^marker-operation-range-not-in-document/ );
  118. } );
  119. it( 'should throw if newRange is not in a document', () => {
  120. const docFrag = new DocumentFragment();
  121. const rangeInDocFrag = Range.createIn( docFrag );
  122. expect( () => {
  123. new MarkerOperation( 'name', null, rangeInDocFrag, 0 );
  124. } ).to.throw( CKEditorError, /^marker-operation-range-not-in-document/ );
  125. } );
  126. it( 'should throw if ranges are in different documents', () => {
  127. const document2 = new Document();
  128. const root2 = document2.createRoot();
  129. const rangeInRoot2 = Range.createIn( root2 );
  130. expect( () => {
  131. new MarkerOperation( 'name', range, rangeInRoot2, 0 );
  132. } ).to.throw( CKEditorError, /^marker-operation-ranges-in-different-documents/ );
  133. } );
  134. describe( 'toJSON', () => {
  135. it( 'should create proper serialized object', () => {
  136. const op = new MarkerOperation( 'name', null, range, doc.version );
  137. const serialized = jsonParseStringify( op );
  138. expect( serialized ).to.deep.equal( {
  139. __className: 'engine.model.operation.MarkerOperation',
  140. baseVersion: 0,
  141. name: 'name',
  142. oldRange: null,
  143. newRange: jsonParseStringify( range )
  144. } );
  145. } );
  146. } );
  147. describe( 'fromJSON', () => {
  148. it( 'should create proper MarkerOperation from json object #1', () => {
  149. const op = new MarkerOperation( 'name', null, range, doc.version );
  150. const serialized = jsonParseStringify( op );
  151. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  152. expect( deserialized ).to.deep.equal( op );
  153. } );
  154. it( 'should create proper MarkerOperation from json object #2', () => {
  155. // Gotta love 100% CC.
  156. const op = new MarkerOperation( 'name', range, null, doc.version );
  157. const serialized = jsonParseStringify( op );
  158. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  159. expect( deserialized ).to.deep.equal( op );
  160. } );
  161. } );
  162. } );