markercollection.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 LiveRange from '../../src/model/liverange';
  9. import Text from '../../src/model/text';
  10. import Model from '../../src/model/model';
  11. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. describe( 'MarkerCollection', () => {
  13. let markers, range, range2, doc, root;
  14. beforeEach( () => {
  15. const model = new Model();
  16. doc = model.document;
  17. markers = new MarkerCollection();
  18. root = doc.createRoot();
  19. range = Range.createFromParentsAndOffsets( root, 0, root, 1 );
  20. range2 = Range.createFromParentsAndOffsets( root, 0, root, 2 );
  21. } );
  22. describe( 'iterator', () => {
  23. it( 'should return markers added to the marker collection', () => {
  24. markers._set( 'a', range );
  25. markers._set( 'b', range );
  26. const markerA = markers.get( 'a' );
  27. const markerB = markers.get( 'b' );
  28. const markersArray = Array.from( markers );
  29. expect( markersArray.includes( markerA ) ).to.be.true;
  30. expect( markersArray.includes( markerB ) ).to.be.true;
  31. expect( markersArray.length ).to.equal( 2 );
  32. } );
  33. } );
  34. describe( '_set', () => {
  35. it( 'should create a marker and fire update:<markerName>', () => {
  36. sinon.spy( markers, 'fire' );
  37. const result = markers._set( 'name', range );
  38. const marker = markers.get( 'name' );
  39. expect( result ).to.equal( marker );
  40. expect( marker.name ).to.equal( 'name' );
  41. expect( marker.managedUsingOperations ).to.false;
  42. expect( marker.getRange().isEqual( range ) ).to.be.true;
  43. sinon.assert.calledWithExactly( markers.fire, 'update:name', result, null, range );
  44. } );
  45. it( 'should create a marker marked as managed by operations', () => {
  46. const marker = markers._set( 'name', range, true );
  47. expect( marker.managedUsingOperations ).to.true;
  48. } );
  49. it( 'should update marker range and fire update:<markerName> event if marker with given name was in the collection', () => {
  50. const marker = markers._set( 'name', range );
  51. sinon.spy( markers, 'fire' );
  52. sinon.spy( marker, '_detachLiveRange' );
  53. sinon.spy( marker, '_attachLiveRange' );
  54. const result = markers._set( 'name', range2 );
  55. expect( result ).to.equal( marker );
  56. expect( marker.getRange().isEqual( range2 ) ).to.be.true;
  57. sinon.assert.calledWithExactly( markers.fire, 'update:name', marker, range, range2 );
  58. sinon.assert.calledOnce( marker._detachLiveRange );
  59. sinon.assert.calledOnce( marker._detachLiveRange );
  60. } );
  61. it( 'should update marker#managedUsingOperations and fire update:<markerName> event if marker with given name ' +
  62. 'was in the collection',
  63. () => {
  64. const marker = markers._set( 'name', range );
  65. sinon.spy( markers, 'fire' );
  66. sinon.spy( marker, '_detachLiveRange' );
  67. sinon.spy( marker, '_attachLiveRange' );
  68. const result = markers._set( 'name', range, true );
  69. expect( result ).to.equal( marker );
  70. expect( marker.managedUsingOperations ).to.true;
  71. expect( marker.getRange().isEqual( range ) ).to.be.true;
  72. sinon.assert.calledWithExactly( markers.fire, 'update:name', marker, range, range );
  73. sinon.assert.notCalled( marker._detachLiveRange );
  74. sinon.assert.notCalled( marker._attachLiveRange );
  75. } );
  76. it( 'should not fire event if given marker has not changed', () => {
  77. const marker = markers._set( 'name', range );
  78. sinon.spy( markers, 'fire' );
  79. const result = markers._set( 'name', range );
  80. expect( marker ).to.equal( result );
  81. sinon.assert.notCalled( markers.fire );
  82. } );
  83. it( 'should accept marker instance instead of name', () => {
  84. const marker = markers._set( 'name', range );
  85. markers._set( marker, range2 );
  86. expect( marker.getRange().isEqual( range2 ) ).to.true;
  87. } );
  88. } );
  89. describe( 'has', () => {
  90. it( 'should return false if marker with given name is not in the collection', () => {
  91. expect( markers.has( 'name' ) ).to.be.false;
  92. } );
  93. it( 'should return true if marker with given name is in the collection', () => {
  94. markers._set( 'name', range );
  95. expect( markers.has( 'name' ) ).to.be.true;
  96. } );
  97. } );
  98. describe( 'get', () => {
  99. it( 'should return null if marker with given name has not been found', () => {
  100. expect( markers.get( 'name' ) ).to.be.null;
  101. } );
  102. it( 'should always return same instance of marker', () => {
  103. expect( markers.get( 'name' ) ).to.equal( markers.get( 'name' ) );
  104. } );
  105. } );
  106. describe( '_remove', () => {
  107. it( 'should remove marker, return true and fire update:<markerName> event', () => {
  108. const marker = markers._set( 'name', range );
  109. sinon.spy( markers, 'fire' );
  110. const result = markers._remove( 'name' );
  111. expect( result ).to.be.true;
  112. expect( markers.get( 'name' ) ).to.be.null;
  113. sinon.assert.calledWithExactly( markers.fire, 'update:name', marker, range, null );
  114. } );
  115. it( 'should destroy marker instance', () => {
  116. const marker = markers._set( 'name', range );
  117. sinon.spy( marker, 'stopListening' );
  118. sinon.spy( marker, '_detachLiveRange' );
  119. markers._remove( 'name' );
  120. expect( marker.stopListening.calledOnce ).to.be.true;
  121. expect( marker._detachLiveRange.calledOnce ).to.be.true;
  122. } );
  123. it( 'should return false if name has not been found in collection', () => {
  124. markers._set( 'name', range );
  125. sinon.spy( markers, 'fire' );
  126. const result = markers._remove( 'other' );
  127. expect( result ).to.be.false;
  128. expect( markers.fire.notCalled ).to.be.true;
  129. } );
  130. it( 'should accept marker instance instead of name', () => {
  131. const marker = markers._set( 'name', range );
  132. sinon.spy( markers, 'fire' );
  133. const result = markers._remove( marker );
  134. expect( result ).to.be.true;
  135. expect( markers.fire.calledWithExactly( 'update:name', marker, range, null ) ).to.be.true;
  136. expect( markers.get( 'name' ) ).to.be.null;
  137. } );
  138. } );
  139. describe( 'getMarkersGroup', () => {
  140. it( 'returns all markers which names start on given prefix', () => {
  141. const markerFooA = markers._set( 'foo:a', range );
  142. const markerFooB = markers._set( 'foo:b', range );
  143. markers._set( 'bar:a', range );
  144. markers._set( 'foobar:a', range );
  145. expect( Array.from( markers.getMarkersGroup( 'foo' ) ) ).to.deep.equal( [ markerFooA, markerFooB ] );
  146. expect( Array.from( markers.getMarkersGroup( 'a' ) ) ).to.deep.equal( [] );
  147. } );
  148. } );
  149. describe( 'getMarkersAtPosition', () => {
  150. it( 'should return iterator iterating over all markers that contains given position', () => {
  151. markers._set( 'a', range );
  152. const markerB = markers._set( 'b', range2 );
  153. const result = Array.from( markers.getMarkersAtPosition( Position.createAt( root, 1 ) ) );
  154. expect( result ).to.deep.equal( [ markerB ] );
  155. } );
  156. } );
  157. describe( 'destroy', () => {
  158. it( 'should make MarkerCollection stop listening to all events and destroy all markers', () => {
  159. const markerA = markers._set( 'a', range );
  160. const markerB = markers._set( 'b', range2 );
  161. sinon.spy( markers, 'stopListening' );
  162. sinon.spy( markerA, 'stopListening' );
  163. sinon.spy( markerB, 'stopListening' );
  164. markers.destroy();
  165. expect( markers.stopListening.calledWithExactly() ).to.be.true;
  166. expect( markerA.stopListening.calledWithExactly() ).to.be.true;
  167. expect( markerB.stopListening.calledWithExactly() ).to.be.true;
  168. expect( markerA._liveRange ).to.be.null;
  169. expect( markerB._liveRange ).to.be.null;
  170. } );
  171. } );
  172. } );
  173. describe( 'Marker', () => {
  174. let model, doc, root;
  175. beforeEach( () => {
  176. model = new Model();
  177. doc = model.document;
  178. root = doc.createRoot();
  179. } );
  180. it( 'should provide API that returns up-to-date marker range parameters', () => {
  181. root.appendChildren( new Text( 'foo' ) );
  182. const range = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  183. const marker = model.markers._set( 'name', range );
  184. expect( marker.getRange().isEqual( range ) ).to.be.true;
  185. expect( marker.getStart().isEqual( range.start ) ).to.be.true;
  186. expect( marker.getEnd().isEqual( range.end ) ).to.be.true;
  187. model.change( writer => {
  188. writer.insertText( 'abc', root );
  189. } );
  190. const updatedRange = Range.createFromParentsAndOffsets( root, 4, root, 5 );
  191. expect( marker.getRange().isEqual( updatedRange ) ).to.be.true;
  192. expect( marker.getStart().isEqual( updatedRange.start ) ).to.be.true;
  193. expect( marker.getEnd().isEqual( updatedRange.end ) ).to.be.true;
  194. } );
  195. it( 'should throw when using the API if marker was removed from markers collection', () => {
  196. const range = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  197. const marker = model.markers._set( 'name', range );
  198. model.markers._remove( 'name' );
  199. expect( () => {
  200. marker.getRange();
  201. } ).to.throw( CKEditorError, /^marker-destroyed/ );
  202. expect( () => {
  203. marker.getStart();
  204. } ).to.throw( CKEditorError, /^marker-destroyed/ );
  205. expect( () => {
  206. marker.getEnd();
  207. } ).to.throw( CKEditorError, /^marker-destroyed/ );
  208. expect( () => {
  209. marker.managedUsingOperations;
  210. } ).to.throw( CKEditorError, /^marker-destroyed/ );
  211. } );
  212. it( 'should attach live range to marker', () => {
  213. const range = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  214. const marker = model.markers._set( 'name', range );
  215. const eventRange = sinon.spy();
  216. const eventContent = sinon.spy();
  217. marker.on( 'change:range', eventRange );
  218. marker.on( 'change:content', eventContent );
  219. marker._liveRange.fire( 'change:range', null, {} );
  220. marker._liveRange.fire( 'change:content', null, {} );
  221. expect( eventRange.calledOnce ).to.be.true;
  222. expect( eventContent.calledOnce ).to.be.true;
  223. } );
  224. it( 'should detach live range from marker', () => {
  225. const range = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  226. const marker = model.markers._set( 'name', range );
  227. const liveRange = marker._liveRange;
  228. const eventRange = sinon.spy();
  229. const eventContent = sinon.spy();
  230. sinon.spy( liveRange, 'detach' );
  231. marker.on( 'change:range', eventRange );
  232. marker.on( 'change:content', eventContent );
  233. marker._detachLiveRange();
  234. liveRange.fire( 'change:range', null, {} );
  235. liveRange.fire( 'change:content', null, {} );
  236. expect( eventRange.notCalled ).to.be.true;
  237. expect( eventContent.notCalled ).to.be.true;
  238. expect( liveRange.detach.calledOnce ).to.true;
  239. } );
  240. it( 'should reattach live range to marker', () => {
  241. const range = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  242. const marker = model.markers._set( 'name', range );
  243. const oldLiveRange = marker._liveRange;
  244. const newLiveRange = LiveRange.createFromParentsAndOffsets( root, 0, root, 1 );
  245. const eventRange = sinon.spy();
  246. const eventContent = sinon.spy();
  247. sinon.spy( oldLiveRange, 'detach' );
  248. marker.on( 'change:range', eventRange );
  249. marker.on( 'change:content', eventContent );
  250. marker._attachLiveRange( newLiveRange );
  251. oldLiveRange.fire( 'change:range', null, {} );
  252. oldLiveRange.fire( 'change:content', null, {} );
  253. expect( eventRange.notCalled ).to.be.true;
  254. expect( eventContent.notCalled ).to.be.true;
  255. expect( oldLiveRange.detach.calledOnce ).to.true;
  256. newLiveRange.fire( 'change:range', null, {} );
  257. newLiveRange.fire( 'change:content', null, {} );
  258. expect( eventRange.calledOnce ).to.be.true;
  259. expect( eventContent.calledOnce ).to.be.true;
  260. } );
  261. it( 'should change managedUsingOperations flag', () => {
  262. const range = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  263. const marker = model.markers._set( 'name', range, false );
  264. expect( marker.managedUsingOperations ).to.false;
  265. marker._managedUsingOperations = true;
  266. expect( marker.managedUsingOperations ).to.true;
  267. marker._managedUsingOperations = false;
  268. expect( marker.managedUsingOperations ).to.false;
  269. } );
  270. } );