Skip to content

Commit

Permalink
Merge pull request #818 from ckeditor/t/800
Browse files Browse the repository at this point in the history
Added selection.isCollapsed method
  • Loading branch information
mlewand authored Aug 30, 2017
2 parents 51c188c + 8247917 commit 66befd5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
16 changes: 16 additions & 0 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,22 @@
return isTableSelection( this.getRanges(), allowPartialSelection );
},

/**
* Checks if the selection contains only one range, which is collapsed.
*
* if ( editor.getSelection().isCollapsed() ) {
* // Do something when selection is collapsed.
* }
*
* @since 4.7.3
* @returns {Boolean}
*/
isCollapsed: function() {
var ranges = this.getRanges();

return ranges.length === 1 && ranges[ 0 ].collapsed;
},

/**
* Creates a bookmark for each range of this selection (from {@link #getRanges})
* by calling the {@link CKEDITOR.dom.range#createBookmark} method,
Expand Down
59 changes: 59 additions & 0 deletions tests/core/selection/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,5 +726,64 @@ bender.test( {
this.editor.document.fire( 'keydown', new CKEDITOR.dom.event( { keyCode: 46 } ) );

assert.areEqual( 7, editable.$.innerText.length );
},

// #800
'test isCollapsed on collapsed selection': function() {
var editor = this.editor;

bender.tools.setHtmlWithSelection( editor, '<p>Te^st</p>' );

assert.isTrue( editor.getSelection().isCollapsed() );
},

// #800
'test isCollapsed on non-collapsed selection': function() {
var editor = this.editor;

bender.tools.setHtmlWithSelection( editor, '<p>T[es]t</p>' );

assert.isFalse( editor.getSelection().isCollapsed() );
},

// #800
'test isCollapsed on selection with no ranges': function() {
// In old IEs it's actually impossible to get really empty selection.
if ( typeof window.getSelection != 'function' ) {
assert.ignore();
}

var editor = this.editor;

bender.tools.setHtmlWithSelection( editor, '<p>T[es]t</p>' );
editor.getSelection().removeAllRanges();

assert.isFalse( editor.getSelection().isCollapsed() );
},

// #800
'test isCollapsed on multi-range selection': function() {
if ( !CKEDITOR.env.gecko ) {
assert.ignore();
}

var editor = this.editor;

bender.tools.setHtmlWithSelection( editor, '<p>[T]e[s]t</p>' );

assert.isFalse( editor.getSelection().isCollapsed() );
},

// #800
'test isCollapsed on multiple collapsed selections': function() {
if ( !CKEDITOR.env.gecko ) {
assert.ignore();
}

var editor = this.editor;

bender.tools.setHtmlWithSelection( editor, '<p>T^es^t</p>' );

assert.isFalse( editor.getSelection().isCollapsed() );
}
} );
7 changes: 6 additions & 1 deletion tests/plugins/tableselection/integrations/core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@
selection = editor.getSelection(),
initialRev = selection.rev,
realSelection,
ranges;
ranges,
i;

bender.tools.setHtmlWithSelection( editor, CKEDITOR.document.getById( 'simpleTable' ).getHtml() );

Expand All @@ -218,6 +219,10 @@
assert.isTrue( _getTableElementFromRange( ranges[ 0 ] ).equals( selection.getSelectedElement() ),
'Selected element equals to the first selected cell' );

for ( i = 0; i < ranges.length; i++ ) {
assert.isFalse( ranges[ i ].collapsed, 'Range #' + i + ' is not collapsed' );
}

realSelection = editor.getSelection( 1 );

assert.areSame( 1, realSelection.getRanges().length, 'Real selection has only one range' );
Expand Down

0 comments on commit 66befd5

Please sign in to comment.