3
3
// order is important!
4
4
jQuery . cssExpand = [ "Top" , "Right" , "Bottom" , "Left" ] ;
5
5
6
- var ralpha = / a l p h a \( [ ^ ) ] * \) / i,
6
+ var curCSS , iframe , iframeDoc ,
7
+ ralpha = / a l p h a \( [ ^ ) ] * \) / i,
7
8
ropacity = / o p a c i t y = ( [ ^ ) ] * ) / ,
8
9
// fixed for IE9, see #8346
9
10
rupper = / ( [ A - Z ] | ^ m s ) / g,
10
11
rnumsplit = / ^ ( [ \- + ] ? (?: \d * \. ) ? \d + ) ( .* ) $ / i,
11
12
rnumnonpx = / ^ - ? (?: \d * \. ) ? \d + (? ! p x ) [ ^ \d \s ] + $ / i,
12
13
rrelNum = / ^ ( [ \- + ] ) = ( [ \- + . \d e ] + ) / ,
13
14
rmargin = / ^ m a r g i n / ,
14
-
15
+ elemdisplay = { } ,
15
16
cssShow = { position : "absolute" , visibility : "hidden" , display : "block" } ,
16
17
17
18
cssExpand = jQuery . cssExpand ,
18
19
cssPrefixes = [ "Webkit" , "O" , "Moz" , "ms" ] ,
19
20
rposition = / ^ ( t o p | r i g h t | b o t t o m | l e f t ) $ / ,
20
21
21
- curCSS ;
22
+ eventsToggle = jQuery . fn . toggle ;
22
23
23
24
// return a css property mapped to a potentially vendor prefixed property
24
25
function vendorPropName ( style , name ) {
@@ -43,13 +44,83 @@ function vendorPropName( style, name ) {
43
44
return origName ;
44
45
}
45
46
46
- jQuery . fn . css = function ( name , value ) {
47
- return jQuery . access ( this , function ( elem , name , value ) {
48
- return value !== undefined ?
49
- jQuery . style ( elem , name , value ) :
50
- jQuery . css ( elem , name ) ;
51
- } , name , value , arguments . length > 1 ) ;
52
- } ;
47
+ function showHide ( elements , show ) {
48
+ var elem , display ,
49
+ values = [ ] ,
50
+ index = 0 ,
51
+ length = elements . length ;
52
+
53
+ for ( ; index < length ; index ++ ) {
54
+ elem = elements [ index ] ;
55
+ if ( ! elem . style ) {
56
+ continue ;
57
+ }
58
+ values [ index ] = jQuery . _data ( elem , "olddisplay" ) ;
59
+ if ( show ) {
60
+ // Reset the inline display of this element to learn if it is
61
+ // being hidden by cascaded rules or not
62
+ if ( ! values [ index ] && elem . style . display === "none" ) {
63
+ elem . style . display = "" ;
64
+ }
65
+
66
+ // Set elements which have been overridden with display: none
67
+ // in a stylesheet to whatever the default browser style is
68
+ // for such an element
69
+ if ( ( elem . style . display === "" && curCSS ( elem , "display" ) === "none" ) ||
70
+ ! jQuery . contains ( elem . ownerDocument . documentElement , elem ) ) {
71
+ values [ index ] = jQuery . _data ( elem , "olddisplay" , jQuery . defaultDisplay ( elem . nodeName ) ) ;
72
+ }
73
+ } else {
74
+ display = curCSS ( elem , "display" ) ;
75
+
76
+ if ( ! values [ index ] && display !== "none" ) {
77
+ jQuery . _data ( elem , "olddisplay" , display ) ;
78
+ }
79
+ }
80
+ }
81
+
82
+ // Set the display of most of the elements in a second loop
83
+ // to avoid the constant reflow
84
+ for ( index = 0 ; index < length ; index ++ ) {
85
+ elem = elements [ index ] ;
86
+ if ( ! elem . style ) {
87
+ continue ;
88
+ }
89
+ if ( ! show || elem . style . display === "none" || elem . style . display === "" ) {
90
+ elem . style . display = show ? values [ index ] || "" : "none" ;
91
+ }
92
+ }
93
+
94
+ return elements ;
95
+ }
96
+
97
+ jQuery . fn . extend ( {
98
+ css : function ( name , value ) {
99
+ return jQuery . access ( this , function ( elem , name , value ) {
100
+ return value !== undefined ?
101
+ jQuery . style ( elem , name , value ) :
102
+ jQuery . css ( elem , name ) ;
103
+ } , name , value , arguments . length > 1 ) ;
104
+ } ,
105
+ show : function ( ) {
106
+ return showHide ( this , true ) ;
107
+ } ,
108
+ hide : function ( ) {
109
+ return showHide ( this ) ;
110
+ } ,
111
+ toggle : function ( fn , fn2 ) {
112
+ var bool = typeof fn === "boolean" ;
113
+
114
+ if ( jQuery . isFunction ( fn ) && jQuery . isFunction ( fn2 ) ) {
115
+ return eventsToggle . apply ( this , arguments ) ;
116
+ }
117
+
118
+ return this . each ( function ( ) {
119
+ var state = bool ? fn : jQuery ( this ) . is ( ":hidden" ) ;
120
+ showHide ( [ this ] , state ) ;
121
+ } ) ;
122
+ }
123
+ } ) ;
53
124
54
125
jQuery . extend ( {
55
126
// Add in style property hooks for overriding the default
@@ -200,6 +271,49 @@ jQuery.extend({
200
271
}
201
272
202
273
return ret ;
274
+ } ,
275
+
276
+ // Try to determine the default display value of an element
277
+ defaultDisplay : function ( nodeName ) {
278
+ if ( elemdisplay [ nodeName ] ) {
279
+ return elemdisplay [ nodeName ] ;
280
+ }
281
+
282
+ var elem = jQuery ( "<" + nodeName + ">" ) . appendTo ( document . body ) ,
283
+ display = elem . css ( "display" ) ;
284
+ elem . remove ( ) ;
285
+
286
+ // If the simple way fails,
287
+ // get element's real default display by attaching it to a temp iframe
288
+ if ( display === "none" || display === "" ) {
289
+ // Use the already-created iframe if possible
290
+ iframe = document . body . appendChild (
291
+ iframe || jQuery . extend ( document . createElement ( "iframe" ) , {
292
+ frameBorder : 0 ,
293
+ width : 0 ,
294
+ height : 0
295
+ } )
296
+ ) ;
297
+
298
+ // Create a cacheable copy of the iframe document on first call.
299
+ // IE and Opera will allow us to reuse the iframeDoc without re-writing the fake HTML
300
+ // document to it; WebKit & Firefox won't allow reusing the iframe document.
301
+ if ( ! iframeDoc || ! iframe . createElement ) {
302
+ iframeDoc = ( iframe . contentWindow || iframe . contentDocument ) . document ;
303
+ iframeDoc . write ( "<!doctype html><html><body>" ) ;
304
+ iframeDoc . close ( ) ;
305
+ }
306
+
307
+ elem = iframeDoc . body . appendChild ( iframeDoc . createElement ( nodeName ) ) ;
308
+
309
+ display = curCSS ( elem , "display" ) ;
310
+ document . body . removeChild ( iframe ) ;
311
+ }
312
+
313
+ // Store the correct default display
314
+ elemdisplay [ nodeName ] = display ;
315
+
316
+ return display ;
203
317
}
204
318
} ) ;
205
319
0 commit comments