From ed898c62c8d3e8c2091592976c181ee4142287ac Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Wed, 25 Jul 2012 21:24:49 -0400 Subject: [PATCH] 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. --- src/css.js | 16 +++++++++------- test/unit/css.js | 13 ++++++++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/css.js b/src/css.js index 57edd1507..744e7a545 100644 --- a/src/css.js +++ b/src/css.js @@ -43,8 +43,7 @@ function vendorPropName( style, name ) { return origName; } -function isHidden( elem, el ) { - elem = el || elem; +function isHidden( elem ) { return jQuery.css( elem, "display" ) === "none" || !jQuery.contains( elem.ownerDocument.documentElement, elem ); } @@ -111,16 +110,19 @@ jQuery.fn.extend({ hide: function() { return showHide( this ); }, - toggle: function( fn, fn2 ) { - var bool = typeof fn === "boolean"; + toggle: function( state, fn2 ) { + var bool = typeof state === "boolean"; - if ( jQuery.isFunction( fn ) && jQuery.isFunction( fn2 ) ) { + if ( jQuery.isFunction( state ) && jQuery.isFunction( fn2 ) ) { return eventsToggle.apply( this, arguments ); } return this.each(function() { - var state = bool ? fn : jQuery( this ).is(":hidden"); - showHide([ this ], state ); + if ( bool ? state : isHidden( this ) ) { + jQuery( this ).show(); + } else { + jQuery( this ).hide(); + } }); } }); diff --git a/test/unit/css.js b/test/unit/css.js index 45aee9f06..c2142052a 100644 --- a/test/unit/css.js +++ b/test/unit/css.js @@ -561,7 +561,7 @@ test( "show() resolves correct default display, detached nodes (#10006)", functi }); test("toggle()", function() { - expect(6); + expect(7); var x = jQuery("#foo"); ok( x.is(":visible"), "is visible" ); x.toggle(); @@ -575,6 +575,17 @@ test("toggle()", function() { ok( x.is(":hidden"), "is hidden" ); x.toggle(true); ok( x.is(":visible"), "is visible again" ); + + // Ensure hide() is called when toggled (#12148) + var oldHide = jQuery.fn.hide; + jQuery.fn.hide = function() { + ok( true, name + " method called on toggle" ); + return oldHide.apply( this, arguments ); + }; + x.toggle( name === "show" ); + jQuery.fn.hide = oldHide; + + }); test("hide hidden elements (bug #7141)", function() { -- 2.39.5