@@ -442,6 +442,224 @@ describe( 'DocumentSelection', () => {
442
442
443
443
expect ( selection . markers . map ( marker => marker . name ) ) . to . have . members ( [ 'marker' ] ) ;
444
444
} ) ;
445
+
446
+ describe ( 'should fire change:marker event when' , ( ) => {
447
+ // Set marker to range 0-4.
448
+ beforeEach ( ( ) => {
449
+ model . change ( writer => {
450
+ writer . addMarker ( 'marker-1' , {
451
+ range : writer . createRange (
452
+ writer . createPositionFromPath ( root , [ 2 , 0 ] ) ,
453
+ writer . createPositionFromPath ( root , [ 2 , 4 ] )
454
+ ) ,
455
+ usingOperation : false
456
+ } ) ;
457
+ } ) ;
458
+ } ) ;
459
+
460
+ it ( 'selection ranges change (marker added to the selection)' , ( ) => {
461
+ const spy = sinon . spy ( ) ;
462
+
463
+ model . change ( writer => {
464
+ // The selection has no markers before the change.
465
+ model . document . selection . on ( 'change:marker' , ( evt , data ) => {
466
+ expect ( data . oldMarkers ) . to . deep . equal ( [ ] ) ;
467
+ spy ( ) ;
468
+ } ) ;
469
+
470
+ // Move selection to 1-2, that is inside 0-4 marker.
471
+ writer . setSelection ( writer . createRange (
472
+ writer . createPositionFromPath ( root , [ 2 , 1 ] ) ,
473
+ writer . createPositionFromPath ( root , [ 2 , 2 ] )
474
+ ) ) ;
475
+ } ) ;
476
+
477
+ expect ( spy . calledOnce ) . to . be . true ;
478
+ } ) ;
479
+
480
+ it ( 'selection ranges change (marker removed from the selection)' , ( ) => {
481
+ const spy = sinon . spy ( ) ;
482
+
483
+ model . change ( writer => {
484
+ writer . setSelection ( writer . createRange (
485
+ writer . createPositionFromPath ( root , [ 2 , 1 ] ) ,
486
+ writer . createPositionFromPath ( root , [ 2 , 2 ] )
487
+ ) ) ;
488
+
489
+ // The selection is in a marker before the change.
490
+ model . document . selection . on ( 'change:marker' , ( evt , data ) => {
491
+ expect ( data . oldMarkers . map ( marker => marker . name ) ) . to . deep . equal ( [ 'marker-1' ] ) ;
492
+ spy ( ) ;
493
+ } ) ;
494
+
495
+ // Move the selection out of the marker.
496
+ writer . setSelection ( writer . createPositionFromPath ( root , [ 2 , 5 ] ) ) ;
497
+ } ) ;
498
+
499
+ expect ( spy . calledOnce ) . to . be . true ;
500
+ } ) ;
501
+
502
+ it ( 'selection focus changes (marker removed from the selection)' , ( ) => {
503
+ const spy = sinon . spy ( ) ;
504
+
505
+ model . change ( writer => {
506
+ writer . setSelection ( writer . createPositionFromPath ( root , [ 2 , 2 ] ) ) ;
507
+
508
+ // The selection is in a marker before the change.
509
+ model . document . selection . on ( 'change:marker' , ( evt , data ) => {
510
+ expect ( data . oldMarkers . map ( marker => marker . name ) ) . to . deep . equal ( [ 'marker-1' ] ) ;
511
+ spy ( ) ;
512
+ } ) ;
513
+
514
+ // Move the selection focus out of the marker.
515
+ writer . setSelectionFocus ( writer . createPositionFromPath ( root , [ 2 , 5 ] ) ) ;
516
+ } ) ;
517
+
518
+ expect ( spy . calledOnce ) . to . be . true ;
519
+ } ) ;
520
+
521
+ it ( 'a new marker contains the selection' , ( ) => {
522
+ const spy = sinon . spy ( ) ;
523
+
524
+ model . change ( writer => {
525
+ writer . setSelection ( writer . createPositionFromPath ( root , [ 2 , 5 ] ) ) ;
526
+
527
+ // The selection is not in a marker before the change.
528
+ model . document . selection . on ( 'change:marker' , ( evt , data ) => {
529
+ expect ( data . oldMarkers ) . to . deep . equal ( [ ] ) ;
530
+ spy ( ) ;
531
+ } ) ;
532
+
533
+ writer . updateMarker ( 'marker-1' , {
534
+ range : writer . createRange (
535
+ writer . createPositionFromPath ( root , [ 2 , 0 ] ) ,
536
+ writer . createPositionFromPath ( root , [ 2 , 6 ] )
537
+ )
538
+ } ) ;
539
+ } ) ;
540
+
541
+ expect ( spy . calledOnce ) . to . be . true ;
542
+ } ) ;
543
+
544
+ it ( 'a marker stops contains the selection' , ( ) => {
545
+ const spy = sinon . spy ( ) ;
546
+
547
+ model . change ( writer => {
548
+ writer . setSelection ( writer . createPositionFromPath ( root , [ 2 , 3 ] ) ) ;
549
+
550
+ // The selection is in a marker before the change.
551
+ model . document . selection . on ( 'change:marker' , ( evt , data ) => {
552
+ expect ( data . oldMarkers . map ( marker => marker . name ) ) . to . deep . equal ( [ 'marker-1' ] ) ;
553
+ spy ( ) ;
554
+ } ) ;
555
+
556
+ writer . updateMarker ( 'marker-1' , {
557
+ range : writer . createRange (
558
+ writer . createPositionFromPath ( root , [ 2 , 0 ] ) ,
559
+ writer . createPositionFromPath ( root , [ 2 , 1 ] )
560
+ )
561
+ } ) ;
562
+ } ) ;
563
+
564
+ expect ( spy . calledOnce ) . to . be . true ;
565
+ } ) ;
566
+ } ) ;
567
+
568
+ describe ( 'should not fire change:marker event when' , ( ) => {
569
+ // Set marker to range 0-4.
570
+ beforeEach ( ( ) => {
571
+ model . change ( writer => {
572
+ writer . addMarker ( 'marker-1' , {
573
+ range : writer . createRange (
574
+ writer . createPositionFromPath ( root , [ 2 , 0 ] ) ,
575
+ writer . createPositionFromPath ( root , [ 2 , 4 ] )
576
+ ) ,
577
+ usingOperation : false
578
+ } ) ;
579
+ } ) ;
580
+ } ) ;
581
+
582
+ it ( 'selection ranges change does not change selection markers (no markers)' , ( ) => {
583
+ const spy = sinon . spy ( ) ;
584
+
585
+ model . document . selection . on ( 'change:marker' , spy ) ;
586
+
587
+ model . change ( writer => {
588
+ writer . setSelection ( writer . createPositionFromPath ( root , [ 2 , 5 ] ) ) ;
589
+ } ) ;
590
+
591
+ expect ( spy . called ) . to . be . false ;
592
+ } ) ;
593
+
594
+ it ( 'selection ranges change does not change selection markers (same markers)' , ( ) => {
595
+ model . change ( writer => {
596
+ writer . setSelection ( writer . createPositionFromPath ( root , [ 2 , 2 ] ) ) ;
597
+ } ) ;
598
+
599
+ const spy = sinon . spy ( ) ;
600
+
601
+ model . document . selection . on ( 'change:marker' , spy ) ;
602
+
603
+ model . change ( writer => {
604
+ writer . setSelection ( writer . createPositionFromPath ( root , [ 2 , 3 ] ) ) ;
605
+ } ) ;
606
+
607
+ expect ( spy . called ) . to . be . false ;
608
+ } ) ;
609
+
610
+ it ( 'selection focus change does not change selection markers' , ( ) => {
611
+ model . change ( writer => {
612
+ writer . setSelection ( writer . createPositionFromPath ( root , [ 2 , 2 ] ) ) ;
613
+ } ) ;
614
+
615
+ const spy = sinon . spy ( ) ;
616
+
617
+ model . document . selection . on ( 'change:marker' , spy ) ;
618
+
619
+ model . change ( writer => {
620
+ writer . setSelectionFocus ( writer . createPositionFromPath ( root , [ 2 , 3 ] ) ) ;
621
+ } ) ;
622
+
623
+ expect ( spy . called ) . to . be . false ;
624
+ } ) ;
625
+
626
+ it ( 'changed marker still contains the selection' , ( ) => {
627
+ model . change ( writer => {
628
+ writer . setSelection ( writer . createPositionFromPath ( root , [ 2 , 2 ] ) ) ;
629
+ } ) ;
630
+
631
+ const spy = sinon . spy ( ) ;
632
+
633
+ model . document . selection . on ( 'change:marker' , spy ) ;
634
+
635
+ model . change ( writer => {
636
+ writer . updateMarker ( 'marker-1' , {
637
+ range : writer . createRange (
638
+ writer . createPositionFromPath ( root , [ 2 , 0 ] ) ,
639
+ writer . createPositionFromPath ( root , [ 2 , 5 ] )
640
+ )
641
+ } ) ;
642
+ } ) ;
643
+
644
+ expect ( spy . called ) . to . be . false ;
645
+ } ) ;
646
+
647
+ it ( 'removed marker did not contain the selection' , ( ) => {
648
+ model . change ( writer => {
649
+ writer . setSelection ( writer . createPositionFromPath ( root , [ 2 , 5 ] ) ) ;
650
+ } ) ;
651
+
652
+ const spy = sinon . spy ( ) ;
653
+
654
+ model . document . selection . on ( 'change:marker' , spy ) ;
655
+
656
+ model . change ( writer => {
657
+ writer . removeMarker ( 'marker-1' ) ;
658
+ } ) ;
659
+
660
+ expect ( spy . called ) . to . be . false ;
661
+ } ) ;
662
+ } ) ;
445
663
} ) ;
446
664
447
665
describe ( 'destroy()' , ( ) => {
0 commit comments