Skip to content

Commit 649f105

Browse files
committed
Dialog: Don't handle overlays on destory if there are not any. Fixed: #9004 - failed in _destroyOverlay when I destroy a modal dialog thau was never opened. Fixed: #9000 Dialog leaves broken event handler after close/destroy in certain cases
1 parent f7f165c commit 649f105

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

tests/unit/dialog/dialog_methods.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ test("init", function() {
3434
});
3535

3636
test("destroy", function() {
37-
expect( 7 );
37+
expect( 17 );
38+
39+
var el, el2;
3840

3941
$( "#dialog1, #form-dialog" ).hide();
4042
domEqual( "#dialog1", function() {
@@ -57,6 +59,35 @@ test("destroy", function() {
5759
domEqual( "#dialog1", function() {
5860
$( "#dialog1" ).dialog().dialog( "destroy" );
5961
});
62+
63+
// Don't throw errors when destroying a never opened modal dialog (#9004)
64+
$( "#dialog1" ).dialog({ autoOpen: false, modal: true }).dialog( "destroy" );
65+
equal( $( ".ui-widget-overlay" ).length, 0, "overlay does not exist" );
66+
equal( $.ui.dialog.overlayInstances, 0, "overlayInstances equals the number of open overlays");
67+
68+
el = $( "#dialog1" ).dialog({ modal: true }),
69+
el2 = $( "#dialog2" ).dialog({ modal: true });
70+
equal( $( ".ui-widget-overlay" ).length, 2, "overlays created when dialogs are open" );
71+
equal( $.ui.dialog.overlayInstances, 2, "overlayInstances equals the number of open overlays" );
72+
el.dialog( "close" );
73+
equal( $( ".ui-widget-overlay" ).length, 1, "overlay remains after closing one dialog" );
74+
equal( $.ui.dialog.overlayInstances, 1, "overlayInstances equals the number of open overlays" );
75+
el.dialog( "destroy" );
76+
equal( $( ".ui-widget-overlay" ).length, 1, "overlay remains after destroying one dialog" );
77+
equal( $.ui.dialog.overlayInstances, 1, "overlayInstances equals the number of open overlays" );
78+
el2.dialog( "destroy" );
79+
equal( $( ".ui-widget-overlay" ).length, 0, "overlays removed when all dialogs are destoryed" );
80+
equal( $.ui.dialog.overlayInstances, 0, "overlayInstances equals the number of open overlays" );
81+
});
82+
83+
asyncTest("#9000: Dialog leaves broken event handler after close/destroy in certain cases", function() {
84+
expect( 1 );
85+
$( "#dialog1" ).dialog({ modal:true }).dialog( "close" ).dialog( "destroy" );
86+
setTimeout(function() {
87+
$( "#favorite-animal" ).focus();
88+
ok( true, "close and destroy modal dialog before its really opened" );
89+
start();
90+
}, 2 );
6091
});
6192

6293
test("#4980: Destroy should place element back in original DOM position", function(){

ui/jquery.ui.dialog.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,11 +704,15 @@ $.widget( "ui.dialog", {
704704
return;
705705
}
706706

707-
$.ui.dialog.overlayInstances--;
708-
if ( !$.ui.dialog.overlayInstances ) {
709-
this._off( this.document, "focusin" );
707+
if ( this.overlay ) {
708+
$.ui.dialog.overlayInstances--;
709+
710+
if ( !$.ui.dialog.overlayInstances ) {
711+
this._off( this.document, "focusin" );
712+
}
713+
this.overlay.remove();
714+
this.overlay = null;
710715
}
711-
this.overlay.remove();
712716
}
713717
});
714718

0 commit comments

Comments
 (0)