Skip to content

Commit c94ec23

Browse files
committed
Widget: modified widget to throw exception on attempt to call private methods. Fixed #6947 - Attempt to access private member of widget returns jQuery object
1 parent ed531ef commit c94ec23

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

tests/unit/widget/widget_core.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,11 @@ test( "direct usage", function() {
166166
});
167167

168168
test( "error handling", function() {
169-
expect( 2 );
169+
expect( 3 );
170170
var error = $.error;
171-
$.widget( "ui.testWidget", {} );
171+
$.widget( "ui.testWidget", {
172+
_privateMethod: function () {}
173+
});
172174
$.error = function( msg ) {
173175
equal( msg, "cannot call methods on testWidget prior to initialization; " +
174176
"attempted to call method 'missing'", "method call before init" );
@@ -179,6 +181,11 @@ test( "error handling", function() {
179181
"invalid method call on widget instance" );
180182
};
181183
$( "<div>" ).testWidget().testWidget( "missing" );
184+
$.error = function ( msg ) {
185+
equal( msg, "no such method '_privateMethod' for testWidget widget instance",
186+
"invalid method call on widget instance" );
187+
};
188+
$( "<div>" ).testWidget().testWidget( "_privateMethod" );
182189
$.error = error;
183190
});
184191

ui/jquery.ui.widget.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,14 @@ $.widget.bridge = function( name, object ) {
104104
$.extend.apply( null, [ true, options ].concat(args) ) :
105105
options;
106106

107-
// prevent calls to internal methods
108-
if ( isMethodCall && options.charAt( 0 ) === "_" ) {
109-
return returnValue;
110-
}
111-
112107
if ( isMethodCall ) {
113108
this.each(function() {
114109
var instance = $.data( this, name );
115110
if ( !instance ) {
116111
return $.error( "cannot call methods on " + name + " prior to initialization; " +
117112
"attempted to call method '" + options + "'" );
118113
}
119-
if ( !$.isFunction( instance[options] ) ) {
114+
if ( !$.isFunction( instance[options] ) || options.charAt( 0 ) === "_" ) {
120115
return $.error( "no such method '" + options + "' for " + name + " widget instance" );
121116
}
122117
var methodValue = instance[ options ].apply( instance, args );

0 commit comments

Comments
 (0)