-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathdocumentselection.js
More file actions
2418 lines (1880 loc) · 75.8 KB
/
documentselection.js
File metadata and controls
2418 lines (1880 loc) · 75.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/
import { Model } from '../../src/model/model.js';
import { Batch } from '../../src/model/batch.js';
import { ModelElement } from '../../src/model/element.js';
import { ModelText } from '../../src/model/text.js';
import { ModelRange } from '../../src/model/range.js';
import { ModelPosition } from '../../src/model/position.js';
import { ModelLiveRange } from '../../src/model/liverange.js';
import { ModelDocumentSelection } from '../../src/model/documentselection.js';
import { InsertOperation } from '../../src/model/operation/insertoperation.js';
import { MoveOperation } from '../../src/model/operation/moveoperation.js';
import { AttributeOperation } from '../../src/model/operation/attributeoperation.js';
import { SplitOperation } from '../../src/model/operation/splitoperation.js';
import { Collection, count } from '@ckeditor/ckeditor5-utils';
import { _setModelData, _getModelData } from '../../src/dev-utils/model.js';
import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils.js';
describe( 'DocumentSelection', () => {
let model, doc, root, selection, liveRange, range;
const fooStoreAttrKey = ModelDocumentSelection._getStoreAttributeKey( 'foo' );
const abcStoreAttrKey = ModelDocumentSelection._getStoreAttributeKey( 'abc' );
beforeEach( () => {
model = new Model();
doc = model.document;
root = doc.createRoot();
root._appendChild( [
new ModelElement( 'p' ),
new ModelElement( 'p' ),
new ModelElement( 'p', [], new ModelText( 'foobar' ) ),
new ModelElement( 'p' ),
new ModelElement( 'p' ),
new ModelElement( 'p' ),
new ModelElement( 'p', [], new ModelText( 'foobar' ) )
] );
selection = doc.selection;
model.schema.register( 'p', { inheritAllFrom: '$block' } );
model.schema.extend( '$text', {
allowAttributes: 'bold'
} );
model.schema.register( 'widget', {
isObject: true
} );
liveRange = new ModelLiveRange( new ModelPosition( root, [ 0 ] ), new ModelPosition( root, [ 1 ] ) );
range = new ModelRange( new ModelPosition( root, [ 2 ] ), new ModelPosition( root, [ 2, 2 ] ) );
} );
afterEach( () => {
sinon.restore();
model.destroy();
liveRange.detach();
} );
describe( 'default range', () => {
it( 'should go to the first editable element', () => {
const ranges = Array.from( selection.getRanges() );
expect( ranges.length ).to.equal( 1 );
expect( selection.anchor.isEqual( new ModelPosition( root, [ 0, 0 ] ) ) ).to.be.true;
expect( selection.focus.isEqual( new ModelPosition( root, [ 0, 0 ] ) ) ).to.be.true;
expect( selection ).to.have.property( 'isBackward', false );
} );
it( 'should be set to the beginning of the doc if there is no editable element', () => {
model = new Model();
doc = model.document;
root = doc.createRoot();
root._insertChild( 0, new ModelText( 'foobar' ) );
selection = doc.selection;
const ranges = Array.from( selection.getRanges() );
expect( ranges.length ).to.equal( 1 );
expect( selection.anchor.isEqual( new ModelPosition( root, [ 0 ] ) ) ).to.be.true;
expect( selection.focus.isEqual( new ModelPosition( root, [ 0 ] ) ) ).to.be.true;
expect( selection ).to.have.property( 'isBackward', false );
expect( count( selection.getAttributes() ) ).to.equal( 0 );
} );
it( 'should skip element when you cannot put selection', () => {
model = new Model();
doc = model.document;
root = doc.createRoot();
root._insertChild( 0, [
new ModelElement( 'img' ),
new ModelElement( 'p', [], new ModelText( 'foobar' ) )
] );
model.schema.register( 'img' );
model.schema.register( 'p', { inheritAllFrom: '$block' } );
selection = doc.selection;
const ranges = Array.from( selection.getRanges() );
expect( ranges.length ).to.equal( 1 );
expect( selection.anchor.isEqual( new ModelPosition( root, [ 1, 0 ] ) ) ).to.be.true;
expect( selection.focus.isEqual( new ModelPosition( root, [ 1, 0 ] ) ) ).to.be.true;
expect( selection ).to.have.property( 'isBackward', false );
expect( count( selection.getAttributes() ) ).to.equal( 0 );
} );
} );
describe( 'isCollapsed', () => {
it( 'should be true for the default range (in text position)', () => {
expect( selection.isCollapsed ).to.be.true;
} );
it( 'should be false for the default range (object selection) ', () => {
root._insertChild( 0, new ModelElement( 'widget' ) );
expect( selection.isCollapsed ).to.be.false;
} );
it( 'should back off to the default algorithm if selection has ranges', () => {
selection._setTo( range );
expect( selection.isCollapsed ).to.be.false;
} );
} );
describe( 'anchor', () => {
it( 'should equal the default range\'s start (in text position)', () => {
const expectedPos = new ModelPosition( root, [ 0, 0 ] );
expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
} );
it( 'should equal the default range\'s start (object selection)', () => {
root._insertChild( 0, new ModelElement( 'widget' ) );
const expectedPos = new ModelPosition( root, [ 0 ] );
expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
} );
it( 'should back off to the default algorithm if selection has ranges', () => {
selection._setTo( range );
expect( selection.anchor.isEqual( range.start ) ).to.be.true;
} );
} );
describe( 'focus', () => {
it( 'should equal the default range\'s end (in text position)', () => {
const expectedPos = new ModelPosition( root, [ 0, 0 ] );
expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
} );
it( 'should equal the default range\'s end (object selection)', () => {
root._insertChild( 0, new ModelElement( 'widget' ) );
const expectedPos = new ModelPosition( root, [ 1 ] );
expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
expect( selection.focus.isEqual( selection.getFirstRange().end ) ).to.be.true;
} );
it( 'should back off to the default algorithm if selection has ranges', () => {
selection._setTo( range );
expect( selection.focus.isEqual( range.end ) ).to.be.true;
} );
} );
describe( 'rangeCount', () => {
it( 'should return proper range count', () => {
expect( selection.rangeCount ).to.equal( 1 );
selection._setTo( new ModelRange( new ModelPosition( root, [ 0 ] ), new ModelPosition( root, [ 0 ] ) ) );
expect( selection.rangeCount ).to.equal( 1 );
selection._setTo( [
new ModelRange( new ModelPosition( root, [ 2 ] ), new ModelPosition( root, [ 2 ] ) ),
new ModelRange( new ModelPosition( root, [ 0 ] ), new ModelPosition( root, [ 0 ] ) )
] );
expect( selection.rangeCount ).to.equal( 2 );
} );
} );
describe( 'hasOwnRange', () => {
it( 'should return true if selection has any ranges set', () => {
selection._setTo( new ModelRange( new ModelPosition( root, [ 0 ] ), new ModelPosition( root, [ 0 ] ) ) );
expect( selection.hasOwnRange ).to.be.true;
} );
it( 'should return false if selection has a default range', () => {
expect( selection.hasOwnRange ).to.be.false;
} );
} );
describe( 'markers', () => {
it( 'should implement #markers collection', () => {
expect( selection.markers ).to.instanceof( Collection );
expect( selection.markers ).to.length( 0 );
} );
it( 'should add markers to the collection when selection is inside the marker range', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
) );
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 0, 0 ] ),
writer.createPositionFromPath( root, [ 2, 2 ] )
),
usingOperation: false
} );
writer.addMarker( 'marker:2', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
),
usingOperation: false
} );
writer.addMarker( 'marker:3', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 5 ] )
),
usingOperation: false
} );
writer.addMarker( 'marker:4', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 4 ] ),
writer.createPositionFromPath( root, [ 3, 0 ] )
),
usingOperation: false
} );
} );
expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker:2', 'marker:3' ] );
} );
it( 'should update markers after selection change', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 2 ] )
) );
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 6 ] )
),
usingOperation: false
} );
writer.addMarker( 'marker:2', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 3 ] )
),
usingOperation: false
} );
writer.addMarker( 'marker:3', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 3 ] ),
writer.createPositionFromPath( root, [ 2, 6 ] )
),
usingOperation: false
} );
} );
expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker:1', 'marker:2' ] );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 4 ] ),
writer.createPositionFromPath( root, [ 2, 5 ] )
) );
} );
expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker:1', 'marker:3' ] );
} );
it( 'should update markers after markers change', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 2 ] )
) );
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 6 ] )
),
usingOperation: false
} );
writer.addMarker( 'marker:2', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 3 ] )
),
usingOperation: false
} );
writer.addMarker( 'marker:3', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 3 ] ),
writer.createPositionFromPath( root, [ 2, 6 ] )
),
usingOperation: false
} );
} );
expect( selection.markers.map( marker => marker.name ), 1 ).to.have.members( [ 'marker:1', 'marker:2' ] );
model.change( writer => {
writer.removeMarker( 'marker:1' );
writer.updateMarker( 'marker:2', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 3 ] ),
writer.createPositionFromPath( root, [ 2, 6 ] )
),
usingOperation: false
} );
writer.updateMarker( 'marker:3', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 3 ] )
),
usingOperation: false
} );
} );
expect( selection.markers.map( marker => marker.name ), 2 ).to.have.members( [ 'marker:3' ] );
} );
it( 'should not add marker when collapsed selection is on the marker left bound', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
) );
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] )
),
usingOperation: false
} );
} );
expect( selection.markers ).to.length( 0 );
} );
it( 'should not add marker when collapsed selection is on the marker right bound', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 4 ] )
) );
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
),
usingOperation: false
} );
} );
expect( selection.markers ).to.length( 0 );
} );
it( 'should add marker when non-collapsed selection is inside a marker and touches the left bound', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 3 ] )
) );
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 5 ] )
),
usingOperation: false
} );
} );
expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker:1' ] );
} );
it( 'should add marker when non-collapsed selection is inside a marker and touches the right bound', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] ),
writer.createPositionFromPath( root, [ 2, 5 ] )
) );
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 5 ] )
),
usingOperation: false
} );
} );
expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker:1' ] );
} );
it( 'should add marker of selected widget', () => {
selection.observeMarkers( 'marker' );
root._insertChild( 0, new ModelElement( 'widget' ) );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 0 ] ),
writer.createPositionFromPath( root, [ 1 ] )
) );
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 0 ] ),
writer.createPositionFromPath( root, [ 1 ] )
),
usingOperation: false
} );
} );
expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker:1' ] );
} );
it( 'should not add markers to the collection when markers observing is not enabled', () => {
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
) );
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 0, 0 ] ),
writer.createPositionFromPath( root, [ 2, 2 ] )
),
usingOperation: false
} );
writer.addMarker( 'marker:2', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
),
usingOperation: false
} );
writer.addMarker( 'marker:3', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 5 ] )
),
usingOperation: false
} );
writer.addMarker( 'marker:4', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 4 ] ),
writer.createPositionFromPath( root, [ 3, 0 ] )
),
usingOperation: false
} );
} );
expect( selection.markers ).to.length( 0 );
} );
it( 'should add markers to the collection only for observed marker groups', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
) );
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 0, 0 ] ),
writer.createPositionFromPath( root, [ 2, 2 ] )
),
usingOperation: false
} );
writer.addMarker( 'marker:2', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
),
usingOperation: false
} );
writer.addMarker( 'otherGroup:3', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 5 ] )
),
usingOperation: false
} );
writer.addMarker( 'otherGroup:4', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 4 ] ),
writer.createPositionFromPath( root, [ 3, 0 ] )
),
usingOperation: false
} );
} );
expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker:2' ] );
} );
it( 'should add marker to the collection only for observed marker name', () => {
selection.observeMarkers( 'testMarker' );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
) );
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 0, 0 ] ),
writer.createPositionFromPath( root, [ 2, 2 ] )
),
usingOperation: false
} );
writer.addMarker( 'testMarker', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 2 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
),
usingOperation: false
} );
writer.addMarker( 'otherMarker', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 5 ] )
),
usingOperation: false
} );
writer.addMarker( 'otherGroup:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 5 ] )
),
usingOperation: false
} );
} );
expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'testMarker' ] );
} );
describe( 'should fire change:marker event when', () => {
// Set marker to range 0-4.
beforeEach( () => {
model.change( writer => {
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
),
usingOperation: false
} );
} );
} );
it( 'selection ranges change (marker added to the selection)', () => {
const spy = sinon.spy();
selection.observeMarkers( 'marker' );
model.change( writer => {
// The selection has no markers before the change.
model.document.selection.on( 'change:marker', ( evt, data ) => {
expect( data.oldMarkers ).to.deep.equal( [] );
spy();
} );
// Move selection to 1-2, that is inside 0-4 marker.
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 2 ] )
) );
} );
expect( spy.calledOnce ).to.be.true;
} );
it( 'selection ranges change (marker removed from the selection)', () => {
const spy = sinon.spy();
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createRange(
writer.createPositionFromPath( root, [ 2, 1 ] ),
writer.createPositionFromPath( root, [ 2, 2 ] )
) );
// The selection is in a marker before the change.
model.document.selection.on( 'change:marker', ( evt, data ) => {
expect( data.oldMarkers.map( marker => marker.name ) ).to.deep.equal( [ 'marker:1' ] );
spy();
} );
// Move the selection out of the marker.
writer.setSelection( writer.createPositionFromPath( root, [ 2, 5 ] ) );
} );
expect( spy.calledOnce ).to.be.true;
} );
it( 'selection focus changes (marker removed from the selection)', () => {
const spy = sinon.spy();
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createPositionFromPath( root, [ 2, 2 ] ) );
// The selection is in a marker before the change.
model.document.selection.on( 'change:marker', ( evt, data ) => {
expect( data.oldMarkers.map( marker => marker.name ) ).to.deep.equal( [ 'marker:1' ] );
spy();
} );
// Move the selection focus out of the marker.
writer.setSelectionFocus( writer.createPositionFromPath( root, [ 2, 5 ] ) );
} );
expect( spy.calledOnce ).to.be.true;
} );
it( 'a new marker contains the selection', () => {
const spy = sinon.spy();
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createPositionFromPath( root, [ 2, 5 ] ) );
// The selection is not in a marker before the change.
model.document.selection.on( 'change:marker', ( evt, data ) => {
expect( data.oldMarkers ).to.deep.equal( [] );
spy();
} );
writer.updateMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 6 ] )
)
} );
} );
expect( spy.calledOnce ).to.be.true;
} );
it( 'a marker stops contains the selection', () => {
const spy = sinon.spy();
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createPositionFromPath( root, [ 2, 3 ] ) );
// The selection is in a marker before the change.
model.document.selection.on( 'change:marker', ( evt, data ) => {
expect( data.oldMarkers.map( marker => marker.name ) ).to.deep.equal( [ 'marker:1' ] );
spy();
} );
writer.updateMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 1 ] )
)
} );
} );
expect( spy.calledOnce ).to.be.true;
} );
} );
describe( 'should not fire change:marker event when', () => {
// Set marker to range 0-4.
beforeEach( () => {
model.change( writer => {
writer.addMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 4 ] )
),
usingOperation: false
} );
} );
} );
it( 'selection ranges change does not change selection markers (no markers)', () => {
const spy = sinon.spy();
selection.observeMarkers( 'marker' );
model.document.selection.on( 'change:marker', spy );
model.change( writer => {
writer.setSelection( writer.createPositionFromPath( root, [ 2, 5 ] ) );
} );
expect( spy.called ).to.be.false;
} );
it( 'selection ranges change does not change selection markers (same markers)', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createPositionFromPath( root, [ 2, 2 ] ) );
} );
const spy = sinon.spy();
model.document.selection.on( 'change:marker', spy );
model.change( writer => {
writer.setSelection( writer.createPositionFromPath( root, [ 2, 3 ] ) );
} );
expect( spy.called ).to.be.false;
} );
it( 'selection focus change does not change selection markers', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createPositionFromPath( root, [ 2, 2 ] ) );
} );
const spy = sinon.spy();
model.document.selection.on( 'change:marker', spy );
model.change( writer => {
writer.setSelectionFocus( writer.createPositionFromPath( root, [ 2, 3 ] ) );
} );
expect( spy.called ).to.be.false;
} );
it( 'changed marker still contains the selection', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createPositionFromPath( root, [ 2, 2 ] ) );
} );
const spy = sinon.spy();
model.document.selection.on( 'change:marker', spy );
model.change( writer => {
writer.updateMarker( 'marker:1', {
range: writer.createRange(
writer.createPositionFromPath( root, [ 2, 0 ] ),
writer.createPositionFromPath( root, [ 2, 5 ] )
)
} );
} );
expect( spy.called ).to.be.false;
} );
it( 'removed marker did not contain the selection', () => {
selection.observeMarkers( 'marker' );
model.change( writer => {
writer.setSelection( writer.createPositionFromPath( root, [ 2, 5 ] ) );
} );
const spy = sinon.spy();
model.document.selection.on( 'change:marker', spy );
model.change( writer => {
writer.removeMarker( 'marker:1' );
} );
expect( spy.called ).to.be.false;
} );
} );
} );
describe( 'destroy()', () => {
it( 'should unbind all events', () => {
selection._setTo( [ range, liveRange ] );
const ranges = Array.from( selection._selection._ranges );
sinon.spy( ranges[ 0 ], 'detach' );
sinon.spy( ranges[ 1 ], 'detach' );
selection.destroy();
expect( ranges[ 0 ].detach.called ).to.be.true;
expect( ranges[ 1 ].detach.called ).to.be.true;
} );
} );
describe( 'getFirstRange()', () => {
it( 'should return default range if no ranges were added', () => {
const firstRange = selection.getFirstRange();
expect( firstRange.start.isEqual( new ModelPosition( root, [ 0, 0 ] ) ) );
expect( firstRange.end.isEqual( new ModelPosition( root, [ 0, 0 ] ) ) );
} );
} );
describe( 'getLastRange()', () => {
it( 'should return default range if no ranges were added', () => {
const lastRange = selection.getLastRange();
expect( lastRange.start.isEqual( new ModelPosition( root, [ 0, 0 ] ) ) );
expect( lastRange.end.isEqual( new ModelPosition( root, [ 0, 0 ] ) ) );
} );
} );
describe( 'getSelectedElement()', () => {
it( 'should return selected element', () => {
selection._setTo( liveRange );
const p = root.getChild( 0 );
expect( selection.getSelectedElement() ).to.equal( p );
} );
} );
describe( 'is()', () => {
it( 'should return true for selection', () => {
expect( selection.is( 'selection' ) ).to.be.true;
expect( selection.is( 'model:selection' ) ).to.be.true;
} );
it( 'should return true for documentSelection', () => {
expect( selection.is( 'documentSelection' ) ).to.be.true;
expect( selection.is( 'model:documentSelection' ) ).to.be.true;
} );
it( 'should return false for other values', () => {
expect( selection.is( 'node' ) ).to.be.false;
expect( selection.is( 'model:node' ) ).to.be.false;
expect( selection.is( '$text' ) ).to.be.false;
expect( selection.is( '$textProxy' ) ).to.be.false;
expect( selection.is( 'element' ) ).to.be.false;
expect( selection.is( 'element', 'paragraph' ) ).to.be.false;
expect( selection.is( 'rootElement' ) ).to.be.false;
expect( selection.is( 'view:selection' ) ).to.be.false;
expect( selection.is( 'view:documentSelection' ) ).to.be.false;
} );
} );
describe( '_setTo() - set collapsed at', () => {
it( 'detaches all existing ranges', () => {
selection._setTo( [ range, liveRange ] );
const spy = sinon.spy( ModelLiveRange.prototype, 'detach' );
selection._setTo( root, 0 );
expect( spy.calledTwice ).to.be.true;
} );
} );
describe( '_setFocus()', () => {
it( 'modifies default range', () => {
const startPos = selection.getFirstPosition();
const endPos = ModelPosition._createAt( root, 'end' );
selection._setFocus( endPos );
expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
} );
it( 'detaches the range it replaces', () => {
const startPos = ModelPosition._createAt( root, 1 );
const endPos = ModelPosition._createAt( root, 2 );
const newEndPos = ModelPosition._createAt( root, 4 );
const spy = sinon.spy( ModelLiveRange.prototype, 'detach' );
selection._setTo( new ModelRange( startPos, endPos ) );
selection._setFocus( newEndPos );
expect( spy.calledOnce ).to.be.true;
} );
it( 'refreshes attributes', () => {
const spy = sinon.spy( selection._selection, '_updateAttributes' );
selection._setFocus( ModelPosition._createAt( root, 1 ) );
expect( spy.called ).to.be.true;
} );
} );
describe( '_setTo() - remove all ranges', () => {
let spy, ranges;
beforeEach( () => {
selection._setTo( [ liveRange, range ] );
spy = sinon.spy();
selection.on( 'change:range', spy );
ranges = Array.from( selection._selection._ranges );
sinon.spy( ranges[ 0 ], 'detach' );
sinon.spy( ranges[ 1 ], 'detach' );
selection._setTo( null );
} );
it( 'should remove all stored ranges (and reset to default range)', () => {
expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
expect( selection.anchor.isEqual( new ModelPosition( root, [ 0, 0 ] ) ) ).to.be.true;
expect( selection.focus.isEqual( new ModelPosition( root, [ 0, 0 ] ) ) ).to.be.true;
} );
it( 'should detach ranges', () => {
expect( ranges[ 0 ].detach.called ).to.be.true;
expect( ranges[ 1 ].detach.called ).to.be.true;
} );
it( 'should refresh attributes', () => {
const spy = sinon.spy( selection._selection, '_updateAttributes' );
selection._setTo( null );
expect( spy.called ).to.be.true;
} );
} );
describe( '_setTo()', () => {
it( 'should throw an error when range is invalid', () => {
expectToThrowCKEditorError( () => {
selection._setTo( [ { invalid: 'range' } ] );
}, /model-selection-set-ranges-not-range/, model );
} );
it( 'should do nothing when trying to set selection to the graveyard', () => {
// Catches the 'Trying to add a Range that is in the graveyard root. Range rejected.' warning in the CK_DEBUG mode.
sinon.stub( console, 'warn' );
const range = new ModelRange( new ModelPosition( model.document.graveyard, [ 0 ] ) );
selection._setTo( range );
expect( selection._ranges ).to.deep.equal( [] );
} );
it( 'should detach removed ranges', () => {