aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>2021-11-19 00:47:56 +0100
committerGitHub <noreply@github.com>2021-11-19 00:47:56 +0100
commited637b04d75e4ebd6ea523f23e6dee7f64b68145 (patch)
tree5f54903bbca19578ba4e6c8c9c57e3cbf9ccba43
parentb52ee4012d13e2f531a39fe0a53366e119dd1501 (diff)
downloadjquery-ui-ed637b04d75e4ebd6ea523f23e6dee7f64b68145.tar.gz
jquery-ui-ed637b04d75e4ebd6ea523f23e6dee7f64b68145.zip
Widget: Make contextless widget construction work
Due to the fact the widget factory code is now in strict mode, the check for being called without using the `new` keyword started breaking if you save the widget constructor to a variable before calling it: ```js var customWidget = $.custom.customWidget; customWidget( {}, elem ); ``` as then `this` is undefined and checking for `this._createWidget` crashes. Account for that with an additional check. Fixes gh-2015 Closes gh-2019
-rw-r--r--tests/unit/widget/core.js12
-rw-r--r--ui/widget.js2
2 files changed, 13 insertions, 1 deletions
diff --git a/tests/unit/widget/core.js b/tests/unit/widget/core.js
index e36459f72..1054bf092 100644
--- a/tests/unit/widget/core.js
+++ b/tests/unit/widget/core.js
@@ -92,6 +92,18 @@ QUnit.test( "element normalization", function( assert ) {
$.ui.testWidget();
} );
+QUnit.test( "contextless construction", function( assert ) {
+ assert.expect( 1 );
+ var testWidget,
+ elem = $( "<div>" );
+
+ $.widget( "ui.testWidget", {} );
+ testWidget = $.ui.testWidget;
+
+ testWidget( {}, elem );
+ assert.ok( true, "No crash" );
+} );
+
QUnit.test( "custom selector expression", function( assert ) {
assert.expect( 1 );
var elem = $( "<div>" ).appendTo( "#qunit-fixture" );
diff --git a/ui/widget.js b/ui/widget.js
index 3b149f11a..04daaa883 100644
--- a/ui/widget.js
+++ b/ui/widget.js
@@ -77,7 +77,7 @@ $.widget = function( name, base, prototype ) {
constructor = $[ namespace ][ name ] = function( options, element ) {
// Allow instantiation without "new" keyword
- if ( !this._createWidget ) {
+ if ( !this || !this._createWidget ) {
return new constructor( options, element );
}