Skip to content

Commit ed898c6

Browse files
committed
Fix #12148. Let .toggle() call the public .hide() for punching.
There is a slightly shorter way to do this but it's not Closure-friendly.
1 parent 5119b25 commit ed898c6

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/css.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ function vendorPropName( style, name ) {
4343
return origName;
4444
}
4545

46-
function isHidden( elem, el ) {
47-
elem = el || elem;
46+
function isHidden( elem ) {
4847
return jQuery.css( elem, "display" ) === "none" || !jQuery.contains( elem.ownerDocument.documentElement, elem );
4948
}
5049

@@ -111,16 +110,19 @@ jQuery.fn.extend({
111110
hide: function() {
112111
return showHide( this );
113112
},
114-
toggle: function( fn, fn2 ) {
115-
var bool = typeof fn === "boolean";
113+
toggle: function( state, fn2 ) {
114+
var bool = typeof state === "boolean";
116115

117-
if ( jQuery.isFunction( fn ) && jQuery.isFunction( fn2 ) ) {
116+
if ( jQuery.isFunction( state ) && jQuery.isFunction( fn2 ) ) {
118117
return eventsToggle.apply( this, arguments );
119118
}
120119

121120
return this.each(function() {
122-
var state = bool ? fn : jQuery( this ).is(":hidden");
123-
showHide([ this ], state );
121+
if ( bool ? state : isHidden( this ) ) {
122+
jQuery( this ).show();
123+
} else {
124+
jQuery( this ).hide();
125+
}
124126
});
125127
}
126128
});

test/unit/css.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ test( "show() resolves correct default display, detached nodes (#10006)", functi
561561
});
562562

563563
test("toggle()", function() {
564-
expect(6);
564+
expect(7);
565565
var x = jQuery("#foo");
566566
ok( x.is(":visible"), "is visible" );
567567
x.toggle();
@@ -575,6 +575,17 @@ test("toggle()", function() {
575575
ok( x.is(":hidden"), "is hidden" );
576576
x.toggle(true);
577577
ok( x.is(":visible"), "is visible again" );
578+
579+
// Ensure hide() is called when toggled (#12148)
580+
var oldHide = jQuery.fn.hide;
581+
jQuery.fn.hide = function() {
582+
ok( true, name + " method called on toggle" );
583+
return oldHide.apply( this, arguments );
584+
};
585+
x.toggle( name === "show" );
586+
jQuery.fn.hide = oldHide;
587+
588+
578589
});
579590

580591
test("hide hidden elements (bug #7141)", function() {

0 commit comments

Comments
 (0)