markercollection.js 14 KB

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