markerdelta.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 Range from '../../../src/model/range';
  7. import Text from '../../../src/model/text';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import MarkerDelta from '../../../src/model/delta/markerdelta';
  10. import MarkerOperation from '../../../src/model/operation/markeroperation';
  11. describe( 'Batch', () => {
  12. let doc, root, range;
  13. beforeEach( () => {
  14. doc = new Document();
  15. root = doc.createRoot();
  16. root.appendChildren( new Text( 'foo' ) );
  17. range = Range.createIn( root );
  18. } );
  19. describe( 'setMarker', () => {
  20. it( 'should add marker to the document marker collection', () => {
  21. doc.batch().setMarker( 'name', range );
  22. expect( doc.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  23. } );
  24. it( 'should update marker in the document marker collection', () => {
  25. doc.batch().setMarker( 'name', range );
  26. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  27. doc.batch().setMarker( 'name', range2 );
  28. expect( doc.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  29. } );
  30. it( 'should accept marker instance', () => {
  31. doc.batch().setMarker( 'name', range );
  32. const marker = doc.markers.get( 'name' );
  33. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  34. const batch = doc.batch().setMarker( marker, range2 );
  35. const op = batch.deltas[ 0 ].operations[ 0 ];
  36. expect( doc.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  37. expect( op.oldRange.isEqual( range ) ).to.be.true;
  38. expect( op.newRange.isEqual( range2 ) ).to.be.true;
  39. } );
  40. it( 'should accept empty range parameter if marker instance is passed', () => {
  41. const marker = doc.markers.set( 'name', range );
  42. sinon.spy( doc, 'fire' );
  43. doc.on( 'change', ( evt, type, changes ) => {
  44. if ( type == 'marker' ) {
  45. expect( changes.type ).to.equal( 'set' );
  46. expect( changes.name ).to.equal( 'name' );
  47. }
  48. } );
  49. const batch = doc.batch().setMarker( marker );
  50. const op = batch.deltas[ 0 ].operations[ 0 ];
  51. expect( doc.fire.calledWith( 'change', 'marker' ) ).to.be.true;
  52. expect( op.oldRange ).to.be.null;
  53. expect( op.newRange.isEqual( range ) ).to.be.true;
  54. } );
  55. it( 'should throw if marker with given name does not exist and range is not passed', () => {
  56. expect( () => {
  57. doc.batch().setMarker( 'name' );
  58. } ).to.throw( CKEditorError, /^batch-setMarker-no-range/ );
  59. } );
  60. } );
  61. describe( 'removeMarker', () => {
  62. it( 'should remove marker from the document marker collection', () => {
  63. doc.batch().setMarker( 'name', range );
  64. doc.batch().removeMarker( 'name' );
  65. expect( doc.markers.get( 'name' ) ).to.be.null;
  66. } );
  67. it( 'should throw when trying to remove non existing marker', () => {
  68. expect( () => {
  69. doc.batch().removeMarker( 'name' );
  70. } ).to.throw( CKEditorError, /^batch-removeMarker-no-marker/ );
  71. } );
  72. it( 'should accept marker instance', () => {
  73. doc.batch().setMarker( 'name', range );
  74. const marker = doc.markers.get( 'name' );
  75. doc.batch().removeMarker( marker );
  76. expect( doc.markers.get( 'name' ) ).to.be.null;
  77. } );
  78. } );
  79. it( 'should be chainable', () => {
  80. const batch = doc.batch();
  81. const chain = batch.setMarker( 'name', range );
  82. expect( chain ).to.equal( batch );
  83. } );
  84. it( 'should add delta to batch and operation to delta before applying operation', () => {
  85. sinon.spy( doc, 'applyOperation' );
  86. const batch = doc.batch().setMarker( 'name', range );
  87. const correctDeltaMatcher = sinon.match( operation => {
  88. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  89. } );
  90. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  91. } );
  92. } );
  93. describe( 'MarkerDelta', () => {
  94. let markerDelta, doc, root, range;
  95. beforeEach( () => {
  96. doc = new Document();
  97. root = doc.createRoot();
  98. range = Range.createIn( root );
  99. markerDelta = new MarkerDelta();
  100. } );
  101. describe( 'constructor()', () => {
  102. it( 'should create merge delta with no operations added', () => {
  103. expect( markerDelta.operations.length ).to.equal( 0 );
  104. } );
  105. } );
  106. describe( 'type', () => {
  107. it( 'should be equal to marker', () => {
  108. expect( markerDelta.type ).to.equal( 'marker' );
  109. } );
  110. } );
  111. describe( 'getReversed', () => {
  112. it( 'should return correct MarkerDelta', () => {
  113. markerDelta.addOperation( new MarkerOperation( 'name', null, range, 0 ) );
  114. const reversed = markerDelta.getReversed();
  115. expect( reversed ).to.be.instanceof( MarkerDelta );
  116. expect( reversed.operations.length ).to.equal( 1 );
  117. const op = reversed.operations[ 0 ];
  118. expect( op ).to.be.an.instanceof( MarkerOperation );
  119. expect( op.oldRange.isEqual( range ) ).to.be.true;
  120. expect( op.newRange ).to.be.null;
  121. } );
  122. } );
  123. it( 'should provide proper className', () => {
  124. expect( MarkerDelta.className ).to.equal( 'engine.model.delta.MarkerDelta' );
  125. } );
  126. } );