diff --git a/core/selection.js b/core/selection.js index 807970880d4..81ce1538075 100644 --- a/core/selection.js +++ b/core/selection.js @@ -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, diff --git a/tests/core/selection/selection.js b/tests/core/selection/selection.js index ce777316cd0..57885cdb59a 100644 --- a/tests/core/selection/selection.js +++ b/tests/core/selection/selection.js @@ -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, '
Te^st
' ); + + assert.isTrue( editor.getSelection().isCollapsed() ); + }, + + // #800 + 'test isCollapsed on non-collapsed selection': function() { + var editor = this.editor; + + bender.tools.setHtmlWithSelection( editor, 'T[es]t
' ); + + 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, 'T[es]t
' ); + 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, '[T]e[s]t
' ); + + 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, 'T^es^t
' ); + + assert.isFalse( editor.getSelection().isCollapsed() ); } } ); diff --git a/tests/plugins/tableselection/integrations/core/selection.js b/tests/plugins/tableselection/integrations/core/selection.js index b305d8fe8db..c6f0d8b3872 100644 --- a/tests/plugins/tableselection/integrations/core/selection.js +++ b/tests/plugins/tableselection/integrations/core/selection.js @@ -199,7 +199,8 @@ selection = editor.getSelection(), initialRev = selection.rev, realSelection, - ranges; + ranges, + i; bender.tools.setHtmlWithSelection( editor, CKEDITOR.document.getById( 'simpleTable' ).getHtml() ); @@ -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' );