@@ -37,6 +37,27 @@ bender.test( {
37
37
assert . areSame ( expected || source , bender . tools . getHtmlWithSelection ( ed ) ) ;
38
38
} ,
39
39
40
+ setSelectionInEmptyInlineElement : function ( editor ) {
41
+ var editable = editor . editable ( ) ,
42
+ range = editor . createRange ( ) ;
43
+
44
+ editable . setHtml ( '<p>x<u></u>x</p>' ) ;
45
+
46
+ var uEl = editable . findOne ( 'u' ) ;
47
+
48
+ range . moveToPosition ( uEl , CKEDITOR . POSITION_AFTER_START ) ;
49
+ editor . getSelection ( ) . selectRanges ( [ range ] ) ;
50
+ } ,
51
+
52
+ assertFillingChar : function ( editable , parent , contents , msg ) {
53
+ var fillingChar = editable . getCustomData ( 'cke-fillingChar' ) ;
54
+ assert . isTrue ( ! ! fillingChar , 'Filling char exists - ' + msg ) ;
55
+ assert . areSame ( parent , fillingChar . getParent ( ) , 'Filling char parent - ' + msg ) ;
56
+ assert . areSame ( contents , fillingChar . getText ( ) , 'Filling char contents - ' + msg ) ;
57
+
58
+ return fillingChar ;
59
+ } ,
60
+
40
61
'test editor selection with no focus' : function ( ) {
41
62
var ed = this . editor ;
42
63
@@ -444,6 +465,211 @@ bender.test( {
444
465
} ) ;
445
466
} ,
446
467
468
+ 'test filling char is removed and restored when taking snapshot' : function ( ) {
469
+ if ( ! CKEDITOR . env . webkit )
470
+ assert . ignore ( ) ;
471
+
472
+ var editor = this . editor ,
473
+ bot = this . editorBot ,
474
+ editable = editor . editable ( ) ,
475
+ range = editor . createRange ( ) ;
476
+
477
+ this . setSelectionInEmptyInlineElement ( editor ) ;
478
+
479
+ var uEl = editable . findOne ( 'u' ) ,
480
+ fillingChar = this . assertFillingChar ( editable , uEl , '\u200b' , 'after set selection' ) ;
481
+
482
+ editor . fire ( 'beforeUndoImage' ) ;
483
+ this . assertFillingChar ( editable , uEl , '' , 'after beforeUndoImage' ) ;
484
+
485
+ editor . fire ( 'afterUndoImage' ) ;
486
+ fillingChar = this . assertFillingChar ( editable , uEl , '\u200b' , 'after afterUndoImage' ) ;
487
+
488
+ range = editor . getSelection ( ) . getRanges ( ) [ 0 ] ;
489
+ assert . areSame ( fillingChar , range . startContainer , 'Selection was restored - container' ) ;
490
+ assert . areSame ( 1 , range . startOffset , 'Selection was restored - offset after ZWS' ) ;
491
+ } ,
492
+
493
+ // #12489
494
+ 'test filling char is removed and restored when taking snapshot if selection is not right after the filling char' : function ( ) {
495
+ if ( ! CKEDITOR . env . webkit )
496
+ assert . ignore ( ) ;
497
+
498
+ var editor = this . editor ,
499
+ bot = this . editorBot ,
500
+ editable = editor . editable ( ) ,
501
+ range = editor . createRange ( ) ;
502
+
503
+ this . setSelectionInEmptyInlineElement ( editor ) ;
504
+
505
+ var uEl = editable . findOne ( 'u' ) ,
506
+ fillingChar = this . assertFillingChar ( editable , uEl , '\u200b' , 'after set selection' ) ;
507
+
508
+ // Happens when typing and navigating...
509
+ // Setting selection using native API to avoid losing the filling char on selection.setRanges().
510
+ fillingChar . setText ( fillingChar . getText ( ) + 'abcd' ) ;
511
+ editor . document . $ . getSelection ( ) . setPosition ( fillingChar . $ , 3 ) ; // ZWSab^cd
512
+
513
+ this . assertFillingChar ( editable , uEl , '\u200babcd' , 'after type' ) ;
514
+
515
+ editor . fire ( 'beforeUndoImage' ) ;
516
+ this . assertFillingChar ( editable , uEl , 'abcd' , 'after beforeUndoImage' ) ;
517
+
518
+ editor . fire ( 'afterUndoImage' ) ;
519
+ fillingChar = this . assertFillingChar ( editable , uEl , '\u200babcd' , 'after afterUndoImage' ) ;
520
+
521
+ range = editor . getSelection ( ) . getRanges ( ) [ 0 ] ;
522
+ assert . areSame ( fillingChar , range . startContainer , 'Selection was restored - container' ) ;
523
+ assert . areSame ( 3 , range . startOffset , 'Selection was restored - offset in ZWSab^cd' ) ;
524
+ } ,
525
+
526
+ // #8617
527
+ 'test selection is preserved when removing filling char on left-arrow' : function ( ) {
528
+ if ( ! CKEDITOR . env . webkit )
529
+ assert . ignore ( ) ;
530
+
531
+ var editor = this . editor ,
532
+ bot = this . editorBot ,
533
+ editable = editor . editable ( ) ,
534
+ range = editor . createRange ( ) ;
535
+
536
+ this . setSelectionInEmptyInlineElement ( editor ) ;
537
+
538
+ var uEl = editable . findOne ( 'u' ) ,
539
+ fillingChar = this . assertFillingChar ( editable , uEl , '\u200b' , 'after setting selection' ) ;
540
+
541
+ // Happens when typing and navigating...
542
+ // Setting selection using native API to avoid losing the filling char on selection.setRanges().
543
+ fillingChar . setText ( fillingChar . getText ( ) + 'abc' ) ;
544
+ editor . document . $ . getSelection ( ) . setPosition ( fillingChar . $ , 4 ) ; // ZWSabc^
545
+
546
+ this . assertFillingChar ( editable , uEl , '\u200babc' , 'after typing' ) ;
547
+
548
+ // Mock LEFT arrow.
549
+ editor . document . fire ( 'keydown' , new CKEDITOR . dom . event ( { keyCode : 37 } ) ) ;
550
+
551
+ assert . areSame ( 'abc' , uEl . getHtml ( ) , 'Filling char is removed on left-arrow press' ) ;
552
+
553
+ range = editor . getSelection ( ) . getRanges ( ) [ 0 ] ;
554
+ assert . areSame ( uEl . getFirst ( ) , range . startContainer , 'Selection was restored - container' ) ;
555
+ assert . areSame ( 3 , range . startOffset , 'Selection was restored - offset in abc^' ) ;
556
+ } ,
557
+
558
+ // #12419
559
+ 'test selection is preserved when removing filling char on select all' : function ( ) {
560
+ if ( ! CKEDITOR . env . webkit )
561
+ assert . ignore ( ) ;
562
+
563
+ var editor = this . editor ,
564
+ bot = this . editorBot ,
565
+ editable = editor . editable ( ) ,
566
+ range = editor . createRange ( ) ,
567
+ htmlMatchingOpts = {
568
+ compareSelection : true ,
569
+ normalizeSelection : true
570
+ } ;
571
+
572
+ this . setSelectionInEmptyInlineElement ( editor ) ;
573
+
574
+ var uEl = editable . findOne ( 'u' ) ,
575
+ fillingChar = this . assertFillingChar ( editable , uEl , '\u200b' , 'after setting selection' ) ;
576
+
577
+ // Happens when typing and navigating...
578
+ // Setting selection using native API to avoid losing the filling char on selection.setRanges().
579
+ fillingChar . setText ( fillingChar . getText ( ) + 'abc' ) ;
580
+ editor . document . $ . getSelection ( ) . setPosition ( fillingChar . $ , 4 ) ; // ZWSabc^
581
+
582
+ this . assertFillingChar ( editable , uEl , '\u200babc' , 'after typing' ) ;
583
+
584
+ // Select all contents.
585
+ range . selectNodeContents ( editable . findOne ( 'p' ) ) ;
586
+ editor . getSelection ( ) . selectRanges ( [ range ] ) ;
587
+
588
+ assert . areSame ( 'abc' , uEl . getHtml ( ) , 'Filling char is removed on selection change' ) ;
589
+ assert . isInnerHtmlMatching ( '<p>[x<u>abc</u>x]</p>' , bender . tools . selection . getWithHtml ( editor ) ,
590
+ htmlMatchingOpts , 'Selection is correctly set' ) ;
591
+ } ,
592
+
593
+ 'test direction of selection is preserved when removing filling char' : function ( ) {
594
+ if ( ! CKEDITOR . env . webkit )
595
+ assert . ignore ( ) ;
596
+
597
+ var editor = this . editor ,
598
+ bot = this . editorBot ,
599
+ editable = editor . editable ( ) ,
600
+ range = editor . createRange ( ) ;
601
+
602
+ this . setSelectionInEmptyInlineElement ( editor ) ;
603
+
604
+ var uEl = editable . findOne ( 'u' ) ,
605
+ fillingChar = this . assertFillingChar ( editable , uEl , '\u200b' , 'after setting selection' ) ;
606
+
607
+ // Happens when typing and making selection from right to left...
608
+ // Setting selection using native API to avoid losing the filling char on selection.setRanges().
609
+ fillingChar . setText ( fillingChar . getText ( ) + 'abc' ) ;
610
+ range = editor . document . $ . createRange ( ) ;
611
+ // ZWSabc]
612
+ range . setStart ( fillingChar . $ , 4 ) ;
613
+ var nativeSel = editor . document . $ . getSelection ( ) ;
614
+ nativeSel . removeAllRanges ( ) ;
615
+ nativeSel . addRange ( range ) ;
616
+ // ZWSa[bc
617
+ nativeSel . extend ( fillingChar . $ , 2 ) ;
618
+
619
+ this . assertFillingChar ( editable , uEl , '\u200babc' , 'after typing' ) ;
620
+
621
+ // Mock LEFT arrow.
622
+ editor . document . fire ( 'keydown' , new CKEDITOR . dom . event ( { keyCode : 37 } ) ) ;
623
+
624
+ assert . areSame ( 'abc' , uEl . getHtml ( ) , 'Filling char is removed on left-arrow press' ) ;
625
+
626
+ nativeSel = editor . document . $ . getSelection ( ) ;
627
+ assert . areSame ( 3 , nativeSel . anchorOffset , 'sel.anchorOffset' ) ;
628
+ assert . areSame ( 1 , nativeSel . focusOffset , 'sel.focusOffset' ) ;
629
+ } ,
630
+
631
+ // This particular scenario is reproducible when after typing in an empty inline element
632
+ // user tries to select text by mouse from right to left in that element - selection is lost.
633
+ // #12491 comment:3
634
+ 'test direction of selection is preserved when taking snapshot' : function ( ) {
635
+ if ( ! CKEDITOR . env . webkit )
636
+ assert . ignore ( ) ;
637
+
638
+ var editor = this . editor ,
639
+ bot = this . editorBot ,
640
+ editable = editor . editable ( ) ,
641
+ range = editor . createRange ( ) ;
642
+
643
+ this . setSelectionInEmptyInlineElement ( editor ) ;
644
+
645
+ var uEl = editable . findOne ( 'u' ) ,
646
+ fillingChar = this . assertFillingChar ( editable , uEl , '\u200b' , 'after set selection' ) ;
647
+
648
+ // Happens when typing and making selection from right to left...
649
+ // Setting selection using native API to avoid losing the filling char on selection.setRanges().
650
+ fillingChar . setText ( fillingChar . getText ( ) + 'abc' ) ;
651
+ range = editor . document . $ . createRange ( ) ;
652
+ // ZWSabc]
653
+ range . setStart ( fillingChar . $ , 4 ) ;
654
+ var nativeSel = editor . document . $ . getSelection ( ) ;
655
+ nativeSel . removeAllRanges ( ) ;
656
+ nativeSel . addRange ( range ) ;
657
+ // ZWSa[bc
658
+ nativeSel . extend ( fillingChar . $ , 2 ) ;
659
+
660
+ this . assertFillingChar ( editable , uEl , '\u200babc' , 'after type' ) ;
661
+
662
+ editor . fire ( 'beforeUndoImage' ) ;
663
+ this . assertFillingChar ( editable , uEl , 'abc' , 'after beforeUndoImage' ) ;
664
+
665
+ editor . fire ( 'afterUndoImage' ) ;
666
+ this . assertFillingChar ( editable , uEl , '\u200babc' , 'after afterUndoImage' ) ;
667
+
668
+ nativeSel = editor . document . $ . getSelection ( ) ;
669
+ assert . areSame ( 4 , nativeSel . anchorOffset , 'sel.anchorOffset' ) ;
670
+ assert . areSame ( 2 , nativeSel . focusOffset , 'sel.focusOffset' ) ;
671
+ } ,
672
+
447
673
'test selection in source mode' : function ( ) {
448
674
bender . editorBot . create ( {
449
675
name : 'test_editor_source' ,
0 commit comments