Skip to content

Commit 28b14ec

Browse files
jzaeffererscottgonzalez
authored andcommitted
Generate a uuid for each widget for unique namespaces. Fixes #8385 - Widget: _bind() on elements such as document are dangerous
1 parent 4a215e3 commit 28b14ec

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

tests/unit/widget/widget_core.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,10 @@ test( "_on() with delegate", function() {
711711
expect( 8 );
712712
$.widget( "ui.testWidget", {
713713
_create: function() {
714+
var uuid = this.uuid;
714715
this.element = {
715716
bind: function( event, handler ) {
716-
equal( event, "click.testWidget" );
717+
equal( event, "click.testWidget" + uuid );
717718
ok( $.isFunction(handler) );
718719
},
719720
trigger: $.noop
@@ -722,7 +723,7 @@ test( "_on() with delegate", function() {
722723
return {
723724
delegate: function( selector, event, handler ) {
724725
equal( selector, "a" );
725-
equal( event, "click.testWidget" );
726+
equal( event, "click.testWidget" + uuid );
726727
ok( $.isFunction(handler) );
727728
}
728729
};
@@ -735,7 +736,7 @@ test( "_on() with delegate", function() {
735736
return {
736737
delegate: function( selector, event, handler ) {
737738
equal( selector, "form fieldset > input" );
738-
equal( event, "change.testWidget" );
739+
equal( event, "change.testWidget" + uuid );
739740
ok( $.isFunction(handler) );
740741
}
741742
};
@@ -748,6 +749,24 @@ test( "_on() with delegate", function() {
748749
$.ui.testWidget();
749750
});
750751

752+
test( "_bind() to common element", function() {
753+
expect( 1 );
754+
$.widget( "ui.testWidget", {
755+
_create: function() {
756+
this._bind( this.document, {
757+
"customevent": "_handler"
758+
});
759+
},
760+
_handler: function() {
761+
ok( true, "handler triggered" );
762+
}
763+
});
764+
var widget = $( "#widget" ).testWidget().data( "testWidget" );
765+
$( "#widget-wrapper" ).testWidget();
766+
widget.destroy();
767+
$( document ).trigger( "customevent" );
768+
});
769+
751770
test( "._hoverable()", function() {
752771
$.widget( "ui.testWidget", {
753772
_create: function() {

ui/jquery.ui.widget.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
*/
1010
(function( $, undefined ) {
1111

12-
var slice = Array.prototype.slice,
12+
var uuid = 0,
13+
slice = Array.prototype.slice,
1314
_cleanData = $.cleanData;
1415
$.cleanData = function( elems ) {
1516
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
@@ -214,6 +215,7 @@ $.Widget.prototype = {
214215
this.options,
215216
this._getCreateOptions(),
216217
options );
218+
this.uuid = uuid++;
217219

218220
this.bindings = $();
219221
this.hoverable = $();
@@ -247,7 +249,7 @@ $.Widget.prototype = {
247249
// we can probably remove the unbind calls in 2.0
248250
// all event bindings should go through this._on()
249251
this.element
250-
.unbind( "." + this.widgetName )
252+
.unbind( "." + this.widgetName + this.uuid )
251253
// 1.9 BC for #7810
252254
// TODO remove dual storage
253255
.removeData( this.widgetName )
@@ -256,14 +258,14 @@ $.Widget.prototype = {
256258
// http://bugs.jquery.com/ticket/9413
257259
.removeData( $.camelCase( this.widgetFullName ) );
258260
this.widget()
259-
.unbind( "." + this.widgetName )
261+
.unbind( "." + this.widgetName + this.uuid )
260262
.removeAttr( "aria-disabled" )
261263
.removeClass(
262264
this.widgetFullName + "-disabled " +
263265
"ui-state-disabled" );
264266

265267
// clean up events and states
266-
this.bindings.unbind( "." + this.widgetName );
268+
this.bindings.unbind( "." + this.widgetName + this.uuid );
267269
this.hoverable.removeClass( "ui-state-hover" );
268270
this.focusable.removeClass( "ui-state-focus" );
269271
},
@@ -374,7 +376,7 @@ $.Widget.prototype = {
374376
}
375377

376378
var match = event.match( /^(\w+)\s*(.*)$/ ),
377-
eventName = match[1] + "." + instance.widgetName,
379+
eventName = match[1] + "." + instance.widgetName + instance.uuid,
378380
selector = match[2];
379381
if ( selector ) {
380382
instance.widget().delegate( selector, eventName, handlerProxy );

0 commit comments

Comments
 (0)