markercollection.js 13 KB

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