Skip to content

Commit 16e93d5

Browse files
committed
Core: Fixed .disableSelect() and .enableSelect() in all browsers. Fixes #5723 - disableSelection() doesn't work cross-browser.
1 parent a3d9a91 commit 16e93d5

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

ui/jquery.ui.core.js

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,6 @@ $.fn.extend({
7373
this._focus.apply( this, arguments );
7474
},
7575

76-
enableSelection: function() {
77-
return this
78-
.attr( "unselectable", "off" )
79-
.css( "MozUserSelect", "" );
80-
},
81-
82-
disableSelection: function() {
83-
return this
84-
.attr( "unselectable", "on" )
85-
.css( "MozUserSelect", "none" );
86-
},
87-
8876
scrollParent: function() {
8977
var scrollParent;
9078
if (($.browser.msie && (/(static|relative)/).test(this.css('position'))) || (/absolute/).test(this.css('position'))) {
@@ -130,6 +118,57 @@ $.fn.extend({
130118
}
131119
});
132120

121+
(function() {
122+
var elem = document.createElement( "div" ),
123+
style = elem.style,
124+
userSelectProp = "userSelect" in style && "userSelect";
125+
126+
if ( !userSelectProp ) {
127+
$.each( [ "Moz", "Webkit", "Khtml" ], function( i, prefix ) {
128+
var vendorProp = prefix + "UserSelect";
129+
if ( vendorProp in style ) {
130+
userSelectProp = vendorProp;
131+
return false;
132+
}
133+
});
134+
}
135+
var selectStart = !userSelectProp && "onselectstart" in elem && "selectstart.mouse";
136+
137+
elem = null;
138+
139+
$.fn.extend({
140+
disableSelection: function() {
141+
if ( userSelectProp ) {
142+
this.css( userSelectProp, "none" );
143+
} else {
144+
this.find( "*" ).andSelf().attr( "unselectable", "on" );
145+
}
146+
147+
if ( selectStart ) {
148+
this.bind( selectStart, function() {
149+
return false;
150+
});
151+
}
152+
153+
return this;
154+
},
155+
156+
enableSelection: function() {
157+
if ( userSelectProp ) {
158+
this.css( userSelectProp, "" );
159+
} else {
160+
this.find( "*" ).andSelf().attr( "unselectable", "off" );
161+
}
162+
163+
if ( selectStart ) {
164+
this.unbind( selectStart );
165+
}
166+
167+
return this;
168+
}
169+
});
170+
})();
171+
133172
$.each( [ "Width", "Height" ], function( i, name ) {
134173
var side = name === "Width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ],
135174
type = name.toLowerCase(),

0 commit comments

Comments
 (0)