Skip to content

Commit 67d7a2e

Browse files
dmethvintimmywil
authored andcommitted
CSS: Make show/hide/toggle methods a module
Unit test changes some uses of .show() and .hide() to .css( "display", ... ), there was already an implicit assumption in several of the existing tests. Fixes gh-2193 Close gh-2648
1 parent e271f66 commit 67d7a2e

File tree

8 files changed

+81
-58
lines changed

8 files changed

+81
-58
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ module.exports = function( grunt ) {
7070
ajax: [ "manipulation/_evalUrl", "event/ajax" ],
7171
callbacks: [ "deferred" ],
7272
css: [ "effects", "dimensions", "offset" ],
73+
"css/showHide": [ "effects" ],
7374
sizzle: [ "css/hiddenVisibleSelectors", "effects/animatedSelector" ]
7475
}
7576
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ Some example modules that can be excluded are:
8181
- **ajax/xhr**: The XMLHTTPRequest AJAX transport only.
8282
- **ajax/script**: The `<script>` AJAX transport only; used to retrieve scripts.
8383
- **ajax/jsonp**: The JSONP AJAX transport only; depends on the ajax/script transport.
84-
- **css**: The `.css()` method plus non-animated `.show()`, `.hide()` and `.toggle()`. Also removes **all** modules depending on css (including **effects**, **dimensions**, and **offset**).
84+
- **css**: The `.css()` method. Also removes **all** modules depending on css (including **effects**, **dimensions**, and **offset**).
85+
- **css/showHide**: Non-animated `.show()`, `.hide()` and `.toggle()`; can be excluded if you use classes or explicit `.css()` calls to set the `display` property. Also removes the **effects** module.
8586
- **deprecated**: Methods documented as deprecated but not yet removed.
8687
- **dimensions**: The `.width()` and `.height()` methods, including `inner-` and `outer-` variations.
8788
- **effects**: The `.animate()` method and its shorthands such as `.slideUp()` or `.hide("slow")`.

src/css.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ define( [
1414
"./css/adjustCSS",
1515
"./css/addGetHookIf",
1616
"./css/support",
17-
"./css/showHide",
1817

1918
"./core/init",
2019
"./core/ready",
2120
"./selector" // contains
2221
], function( jQuery, pnum, access, rmargin, document, rcssNum, rnumnonpx, cssExpand,
23-
isHidden, getStyles, swap, curCSS, adjustCSS, addGetHookIf, support, showHide ) {
22+
isHidden, getStyles, swap, curCSS, adjustCSS, addGetHookIf, support ) {
2423

2524
var
2625

@@ -401,25 +400,6 @@ jQuery.fn.extend( {
401400
jQuery.style( elem, name, value ) :
402401
jQuery.css( elem, name );
403402
}, name, value, arguments.length > 1 );
404-
},
405-
show: function() {
406-
return showHide( this, true );
407-
},
408-
hide: function() {
409-
return showHide( this );
410-
},
411-
toggle: function( state ) {
412-
if ( typeof state === "boolean" ) {
413-
return state ? this.show() : this.hide();
414-
}
415-
416-
return this.each( function() {
417-
if ( isHidden( this ) ) {
418-
jQuery( this ).show();
419-
} else {
420-
jQuery( this ).hide();
421-
}
422-
} );
423403
}
424404
} );
425405

src/css/showHide.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
define( [
2-
"../data/var/dataPriv"
3-
], function( dataPriv ) {
2+
"../data/var/dataPriv",
3+
"../css/var/isHidden"
4+
], function( dataPriv, isHidden ) {
45

56
function showHide( elements, show ) {
67
var display, elem,
@@ -43,6 +44,26 @@ function showHide( elements, show ) {
4344
return elements;
4445
}
4546

46-
return showHide;
47+
jQuery.fn.extend( {
48+
show: function() {
49+
return showHide( this, true );
50+
},
51+
hide: function() {
52+
return showHide( this );
53+
},
54+
toggle: function( state ) {
55+
if ( typeof state === "boolean" ) {
56+
return state ? this.show() : this.hide();
57+
}
58+
59+
return this.each( function() {
60+
if ( isHidden( this ) ) {
61+
jQuery( this ).show();
62+
} else {
63+
jQuery( this ).hide();
64+
}
65+
} );
66+
}
67+
} );
4768

4869
} );

test/unit/basic.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,19 @@ QUnit.test( "attributes", function( assert ) {
5454

5555
if ( jQuery.css ) {
5656
QUnit.test( "css", function( assert ) {
57-
assert.expect( 3 );
57+
assert.expect( 1 );
5858

5959
var div = jQuery( "<div/>" ).appendTo( "#qunit-fixture" );
6060

6161
assert.strictEqual( div.css( "width", "50px" ).css( "width" ), "50px", ".css getter/setter" );
62+
} );
63+
}
64+
65+
if ( jQuery.fn.show && jQuery.fn.hide ) {
66+
QUnit.test( "show/hide", function( assert ) {
67+
assert.expect( 2 );
68+
69+
var div = jQuery( "<div/>" ).appendTo( "#qunit-fixture" );
6270

6371
div.hide();
6472
assert.strictEqual( div.css( "display" ), "none", "div hidden" );

test/unit/css.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,9 @@ QUnit.test( "css(Object) where values are Functions with incoming values", funct
471471
jQuery( "#cssFunctionTest" ).remove();
472472
} );
473473

474+
// .show(), .hide(), can be excluded from the build
475+
if ( jQuery.fn.show && jQuery.fn.hide ) {
476+
474477
QUnit.test( "show(); hide()", function( assert ) {
475478

476479
assert.expect( 4 );
@@ -636,6 +639,35 @@ QUnit.test( "show() resolves correct default display for detached nodes", functi
636639
span.remove();
637640
} );
638641

642+
QUnit.test( "hide hidden elements (bug #7141)", function( assert ) {
643+
assert.expect( 3 );
644+
645+
var div = jQuery( "<div style='display:none'></div>" ).appendTo( "#qunit-fixture" );
646+
assert.equal( div.css( "display" ), "none", "Element is hidden by default" );
647+
div.hide();
648+
assert.ok( !jQuery._data( div, "olddisplay" ), "olddisplay is undefined after hiding an already-hidden element" );
649+
div.show();
650+
assert.equal( div.css( "display" ), "block", "Show a double-hidden element" );
651+
652+
div.remove();
653+
} );
654+
655+
QUnit.test( "show() after hide() should always set display to initial value (#14750)", function( assert ) {
656+
assert.expect( 1 );
657+
658+
var div = jQuery( "<div />" ),
659+
fixture = jQuery( "#qunit-fixture" );
660+
661+
fixture.append( div );
662+
663+
div.css( "display", "inline" ).hide().show().css( "display", "list-item" ).hide().show();
664+
assert.equal( div.css( "display" ), "list-item", "should get last set display value" );
665+
} );
666+
667+
}
668+
669+
if ( jQuery.fn.toggle ) {
670+
639671
QUnit.test( "toggle()", function( assert ) {
640672
assert.expect( 9 );
641673
var div, oldHide,
@@ -669,18 +701,7 @@ QUnit.test( "toggle()", function( assert ) {
669701
jQuery.fn.hide = oldHide;
670702
} );
671703

672-
QUnit.test( "hide hidden elements (bug #7141)", function( assert ) {
673-
assert.expect( 3 );
674-
675-
var div = jQuery( "<div style='display:none'></div>" ).appendTo( "#qunit-fixture" );
676-
assert.equal( div.css( "display" ), "none", "Element is hidden by default" );
677-
div.hide();
678-
assert.ok( !jQuery._data( div, "olddisplay" ), "olddisplay is undefined after hiding an already-hidden element" );
679-
div.show();
680-
assert.equal( div.css( "display" ), "block", "Show a double-hidden element" );
681-
682-
div.remove();
683-
} );
704+
}
684705

685706
QUnit.test( "jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function( assert ) {
686707
assert.expect( 4 );
@@ -1128,18 +1149,6 @@ QUnit.test(
11281149
}
11291150
);
11301151

1131-
QUnit.test( "show() after hide() should always set display to initial value (#14750)", function( assert ) {
1132-
assert.expect( 1 );
1133-
1134-
var div = jQuery( "<div />" ),
1135-
fixture = jQuery( "#qunit-fixture" );
1136-
1137-
fixture.append( div );
1138-
1139-
div.css( "display", "inline" ).hide().show().css( "display", "list-item" ).hide().show();
1140-
assert.equal( div.css( "display" ), "list-item", "should get last set display value" );
1141-
} );
1142-
11431152
// Support: IE < 11
11441153
// We have to jump through the hoops here in order to test work with "order" CSS property,
11451154
// that some browsers do not support. This test is not, strictly speaking, correct,

test/unit/dimensions.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ function testWidth( val, assert ) {
3535
$div = jQuery( "#nothiddendiv" );
3636
$div.width( val( 30 ) );
3737
assert.equal( $div.width(), 30, "Test set to 30 correctly" );
38-
$div.hide();
38+
$div.css( "display", "none" );
3939
assert.equal( $div.width(), 30, "Test hidden div" );
40-
$div.show();
40+
$div.css( "display", "" );
4141
$div.width( val( -1 ) ); // handle negative numbers by setting to 0 #11604
4242
assert.equal( $div.width(), 0, "Test negative width normalized to 0" );
4343
$div.css( "padding", "20px" );
@@ -88,9 +88,9 @@ function testHeight( val, assert ) {
8888
$div = jQuery( "#nothiddendiv" );
8989
$div.height( val( 30 ) );
9090
assert.equal( $div.height(), 30, "Test set to 30 correctly" );
91-
$div.hide();
91+
$div.css( "display", "none" );
9292
assert.equal( $div.height(), 30, "Test hidden div" );
93-
$div.show();
93+
$div.css( "display", "" );
9494
$div.height( val( -1 ) ); // handle negative numbers by setting to 0 #11604
9595
assert.equal( $div.height(), 0, "Test negative height normalized to 0" );
9696
$div.css( "padding", "20px" );
@@ -153,7 +153,7 @@ QUnit.test( "innerWidth()", function( assert ) {
153153
assert.equal( $div.innerWidth(), 30, "Test with margin and border" );
154154
$div.css( "padding", "20px" );
155155
assert.equal( $div.innerWidth(), 70, "Test with margin, border and padding" );
156-
$div.hide();
156+
$div.css( "display", "none" );
157157
assert.equal( $div.innerWidth(), 70, "Test hidden div" );
158158

159159
// reset styles
@@ -188,7 +188,7 @@ QUnit.test( "innerHeight()", function( assert ) {
188188
assert.equal( $div.innerHeight(), 30, "Test with margin and border" );
189189
$div.css( "padding", "20px" );
190190
assert.equal( $div.innerHeight(), 70, "Test with margin, border and padding" );
191-
$div.hide();
191+
$div.css( "display", "none" );
192192
assert.equal( $div.innerHeight(), 70, "Test hidden div" );
193193

194194
// reset styles
@@ -227,7 +227,7 @@ QUnit.test( "outerWidth()", function( assert ) {
227227
assert.equal( $div.outerWidth(), 74, "Test with padding, border and margin without margin option" );
228228
$div.css( "position", "absolute" );
229229
assert.equal( $div.outerWidth( true ), 94, "Test with padding, border and margin with margin option" );
230-
$div.hide();
230+
$div.css( "display", "none" );
231231
assert.equal( $div.outerWidth( true ), 94, "Test hidden div with padding, border and margin with margin option" );
232232

233233
// reset styles
@@ -383,7 +383,7 @@ QUnit.test( "outerHeight()", function( assert ) {
383383
$div.css( "margin", "10px" );
384384
assert.equal( $div.outerHeight(), 74, "Test with padding, border and margin without margin option" );
385385
assert.equal( $div.outerHeight( true ), 94, "Test with padding, border and margin with margin option" );
386-
$div.hide();
386+
$div.css( "display", "none" );
387387
assert.equal( $div.outerHeight( true ), 94, "Test hidden div with padding, border and margin with margin option" );
388388

389389
// reset styles

test/unit/queue.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ QUnit.asyncTest( "fn.promise( \"queue\" ) - called whenever last queue function
231231
foo.dequeue( "queue" );
232232
} );
233233

234+
if ( jQuery.fn.animate ) {
235+
234236
QUnit.asyncTest( "fn.promise( \"queue\" ) - waits for animation to complete before resolving", 2, function( assert ) {
235237
var foo = jQuery( "#foo" ),
236238
test = 1;
@@ -251,6 +253,7 @@ QUnit.asyncTest( "fn.promise( \"queue\" ) - waits for animation to complete befo
251253
} );
252254

253255
} );
256+
}
254257

255258
QUnit.test( ".promise(obj)", function( assert ) {
256259
assert.expect( 2 );

0 commit comments

Comments
 (0)