markeroperation.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 fire document change event but not document markers remove event if oldRange and newRange are null', () => {
  78. sinon.spy( doc, 'fire' );
  79. sinon.spy( doc.markers, 'fire' );
  80. doc.on( 'change', ( evt, type, changes ) => {
  81. expect( type ).to.equal( 'marker' );
  82. expect( changes.name ).to.equal( 'name' );
  83. expect( changes.type ).to.equal( 'remove' );
  84. } );
  85. doc.applyOperation( wrapInDelta(
  86. new MarkerOperation( 'name', null, null, doc.version )
  87. ) );
  88. expect( doc.fire.calledWith( 'change', 'marker' ) ).to.be.true;
  89. expect( doc.markers.fire.notCalled ).to.be.true;
  90. } );
  91. it( 'should fire document change event but not document markers remove event if oldRange and newRange are equal', () => {
  92. sinon.spy( doc, 'fire' );
  93. sinon.spy( doc.markers, 'fire' );
  94. doc.on( 'change', ( evt, type, changes ) => {
  95. expect( type ).to.equal( 'marker' );
  96. expect( changes.name ).to.equal( 'name' );
  97. expect( changes.type ).to.equal( 'set' );
  98. } );
  99. doc.applyOperation( wrapInDelta(
  100. new MarkerOperation( 'name', range, range, doc.version )
  101. ) );
  102. expect( doc.fire.calledWith( 'change', 'marker' ) ).to.be.true;
  103. expect( doc.markers.fire.notCalled ).to.be.true;
  104. } );
  105. it( 'should return MarkerOperation with swapped ranges as reverse operation', () => {
  106. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  107. const op1 = new MarkerOperation( 'name', null, range, doc.version );
  108. const reversed1 = op1.getReversed();
  109. const op2 = new MarkerOperation( 'name', range, range2, doc.version );
  110. const reversed2 = op2.getReversed();
  111. expect( reversed1 ).to.be.an.instanceof( MarkerOperation );
  112. expect( reversed2 ).to.be.an.instanceof( MarkerOperation );
  113. expect( reversed1.name ).to.equal( 'name' );
  114. expect( reversed1.oldRange.isEqual( range ) ).to.be.true;
  115. expect( reversed1.newRange ).to.be.null;
  116. expect( reversed1.baseVersion ).to.equal( 1 );
  117. expect( reversed2.name ).to.equal( 'name' );
  118. expect( reversed2.oldRange.isEqual( range2 ) ).to.be.true;
  119. expect( reversed2.newRange.isEqual( range ) ).to.be.true;
  120. expect( reversed2.baseVersion ).to.equal( 1 );
  121. } );
  122. it( 'should create a MarkerOperation with the same parameters when cloned', () => {
  123. const op = new MarkerOperation( 'name', null, range, 0 );
  124. const clone = op.clone();
  125. expect( clone ).to.be.an.instanceof( MarkerOperation );
  126. expect( clone ).to.deep.equal( op );
  127. } );
  128. it( 'should throw if oldRange is not in a document', () => {
  129. const docFrag = new DocumentFragment();
  130. const rangeInDocFrag = Range.createIn( docFrag );
  131. expect( () => {
  132. new MarkerOperation( 'name', rangeInDocFrag, null, 0 );
  133. } ).to.throw( CKEditorError, /^marker-operation-range-not-in-document/ );
  134. } );
  135. it( 'should throw if newRange is not in a document', () => {
  136. const docFrag = new DocumentFragment();
  137. const rangeInDocFrag = Range.createIn( docFrag );
  138. expect( () => {
  139. new MarkerOperation( 'name', null, rangeInDocFrag, 0 );
  140. } ).to.throw( CKEditorError, /^marker-operation-range-not-in-document/ );
  141. } );
  142. it( 'should throw if ranges are in different documents', () => {
  143. const document2 = new Document();
  144. const root2 = document2.createRoot();
  145. const rangeInRoot2 = Range.createIn( root2 );
  146. expect( () => {
  147. new MarkerOperation( 'name', range, rangeInRoot2, 0 );
  148. } ).to.throw( CKEditorError, /^marker-operation-ranges-in-different-documents/ );
  149. } );
  150. describe( 'toJSON', () => {
  151. it( 'should create proper serialized object', () => {
  152. const op = new MarkerOperation( 'name', null, range, doc.version );
  153. const serialized = jsonParseStringify( op );
  154. expect( serialized ).to.deep.equal( {
  155. __className: 'engine.model.operation.MarkerOperation',
  156. baseVersion: 0,
  157. name: 'name',
  158. oldRange: null,
  159. newRange: jsonParseStringify( range )
  160. } );
  161. } );
  162. } );
  163. describe( 'fromJSON', () => {
  164. it( 'should create proper MarkerOperation from json object #1', () => {
  165. const op = new MarkerOperation( 'name', null, range, doc.version );
  166. const serialized = jsonParseStringify( op );
  167. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  168. expect( deserialized ).to.deep.equal( op );
  169. } );
  170. it( 'should create proper MarkerOperation from json object #2', () => {
  171. // Gotta love 100% CC.
  172. const op = new MarkerOperation( 'name', range, null, doc.version );
  173. const serialized = jsonParseStringify( op );
  174. const deserialized = MarkerOperation.fromJSON( serialized, doc );
  175. expect( deserialized ).to.deep.equal( op );
  176. } );
  177. } );
  178. } );