@@ -171,16 +171,21 @@ describe( 'LiveSelection', () => {
171
171
selection . addRange ( liveRange ) ;
172
172
selection . addRange ( range ) ;
173
173
174
- const ranges = selection . _ranges ;
174
+ const ranges = Array . from ( selection . _ranges ) ;
175
175
176
176
sinon . spy ( ranges [ 0 ] , 'detach' ) ;
177
177
sinon . spy ( ranges [ 1 ] , 'detach' ) ;
178
178
179
+ sinon . spy ( selection , 'stopListening' ) ;
180
+
179
181
selection . destroy ( ) ;
180
182
181
183
expect ( ranges [ 0 ] . detach . called ) . to . be . true ;
182
184
expect ( ranges [ 1 ] . detach . called ) . to . be . true ;
183
185
186
+ expect ( selection . stopListening . calledWith ( ranges [ 0 ] ) ) . to . be . true ;
187
+ expect ( selection . stopListening . calledWith ( ranges [ 1 ] ) ) . to . be . true ;
188
+
184
189
ranges [ 0 ] . detach . restore ( ) ;
185
190
ranges [ 1 ] . detach . restore ( ) ;
186
191
} ) ;
@@ -226,6 +231,8 @@ describe( 'LiveSelection', () => {
226
231
sinon . spy ( ranges [ 0 ] , 'detach' ) ;
227
232
sinon . spy ( ranges [ 1 ] , 'detach' ) ;
228
233
234
+ sinon . spy ( selection , 'stopListening' ) ;
235
+
229
236
selection . removeAllRanges ( ) ;
230
237
} ) ;
231
238
@@ -240,9 +247,12 @@ describe( 'LiveSelection', () => {
240
247
expect ( selection . focus . isEqual ( new Position ( root , [ 0 , 0 ] ) ) ) . to . be . true ;
241
248
} ) ;
242
249
243
- it ( 'should detach ranges' , ( ) => {
250
+ it ( 'should detach ranges and stop listening to removed ranges ' , ( ) => {
244
251
expect ( ranges [ 0 ] . detach . called ) . to . be . true ;
245
252
expect ( ranges [ 1 ] . detach . called ) . to . be . true ;
253
+
254
+ expect ( selection . stopListening . calledWith ( ranges [ 0 ] ) ) . to . be . true ;
255
+ expect ( selection . stopListening . calledWith ( ranges [ 1 ] ) ) . to . be . true ;
246
256
} ) ;
247
257
248
258
it ( 'should refresh attributes' , ( ) => {
@@ -261,7 +271,7 @@ describe( 'LiveSelection', () => {
261
271
} ) . to . throw ( CKEditorError , / m o d e l - s e l e c t i o n - a d d e d - n o t - r a n g e / ) ;
262
272
} ) ;
263
273
264
- it ( 'should detach removed ranges' , ( ) => {
274
+ it ( 'should detach and stop listening to removed ranges' , ( ) => {
265
275
selection . addRange ( liveRange ) ;
266
276
selection . addRange ( range ) ;
267
277
@@ -270,10 +280,15 @@ describe( 'LiveSelection', () => {
270
280
sinon . spy ( oldRanges [ 0 ] , 'detach' ) ;
271
281
sinon . spy ( oldRanges [ 1 ] , 'detach' ) ;
272
282
283
+ sinon . spy ( selection , 'stopListening' ) ;
284
+
273
285
selection . setRanges ( [ ] ) ;
274
286
275
287
expect ( oldRanges [ 0 ] . detach . called ) . to . be . true ;
276
288
expect ( oldRanges [ 1 ] . detach . called ) . to . be . true ;
289
+
290
+ expect ( selection . stopListening . calledWith ( oldRanges [ 0 ] ) ) . to . be . true ;
291
+ expect ( selection . stopListening . calledWith ( oldRanges [ 1 ] ) ) . to . be . true ;
277
292
} ) ;
278
293
279
294
it ( 'should refresh attributes' , ( ) => {
@@ -599,6 +614,26 @@ describe( 'LiveSelection', () => {
599
614
600
615
expect ( selection . getFirstPosition ( ) . path ) . to . deep . equal ( [ 0 , 6 ] ) ;
601
616
} ) ;
617
+
618
+ it ( 'detach and stop listening to a range that ended up in in graveyard' , ( ) => {
619
+ selection . collapse ( new Position ( root , [ 1 , 3 ] ) ) ;
620
+
621
+ const range = selection . _ranges [ 0 ] ;
622
+
623
+ sinon . spy ( range , 'detach' ) ;
624
+ sinon . spy ( selection , 'stopListening' ) ;
625
+
626
+ doc . applyOperation ( wrapInDelta (
627
+ new RemoveOperation (
628
+ new Position ( root , [ 1 , 2 ] ) ,
629
+ 2 ,
630
+ doc . version
631
+ )
632
+ ) ) ;
633
+
634
+ expect ( range . detach . called ) . to . be . true ;
635
+ expect ( selection . stopListening . calledWith ( range ) ) . to . be . true ;
636
+ } ) ;
602
637
} ) ;
603
638
} ) ;
604
639
0 commit comments