Skip to content

Commit d4a437e

Browse files
committed
Selectmenu: Preserve text selection and button focus on click
Fixes #10639 Closes gh-1358
1 parent 73cdc09 commit d4a437e

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

ui/selectmenu.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ return $.widget( "ui.selectmenu", {
144144
role: "listbox",
145145
select: function( event, ui ) {
146146
event.preventDefault();
147+
148+
// support: IE8
149+
// If the item was selected via a click, the text selection
150+
// will be destroyed in IE
151+
that._setSelection();
152+
147153
that._select( ui.item.data( "ui-selectmenu-item" ), event );
148154
},
149155
focus: function( event, ui ) {
@@ -254,6 +260,7 @@ return $.widget( "ui.selectmenu", {
254260
this.isOpen = false;
255261
this._toggleAttr();
256262

263+
this.range = null;
257264
this._off( this.document );
258265

259266
this._trigger( "close", event );
@@ -342,6 +349,29 @@ return $.widget( "ui.selectmenu", {
342349
this[ this.isOpen ? "close" : "open" ]( event );
343350
},
344351

352+
_setSelection: function() {
353+
var selection;
354+
355+
if ( !this.range ) {
356+
return;
357+
}
358+
359+
if ( window.getSelection ) {
360+
selection = window.getSelection();
361+
selection.removeAllRanges();
362+
selection.addRange( this.range );
363+
364+
// support: IE8
365+
} else {
366+
this.range.select();
367+
}
368+
369+
// support: IE
370+
// Setting the text selection kills the button focus in IE, but
371+
// restoring the focus doesn't kill the selection.
372+
this.button.focus();
373+
},
374+
345375
_documentClick: {
346376
mousedown: function( event ) {
347377
if ( !this.isOpen ) {
@@ -357,11 +387,25 @@ return $.widget( "ui.selectmenu", {
357387
_buttonEvents: {
358388

359389
// Prevent text selection from being reset when interacting with the selectmenu (#10144)
360-
mousedown: function( event ) {
361-
event.preventDefault();
390+
mousedown: function() {
391+
var selection;
392+
393+
if ( window.getSelection ) {
394+
selection = window.getSelection();
395+
if ( selection.rangeCount ) {
396+
this.range = selection.getRangeAt( 0 );
397+
}
398+
399+
// support: IE8
400+
} else {
401+
this.range = document.selection.createRange();
402+
}
362403
},
363404

364-
click: "_toggle",
405+
click: function( event ) {
406+
this._setSelection();
407+
this._toggle( event );
408+
},
365409

366410
keydown: function( event ) {
367411
var preventDefault = true;

0 commit comments

Comments
 (0)