markercollection.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import MarkerCollection from '../../src/model/markercollection';
  6. import Position from '../../src/model/position';
  7. import Range from '../../src/model/range';
  8. import Text from '../../src/model/text';
  9. import Document from '../../src/model/document';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. describe( 'MarkerCollection', () => {
  12. let markers, range, range2, doc, root;
  13. beforeEach( () => {
  14. doc = new Document();
  15. markers = new MarkerCollection();
  16. root = doc.createRoot();
  17. range = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  18. range2 = Range.createFromParentsAndOffsets( root, 0, root, 2 );
  19. } );
  20. describe( 'iterator', () => {
  21. it( 'should return markers added to the marker collection', () => {
  22. markers.set( 'a', range );
  23. markers.set( 'b', range );
  24. const markerA = markers.get( 'a' );
  25. const markerB = markers.get( 'b' );
  26. const markersArray = Array.from( markers );
  27. expect( markersArray.includes( markerA ) ).to.be.true;
  28. expect( markersArray.includes( markerB ) ).to.be.true;
  29. expect( markersArray.length ).to.equal( 2 );
  30. } );
  31. } );
  32. describe( 'set', () => {
  33. it( 'should create a marker, fire add:<markerName> event and return true', () => {
  34. sinon.spy( markers, 'fire' );
  35. const result = markers.set( 'name', range );
  36. const marker = markers.get( 'name' );
  37. expect( result ).to.equal( marker );
  38. expect( marker.name ).to.equal( 'name' );
  39. expect( marker.getRange().isEqual( range ) ).to.be.true;
  40. expect( markers.fire.calledWithExactly( 'add:name', marker ) ).to.be.true;
  41. } );
  42. it( 'should fire remove:<markerName> event, and create a new marker if marker with given name was in the collection', () => {
  43. const marker1 = markers.set( 'name', range );
  44. sinon.spy( markers, 'fire' );
  45. const marker2 = markers.set( 'name', range2 );
  46. expect( markers.fire.calledWithExactly( 'remove:name', marker1 ) ).to.be.true;
  47. expect( markers.fire.calledWithExactly( 'add:name', marker2 ) ).to.be.true;
  48. expect( marker2.name ).to.equal( 'name' );
  49. expect( marker2.getRange().isEqual( range2 ) ).to.be.true;
  50. expect( marker1 ).not.to.equal( marker2 );
  51. } );
  52. it( 'should not fire event and return the same marker if given marker has a range equal to given range', () => {
  53. const marker1 = markers.set( 'name', range );
  54. sinon.spy( markers, 'fire' );
  55. const marker2 = markers.set( 'name', range );
  56. expect( marker1 ).to.equal( marker2 );
  57. expect( markers.fire.notCalled ).to.be.true;
  58. } );
  59. it( 'should accept marker instance instead of name', () => {
  60. markers.set( 'name', range );
  61. const marker1 = markers.get( 'name' );
  62. const result = markers.set( marker1, range2 );
  63. const marker2 = markers.get( 'name' );
  64. expect( result ).to.equal( marker2 );
  65. expect( marker2.getRange().isEqual( range2 ) );
  66. expect( marker1 ).not.to.equal( marker2 );
  67. } );
  68. } );
  69. describe( 'has', () => {
  70. it( 'should return false if marker with given name is not in the collection', () => {
  71. expect( markers.has( 'name' ) ).to.be.false;
  72. } );
  73. it( 'should return true if marker with given name is in the collection', () => {
  74. markers.set( 'name', range );
  75. expect( markers.has( 'name' ) ).to.be.true;
  76. } );
  77. } );
  78. describe( 'get', () => {
  79. it( 'should return null if marker with given name has not been found', () => {
  80. expect( markers.get( 'name' ) ).to.be.null;
  81. } );
  82. it( 'should always return same instance of marker', () => {
  83. expect( markers.get( 'name' ) ).to.equal( markers.get( 'name' ) );
  84. } );
  85. } );
  86. describe( 'remove', () => {
  87. it( 'should remove marker, return true and fire remove:<markerName> event', () => {
  88. const marker = markers.set( 'name', range );
  89. sinon.spy( markers, 'fire' );
  90. const result = markers.remove( 'name' );
  91. expect( result ).to.be.true;
  92. expect( markers.fire.calledWithExactly( 'remove:name', marker ) ).to.be.true;
  93. expect( markers.get( 'name' ) ).to.be.null;
  94. } );
  95. it( 'should destroy marker instance', () => {
  96. const marker = markers.set( 'name', range );
  97. const liveRange = marker._liveRange;
  98. sinon.spy( marker, 'stopListening' );
  99. sinon.spy( liveRange, 'detach' );
  100. markers.remove( 'name' );
  101. expect( marker.stopListening.calledOnce ).to.be.true;
  102. expect( marker._liveRange ).to.be.null;
  103. expect( liveRange.detach.calledOnce ).to.be.true;
  104. } );
  105. it( 'should return false if name has not been found in collection', () => {
  106. markers.set( 'name', range );
  107. sinon.spy( markers, 'fire' );
  108. const result = markers.remove( 'other' );
  109. expect( result ).to.be.false;
  110. expect( markers.fire.notCalled ).to.be.true;
  111. } );
  112. it( 'should accept marker instance instead of name', () => {
  113. const marker = markers.set( 'name', range );
  114. sinon.spy( markers, 'fire' );
  115. const result = markers.remove( marker );
  116. expect( result ).to.be.true;
  117. expect( markers.fire.calledWithExactly( 'remove:name', marker ) ).to.be.true;
  118. expect( markers.get( 'name' ) ).to.be.null;
  119. } );
  120. } );
  121. describe( 'getMarkersGroup', () => {
  122. it( 'returns all markers which names start on given prefix', () => {
  123. const markerFooA = markers.set( 'foo:a', range );
  124. const markerFooB = markers.set( 'foo:b', range );
  125. markers.set( 'bar:a', range );
  126. markers.set( 'foobar:a', range );
  127. expect( Array.from( markers.getMarkersGroup( 'foo' ) ) ).to.deep.equal( [ markerFooA, markerFooB ] );
  128. expect( Array.from( markers.getMarkersGroup( 'a' ) ) ).to.deep.equal( [] );
  129. } );
  130. } );
  131. describe( 'getMarkersAtPosition', () => {
  132. it( 'should return iterator iterating over all markers that contains given position', () => {
  133. markers.set( 'a', range );
  134. const markerB = markers.set( 'b', range2 );
  135. const result = Array.from( markers.getMarkersAtPosition( Position.createAt( root, 1 ) ) );
  136. expect( result ).to.deep.equal( [ markerB ] );
  137. } );
  138. } );
  139. describe( 'destroy', () => {
  140. it( 'should make MarkerCollection stop listening to all events and destroy all markers', () => {
  141. const markerA = markers.set( 'a', range );
  142. const markerB = markers.set( 'b', range2 );
  143. sinon.spy( markers, 'stopListening' );
  144. sinon.spy( markerA, 'stopListening' );
  145. sinon.spy( markerB, 'stopListening' );
  146. markers.destroy();
  147. expect( markers.stopListening.calledWithExactly() ).to.be.true;
  148. expect( markerA.stopListening.calledWithExactly() ).to.be.true;
  149. expect( markerB.stopListening.calledWithExactly() ).to.be.true;
  150. expect( markerA._liveRange ).to.be.null;
  151. expect( markerB._liveRange ).to.be.null;
  152. } );
  153. } );
  154. } );
  155. describe( 'Marker', () => {
  156. let doc, root;
  157. beforeEach( () => {
  158. doc = new Document();
  159. root = doc.createRoot();
  160. } );
  161. it( 'should provide API that returns up-to-date marker range parameters', () => {
  162. root.appendChildren( new Text( 'foo' ) );
  163. const range = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  164. const marker = doc.markers.set( 'name', range );
  165. expect( marker.getRange().isEqual( range ) ).to.be.true;
  166. expect( marker.getStart().isEqual( range.start ) ).to.be.true;
  167. expect( marker.getEnd().isEqual( range.end ) ).to.be.true;
  168. doc.enqueueChanges( () => {
  169. doc.batch().insert( Position.createAt( root, 0 ), 'abc' );
  170. } );
  171. const updatedRange = Range.createFromParentsAndOffsets( root, 4, root, 5 );
  172. expect( marker.getRange().isEqual( updatedRange ) ).to.be.true;
  173. expect( marker.getStart().isEqual( updatedRange.start ) ).to.be.true;
  174. expect( marker.getEnd().isEqual( updatedRange.end ) ).to.be.true;
  175. } );
  176. it( 'should throw when using the API if marker was removed from markers collection', () => {
  177. const range = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  178. const marker = doc.markers.set( 'name', range );
  179. doc.markers.remove( 'name' );
  180. expect( () => {
  181. marker.getRange();
  182. } ).to.throw( CKEditorError, /^marker-destroyed/ );
  183. expect( () => {
  184. marker.getStart();
  185. } ).to.throw( CKEditorError, /^marker-destroyed/ );
  186. expect( () => {
  187. marker.getEnd();
  188. } ).to.throw( CKEditorError, /^marker-destroyed/ );
  189. } );
  190. } );