markercollection.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  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. it( 'should throw if marker name with "," is added', () => {
  94. expectToThrowCKEditorError( () => {
  95. markers._set( 'foo,bar', range );
  96. }, /^markercollection-incorrect-marker-name:/, markers );
  97. } );
  98. } );
  99. describe( 'has', () => {
  100. it( 'should return false if marker with given name is not in the collection', () => {
  101. expect( markers.has( 'name' ) ).to.be.false;
  102. } );
  103. it( 'should return true if marker with given name is in the collection', () => {
  104. markers._set( 'name', range );
  105. expect( markers.has( 'name' ) ).to.be.true;
  106. } );
  107. } );
  108. describe( 'get', () => {
  109. it( 'should return null if marker with given name has not been found', () => {
  110. expect( markers.get( 'name' ) ).to.be.null;
  111. } );
  112. it( 'should always return same instance of marker', () => {
  113. expect( markers.get( 'name' ) ).to.equal( markers.get( 'name' ) );
  114. } );
  115. } );
  116. describe( '_remove', () => {
  117. it( 'should remove marker, return true and fire update:<markerName> event', () => {
  118. const marker = markers._set( 'name', range );
  119. sinon.spy( markers, 'fire' );
  120. const result = markers._remove( 'name' );
  121. expect( result ).to.be.true;
  122. expect( markers.get( 'name' ) ).to.be.null;
  123. sinon.assert.calledWithExactly( markers.fire, 'update:name', marker, range, null );
  124. } );
  125. it( 'should destroy marker instance', () => {
  126. const marker = markers._set( 'name', range );
  127. sinon.spy( marker, 'stopListening' );
  128. sinon.spy( marker, '_detachLiveRange' );
  129. markers._remove( 'name' );
  130. expect( marker.stopListening.calledOnce ).to.be.true;
  131. expect( marker._detachLiveRange.calledOnce ).to.be.true;
  132. } );
  133. it( 'should return false if name has not been found in collection', () => {
  134. markers._set( 'name', range );
  135. sinon.spy( markers, 'fire' );
  136. const result = markers._remove( 'other' );
  137. expect( result ).to.be.false;
  138. expect( markers.fire.notCalled ).to.be.true;
  139. } );
  140. it( 'should accept marker instance instead of name', () => {
  141. const marker = markers._set( 'name', range );
  142. sinon.spy( markers, 'fire' );
  143. const result = markers._remove( marker );
  144. expect( result ).to.be.true;
  145. expect( markers.fire.calledWithExactly( 'update:name', marker, range, null ) ).to.be.true;
  146. expect( markers.get( 'name' ) ).to.be.null;
  147. } );
  148. } );
  149. describe( '_refresh()', () => {
  150. it( 'should fire update:<markerName> event', () => {
  151. const marker = markers._set( 'name', range );
  152. sinon.spy( markers, 'fire' );
  153. markers._refresh( 'name' );
  154. sinon.assert.calledWithExactly( markers.fire, 'update:name', marker, range, range, false, false );
  155. } );
  156. it( 'should throw if marker does not exist', () => {
  157. expectToThrowCKEditorError( () => {
  158. markers._refresh( 'name' );
  159. }, /^markercollection-refresh-marker-not-exists:/, markers );
  160. } );
  161. } );
  162. describe( 'getMarkersGroup', () => {
  163. it( 'returns all markers which names start on given prefix', () => {
  164. const markerFooA = markers._set( 'foo:a', range );
  165. const markerFooB = markers._set( 'foo:b', range );
  166. markers._set( 'bar:a', range );
  167. markers._set( 'foobar:a', range );
  168. expect( Array.from( markers.getMarkersGroup( 'foo' ) ) ).to.deep.equal( [ markerFooA, markerFooB ] );
  169. expect( Array.from( markers.getMarkersGroup( 'a' ) ) ).to.deep.equal( [] );
  170. } );
  171. } );
  172. describe( 'getMarkersAtPosition', () => {
  173. it( 'should return iterator iterating over all markers that contains given position', () => {
  174. markers._set( 'a', range );
  175. const markerB = markers._set( 'b', range2 );
  176. const result = Array.from( markers.getMarkersAtPosition( Position._createAt( root, 1 ) ) );
  177. expect( result ).to.deep.equal( [ markerB ] );
  178. } );
  179. } );
  180. describe( 'destroy', () => {
  181. it( 'should make MarkerCollection stop listening to all events and destroy all markers', () => {
  182. const markerA = markers._set( 'a', range );
  183. const markerB = markers._set( 'b', range2 );
  184. sinon.spy( markers, 'stopListening' );
  185. sinon.spy( markerA, 'stopListening' );
  186. sinon.spy( markerB, 'stopListening' );
  187. markers.destroy();
  188. expect( markers.stopListening.calledWithExactly() ).to.be.true;
  189. expect( markerA.stopListening.calledWithExactly() ).to.be.true;
  190. expect( markerB.stopListening.calledWithExactly() ).to.be.true;
  191. expect( markerA._liveRange ).to.be.null;
  192. expect( markerB._liveRange ).to.be.null;
  193. } );
  194. } );
  195. } );
  196. describe( 'Marker', () => {
  197. let model, doc, root;
  198. beforeEach( () => {
  199. model = new Model();
  200. doc = model.document;
  201. root = doc.createRoot();
  202. } );
  203. it( 'should provide API that returns up-to-date marker range parameters', () => {
  204. root._appendChild( new Text( 'foo' ) );
  205. const range = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  206. const marker = model.markers._set( 'name', range );
  207. expect( marker.getRange().isEqual( range ) ).to.be.true;
  208. expect( marker.getStart().isEqual( range.start ) ).to.be.true;
  209. expect( marker.getEnd().isEqual( range.end ) ).to.be.true;
  210. model.change( writer => {
  211. writer.insertText( 'abc', root );
  212. } );
  213. const updatedRange = new Range( Position._createAt( root, 4 ), Position._createAt( root, 5 ) );
  214. expect( marker.getRange().isEqual( updatedRange ) ).to.be.true;
  215. expect( marker.getStart().isEqual( updatedRange.start ) ).to.be.true;
  216. expect( marker.getEnd().isEqual( updatedRange.end ) ).to.be.true;
  217. } );
  218. it( 'should throw when using the API if marker was removed from markers collection', () => {
  219. const range = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  220. const marker = model.markers._set( 'name', range );
  221. model.markers._remove( 'name' );
  222. expectToThrowCKEditorError( () => {
  223. marker.getRange();
  224. }, /^marker-destroyed/ );
  225. expectToThrowCKEditorError( () => {
  226. marker.getStart();
  227. }, /^marker-destroyed/ );
  228. expectToThrowCKEditorError( () => {
  229. marker.getEnd();
  230. }, /^marker-destroyed/ );
  231. expectToThrowCKEditorError( () => {
  232. marker.managedUsingOperations;
  233. }, /^marker-destroyed/ );
  234. expectToThrowCKEditorError( () => {
  235. marker.affectsData;
  236. }, /^marker-destroyed/ );
  237. } );
  238. it( 'should attach live range to marker', () => {
  239. const range = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  240. const marker = model.markers._set( 'name', range );
  241. const eventRange = sinon.spy();
  242. const eventContent = sinon.spy();
  243. marker.on( 'change:range', eventRange );
  244. marker.on( 'change:content', eventContent );
  245. marker._liveRange.fire( 'change:range', null, {} );
  246. marker._liveRange.fire( 'change:content', null, {} );
  247. expect( eventRange.calledOnce ).to.be.true;
  248. expect( eventContent.calledOnce ).to.be.true;
  249. } );
  250. it( 'should detach live range from marker', () => {
  251. const range = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  252. const marker = model.markers._set( 'name', range );
  253. const liveRange = marker._liveRange;
  254. const eventRange = sinon.spy();
  255. const eventContent = sinon.spy();
  256. sinon.spy( liveRange, 'detach' );
  257. marker.on( 'change:range', eventRange );
  258. marker.on( 'change:content', eventContent );
  259. marker._detachLiveRange();
  260. liveRange.fire( 'change:range', null, {} );
  261. liveRange.fire( 'change:content', null, {} );
  262. expect( eventRange.notCalled ).to.be.true;
  263. expect( eventContent.notCalled ).to.be.true;
  264. expect( liveRange.detach.calledOnce ).to.be.true;
  265. } );
  266. it( 'should reattach live range to marker', () => {
  267. const range = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  268. const marker = model.markers._set( 'name', range );
  269. const oldLiveRange = marker._liveRange;
  270. const newLiveRange = new LiveRange( Position._createAt( root, 0 ), Position._createAt( root, 1 ) );
  271. const eventRange = sinon.spy();
  272. const eventContent = sinon.spy();
  273. sinon.spy( oldLiveRange, 'detach' );
  274. marker.on( 'change:range', eventRange );
  275. marker.on( 'change:content', eventContent );
  276. marker._attachLiveRange( newLiveRange );
  277. oldLiveRange.fire( 'change:range', null, {} );
  278. oldLiveRange.fire( 'change:content', null, {} );
  279. expect( eventRange.notCalled ).to.be.true;
  280. expect( eventContent.notCalled ).to.be.true;
  281. expect( oldLiveRange.detach.calledOnce ).to.be.true;
  282. newLiveRange.fire( 'change:range', null, {} );
  283. newLiveRange.fire( 'change:content', null, {} );
  284. expect( eventRange.calledOnce ).to.be.true;
  285. expect( eventContent.calledOnce ).to.be.true;
  286. } );
  287. it( 'should change managedUsingOperations flag', () => {
  288. const range = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  289. const marker = model.markers._set( 'name', range, false );
  290. expect( marker.managedUsingOperations ).to.be.false;
  291. model.markers._set( 'name', range, true );
  292. expect( marker.managedUsingOperations ).to.be.true;
  293. model.markers._set( 'name', range, false );
  294. expect( marker.managedUsingOperations ).to.be.false;
  295. } );
  296. it( 'should change affectsData flag', () => {
  297. const range = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  298. const marker = model.markers._set( 'name', range, false, false );
  299. expect( marker.affectsData ).to.be.false;
  300. model.markers._set( 'name', range, false, true );
  301. expect( marker.affectsData ).to.be.true;
  302. model.markers._set( 'name', range, false, false );
  303. expect( marker.affectsData ).to.be.false;
  304. } );
  305. describe( 'is()', () => {
  306. let marker;
  307. beforeEach( () => {
  308. const range = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  309. marker = model.markers._set( 'name', range );
  310. } );
  311. it( 'should return true for "marker"', () => {
  312. expect( marker.is( 'marker' ) ).to.be.true;
  313. expect( marker.is( 'model:marker' ) ).to.be.true;
  314. } );
  315. it( 'should return false for incorrect values', () => {
  316. expect( marker.is( 'model' ) ).to.be.false;
  317. expect( marker.is( 'model:node' ) ).to.be.false;
  318. expect( marker.is( '$text' ) ).to.be.false;
  319. expect( marker.is( 'element', 'paragraph' ) ).to.be.false;
  320. } );
  321. } );
  322. } );