Skip to content

Commit

Permalink
Widget: modified widget to throw exception on attempt to call private…
Browse files Browse the repository at this point in the history
… methods. Fixed #6947 - Attempt to access private member of widget returns jQuery object
  • Loading branch information
mindfilleter committed Feb 16, 2011
1 parent ed531ef commit c94ec23
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
11 changes: 9 additions & 2 deletions tests/unit/widget/widget_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,11 @@ test( "direct usage", function() {
});

test( "error handling", function() {
expect( 2 );
expect( 3 );
var error = $.error;
$.widget( "ui.testWidget", {} );
$.widget( "ui.testWidget", {
_privateMethod: function () {}
});
$.error = function( msg ) {
equal( msg, "cannot call methods on testWidget prior to initialization; " +
"attempted to call method 'missing'", "method call before init" );
Expand All @@ -179,6 +181,11 @@ test( "error handling", function() {
"invalid method call on widget instance" );
};
$( "<div>" ).testWidget().testWidget( "missing" );
$.error = function ( msg ) {
equal( msg, "no such method '_privateMethod' for testWidget widget instance",
"invalid method call on widget instance" );
};
$( "<div>" ).testWidget().testWidget( "_privateMethod" );
$.error = error;
});

Expand Down
7 changes: 1 addition & 6 deletions ui/jquery.ui.widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,14 @@ $.widget.bridge = function( name, object ) {
$.extend.apply( null, [ true, options ].concat(args) ) :
options;

// prevent calls to internal methods
if ( isMethodCall && options.charAt( 0 ) === "_" ) {
return returnValue;
}

if ( isMethodCall ) {
this.each(function() {
var instance = $.data( this, name );
if ( !instance ) {
return $.error( "cannot call methods on " + name + " prior to initialization; " +
"attempted to call method '" + options + "'" );
}
if ( !$.isFunction( instance[options] ) ) {
if ( !$.isFunction( instance[options] ) || options.charAt( 0 ) === "_" ) {
return $.error( "no such method '" + options + "' for " + name + " widget instance" );
}
var methodValue = instance[ options ].apply( instance, args );
Expand Down

0 comments on commit c94ec23

Please sign in to comment.