markercollection.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 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', marker ) ).to.be.true;
  41. } );
  42. it( 'should fire remove event, and create a new marker if marker with given name was in the collection', () => {
  43. markers.set( 'name', range );
  44. const marker1 = markers.get( 'name' );
  45. sinon.spy( markers, 'fire' );
  46. const result = markers.set( 'name', range2 );
  47. const marker2 = markers.get( 'name' );
  48. expect( result ).to.equal( marker2 );
  49. expect( markers.fire.calledWithExactly( 'remove', marker1 ) ).to.be.true;
  50. expect( markers.fire.calledWithExactly( 'add', marker2 ) ).to.be.true;
  51. expect( marker2.name ).to.equal( 'name' );
  52. expect( marker2.getRange().isEqual( range2 ) ).to.be.true;
  53. expect( marker1 ).not.to.equal( marker2 );
  54. } );
  55. it( 'should accept marker instance instead of name', () => {
  56. markers.set( 'name', range );
  57. const marker1 = markers.get( 'name' );
  58. const result = markers.set( marker1, range2 );
  59. const marker2 = markers.get( 'name' );
  60. expect( result ).to.equal( marker2 );
  61. expect( marker2.getRange().isEqual( range2 ) );
  62. expect( marker1 ).not.to.equal( marker2 );
  63. } );
  64. } );
  65. describe( 'has', () => {
  66. it( 'should return false if marker with given name is not in the collection', () => {
  67. expect( markers.has( 'name' ) ).to.be.false;
  68. } );
  69. it( 'should return true if marker with given name is in the collection', () => {
  70. markers.set( 'name', range );
  71. expect( markers.has( 'name' ) ).to.be.true;
  72. } );
  73. } );
  74. describe( 'get', () => {
  75. it( 'should return null if marker with given name has not been found', () => {
  76. expect( markers.get( 'name' ) ).to.be.null;
  77. } );
  78. it( 'should always return same instance of marker', () => {
  79. expect( markers.get( 'name' ) ).to.equal( markers.get( 'name' ) );
  80. } );
  81. } );
  82. describe( 'remove', () => {
  83. it( 'should remove marker, return true and fire remove event', () => {
  84. const marker = markers.set( 'name', range );
  85. sinon.spy( markers, 'fire' );
  86. const result = markers.remove( 'name' );
  87. expect( result ).to.be.true;
  88. expect( markers.fire.calledWithExactly( 'remove', marker ) ).to.be.true;
  89. expect( markers.get( 'name' ) ).to.be.null;
  90. } );
  91. it( 'should destroy marker instance', () => {
  92. const marker = markers.set( 'name', range );
  93. const liveRange = marker._liveRange;
  94. sinon.spy( marker, 'stopListening' );
  95. sinon.spy( liveRange, 'detach' );
  96. markers.remove( 'name' );
  97. expect( marker.stopListening.calledOnce ).to.be.true;
  98. expect( marker._liveRange ).to.be.null;
  99. expect( liveRange.detach.calledOnce ).to.be.true;
  100. } );
  101. it( 'should return false if name has not been found in collection', () => {
  102. markers.set( 'name', range );
  103. sinon.spy( markers, 'fire' );
  104. const result = markers.remove( 'other' );
  105. expect( result ).to.be.false;
  106. expect( markers.fire.notCalled ).to.be.true;
  107. } );
  108. it( 'should accept marker instance instead of name', () => {
  109. const marker = markers.set( 'name', range );
  110. sinon.spy( markers, 'fire' );
  111. const result = markers.remove( marker );
  112. expect( result ).to.be.true;
  113. expect( markers.fire.calledWithExactly( 'remove', marker ) ).to.be.true;
  114. expect( markers.get( 'name' ) ).to.be.null;
  115. } );
  116. } );
  117. describe( 'getMarkersGroup', () => {
  118. it( 'returns all markers which names start on given prefix', () => {
  119. const markerFooA = markers.set( 'foo:a', range );
  120. const markerFooB = markers.set( 'foo:b', range );
  121. markers.set( 'bar:a', range );
  122. markers.set( 'foobar:a', range );
  123. expect( Array.from( markers.getMarkersGroup( 'foo' ) ) ).to.deep.equal( [ markerFooA, markerFooB ] );
  124. expect( Array.from( markers.getMarkersGroup( 'a' ) ) ).to.deep.equal( [] );
  125. } );
  126. } );
  127. describe( 'getMarkersAtPosition', () => {
  128. it( 'should return iterator iterating over all markers that contains given position', () => {
  129. markers.set( 'a', range );
  130. const markerB = markers.set( 'b', range2 );
  131. const result = Array.from( markers.getMarkersAtPosition( Position.createAt( root, 1 ) ) );
  132. expect( result ).to.deep.equal( [ markerB ] );
  133. } );
  134. } );
  135. describe( 'destroy', () => {
  136. it( 'should make MarkerCollection stop listening to all events and destroy all markers', () => {
  137. const markerA = markers.set( 'a', range );
  138. const markerB = markers.set( 'b', range2 );
  139. sinon.spy( markers, 'stopListening' );
  140. sinon.spy( markerA, 'stopListening' );
  141. sinon.spy( markerB, 'stopListening' );
  142. markers.destroy();
  143. expect( markers.stopListening.calledWithExactly() ).to.be.true;
  144. expect( markerA.stopListening.calledWithExactly() ).to.be.true;
  145. expect( markerB.stopListening.calledWithExactly() ).to.be.true;
  146. expect( markerA._liveRange ).to.be.null;
  147. expect( markerB._liveRange ).to.be.null;
  148. } );
  149. } );
  150. } );
  151. describe( 'Marker', () => {
  152. let doc, root;
  153. beforeEach( () => {
  154. doc = new Document();
  155. root = doc.createRoot();
  156. } );
  157. it( 'should provide API that returns up-to-date marker range parameters', () => {
  158. root.appendChildren( new Text( 'foo' ) );
  159. const range = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  160. const marker = doc.markers.set( 'name', range );
  161. expect( marker.getRange().isEqual( range ) ).to.be.true;
  162. expect( marker.getStart().isEqual( range.start ) ).to.be.true;
  163. expect( marker.getEnd().isEqual( range.end ) ).to.be.true;
  164. doc.enqueueChanges( () => {
  165. doc.batch().insert( Position.createAt( root, 0 ), 'abc' );
  166. } );
  167. const updatedRange = Range.createFromParentsAndOffsets( root, 4, root, 5 );
  168. expect( marker.getRange().isEqual( updatedRange ) ).to.be.true;
  169. expect( marker.getStart().isEqual( updatedRange.start ) ).to.be.true;
  170. expect( marker.getEnd().isEqual( updatedRange.end ) ).to.be.true;
  171. } );
  172. it( 'should throw when using the API if marker was removed from markers collection', () => {
  173. const range = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  174. const marker = doc.markers.set( 'name', range );
  175. doc.markers.remove( 'name' );
  176. expect( () => {
  177. marker.getRange();
  178. } ).to.throw( CKEditorError, /^marker-destroyed/ );
  179. expect( () => {
  180. marker.getStart();
  181. } ).to.throw( CKEditorError, /^marker-destroyed/ );
  182. expect( () => {
  183. marker.getEnd();
  184. } ).to.throw( CKEditorError, /^marker-destroyed/ );
  185. } );
  186. } );