UNPKG

12.7 kBJavaScriptView Raw
1define([
2 "./core",
3 "./var/pnum",
4 "./core/access",
5 "./css/var/rmargin",
6 "./css/var/rnumnonpx",
7 "./css/var/cssExpand",
8 "./css/var/isHidden",
9 "./css/var/getStyles",
10 "./css/curCSS",
11 "./css/defaultDisplay",
12 "./css/addGetHookIf",
13 "./css/support",
14 "./data/var/data_priv",
15
16 "./core/init",
17 "./css/swap",
18 "./core/ready",
19 "./selector" // contains
20], function( jQuery, pnum, access, rmargin, rnumnonpx, cssExpand, isHidden,
21 getStyles, curCSS, defaultDisplay, addGetHookIf, support, data_priv ) {
22
23var
24 // swappable if display is none or starts with table except "table", "table-cell", or "table-caption"
25 // see here for display values: https://developer.mozilla.org/en-US/docs/CSS/display
26 rdisplayswap = /^(none|table(?!-c[ea]).+)/,
27 rnumsplit = new RegExp( "^(" + pnum + ")(.*)$", "i" ),
28 rrelNum = new RegExp( "^([+-])=(" + pnum + ")", "i" ),
29
30 cssShow = { position: "absolute", visibility: "hidden", display: "block" },
31 cssNormalTransform = {
32 letterSpacing: "0",
33 fontWeight: "400"
34 },
35
36 cssPrefixes = [ "Webkit", "O", "Moz", "ms" ];
37
38// return a css property mapped to a potentially vendor prefixed property
39function vendorPropName( style, name ) {
40
41 // shortcut for names that are not vendor prefixed
42 if ( name in style ) {
43 return name;
44 }
45
46 // check for vendor prefixed names
47 var capName = name[0].toUpperCase() + name.slice(1),
48 origName = name,
49 i = cssPrefixes.length;
50
51 while ( i-- ) {
52 name = cssPrefixes[ i ] + capName;
53 if ( name in style ) {
54 return name;
55 }
56 }
57
58 return origName;
59}
60
61function setPositiveNumber( elem, value, subtract ) {
62 var matches = rnumsplit.exec( value );
63 return matches ?
64 // Guard against undefined "subtract", e.g., when used as in cssHooks
65 Math.max( 0, matches[ 1 ] - ( subtract || 0 ) ) + ( matches[ 2 ] || "px" ) :
66 value;
67}
68
69function augmentWidthOrHeight( elem, name, extra, isBorderBox, styles ) {
70 var i = extra === ( isBorderBox ? "border" : "content" ) ?
71 // If we already have the right measurement, avoid augmentation
72 4 :
73 // Otherwise initialize for horizontal or vertical properties
74 name === "width" ? 1 : 0,
75
76 val = 0;
77
78 for ( ; i < 4; i += 2 ) {
79 // both box models exclude margin, so add it if we want it
80 if ( extra === "margin" ) {
81 val += jQuery.css( elem, extra + cssExpand[ i ], true, styles );
82 }
83
84 if ( isBorderBox ) {
85 // border-box includes padding, so remove it if we want content
86 if ( extra === "content" ) {
87 val -= jQuery.css( elem, "padding" + cssExpand[ i ], true, styles );
88 }
89
90 // at this point, extra isn't border nor margin, so remove border
91 if ( extra !== "margin" ) {
92 val -= jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
93 }
94 } else {
95 // at this point, extra isn't content, so add padding
96 val += jQuery.css( elem, "padding" + cssExpand[ i ], true, styles );
97
98 // at this point, extra isn't content nor padding, so add border
99 if ( extra !== "padding" ) {
100 val += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
101 }
102 }
103 }
104
105 return val;
106}
107
108function getWidthOrHeight( elem, name, extra ) {
109
110 // Start with offset property, which is equivalent to the border-box value
111 var valueIsBorderBox = true,
112 val = name === "width" ? elem.offsetWidth : elem.offsetHeight,
113 styles = getStyles( elem ),
114 isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box";
115
116 // some non-html elements return undefined for offsetWidth, so check for null/undefined
117 // svg - https://bugzilla.mozilla.org/show_bug.cgi?id=649285
118 // MathML - https://bugzilla.mozilla.org/show_bug.cgi?id=491668
119 if ( val <= 0 || val == null ) {
120 // Fall back to computed then uncomputed css if necessary
121 val = curCSS( elem, name, styles );
122 if ( val < 0 || val == null ) {
123 val = elem.style[ name ];
124 }
125
126 // Computed unit is not pixels. Stop here and return.
127 if ( rnumnonpx.test(val) ) {
128 return val;
129 }
130
131 // we need the check for style in case a browser which returns unreliable values
132 // for getComputedStyle silently falls back to the reliable elem.style
133 valueIsBorderBox = isBorderBox &&
134 ( support.boxSizingReliable() || val === elem.style[ name ] );
135
136 // Normalize "", auto, and prepare for extra
137 val = parseFloat( val ) || 0;
138 }
139
140 // use the active box-sizing model to add/subtract irrelevant styles
141 return ( val +
142 augmentWidthOrHeight(
143 elem,
144 name,
145 extra || ( isBorderBox ? "border" : "content" ),
146 valueIsBorderBox,
147 styles
148 )
149 ) + "px";
150}
151
152function showHide( elements, show ) {
153 var display, elem, hidden,
154 values = [],
155 index = 0,
156 length = elements.length;
157
158 for ( ; index < length; index++ ) {
159 elem = elements[ index ];
160 if ( !elem.style ) {
161 continue;
162 }
163
164 values[ index ] = data_priv.get( elem, "olddisplay" );
165 display = elem.style.display;
166 if ( show ) {
167 // Reset the inline display of this element to learn if it is
168 // being hidden by cascaded rules or not
169 if ( !values[ index ] && display === "none" ) {
170 elem.style.display = "";
171 }
172
173 // Set elements which have been overridden with display: none
174 // in a stylesheet to whatever the default browser style is
175 // for such an element
176 if ( elem.style.display === "" && isHidden( elem ) ) {
177 values[ index ] = data_priv.access( elem, "olddisplay", defaultDisplay(elem.nodeName) );
178 }
179 } else {
180 hidden = isHidden( elem );
181
182 if ( display !== "none" || !hidden ) {
183 data_priv.set( elem, "olddisplay", hidden ? display : jQuery.css( elem, "display" ) );
184 }
185 }
186 }
187
188 // Set the display of most of the elements in a second loop
189 // to avoid the constant reflow
190 for ( index = 0; index < length; index++ ) {
191 elem = elements[ index ];
192 if ( !elem.style ) {
193 continue;
194 }
195 if ( !show || elem.style.display === "none" || elem.style.display === "" ) {
196 elem.style.display = show ? values[ index ] || "" : "none";
197 }
198 }
199
200 return elements;
201}
202
203jQuery.extend({
204 // Add in style property hooks for overriding the default
205 // behavior of getting and setting a style property
206 cssHooks: {
207 opacity: {
208 get: function( elem, computed ) {
209 if ( computed ) {
210 // We should always get a number back from opacity
211 var ret = curCSS( elem, "opacity" );
212 return ret === "" ? "1" : ret;
213 }
214 }
215 }
216 },
217
218 // Don't automatically add "px" to these possibly-unitless properties
219 cssNumber: {
220 "columnCount": true,
221 "fillOpacity": true,
222 "flexGrow": true,
223 "flexShrink": true,
224 "fontWeight": true,
225 "lineHeight": true,
226 "opacity": true,
227 "order": true,
228 "orphans": true,
229 "widows": true,
230 "zIndex": true,
231 "zoom": true
232 },
233
234 // Add in properties whose names you wish to fix before
235 // setting or getting the value
236 cssProps: {
237 // normalize float css property
238 "float": "cssFloat"
239 },
240
241 // Get and set the style property on a DOM Node
242 style: function( elem, name, value, extra ) {
243 // Don't set styles on text and comment nodes
244 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style ) {
245 return;
246 }
247
248 // Make sure that we're working with the right name
249 var ret, type, hooks,
250 origName = jQuery.camelCase( name ),
251 style = elem.style;
252
253 name = jQuery.cssProps[ origName ] || ( jQuery.cssProps[ origName ] = vendorPropName( style, origName ) );
254
255 // gets hook for the prefixed version
256 // followed by the unprefixed version
257 hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
258
259 // Check if we're setting a value
260 if ( value !== undefined ) {
261 type = typeof value;
262
263 // convert relative number strings (+= or -=) to relative numbers. #7345
264 if ( type === "string" && (ret = rrelNum.exec( value )) ) {
265 value = ( ret[1] + 1 ) * ret[2] + parseFloat( jQuery.css( elem, name ) );
266 // Fixes bug #9237
267 type = "number";
268 }
269
270 // Make sure that null and NaN values aren't set. See: #7116
271 if ( value == null || value !== value ) {
272 return;
273 }
274
275 // If a number was passed in, add 'px' to the (except for certain CSS properties)
276 if ( type === "number" && !jQuery.cssNumber[ origName ] ) {
277 value += "px";
278 }
279
280 // Fixes #8908, it can be done more correctly by specifying setters in cssHooks,
281 // but it would mean to define eight (for every problematic property) identical functions
282 if ( !support.clearCloneStyle && value === "" && name.indexOf( "background" ) === 0 ) {
283 style[ name ] = "inherit";
284 }
285
286 // If a hook was provided, use that value, otherwise just set the specified value
287 if ( !hooks || !("set" in hooks) || (value = hooks.set( elem, value, extra )) !== undefined ) {
288 style[ name ] = value;
289 }
290
291 } else {
292 // If a hook was provided get the non-computed value from there
293 if ( hooks && "get" in hooks && (ret = hooks.get( elem, false, extra )) !== undefined ) {
294 return ret;
295 }
296
297 // Otherwise just get the value from the style object
298 return style[ name ];
299 }
300 },
301
302 css: function( elem, name, extra, styles ) {
303 var val, num, hooks,
304 origName = jQuery.camelCase( name );
305
306 // Make sure that we're working with the right name
307 name = jQuery.cssProps[ origName ] || ( jQuery.cssProps[ origName ] = vendorPropName( elem.style, origName ) );
308
309 // gets hook for the prefixed version
310 // followed by the unprefixed version
311 hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
312
313 // If a hook was provided get the computed value from there
314 if ( hooks && "get" in hooks ) {
315 val = hooks.get( elem, true, extra );
316 }
317
318 // Otherwise, if a way to get the computed value exists, use that
319 if ( val === undefined ) {
320 val = curCSS( elem, name, styles );
321 }
322
323 //convert "normal" to computed value
324 if ( val === "normal" && name in cssNormalTransform ) {
325 val = cssNormalTransform[ name ];
326 }
327
328 // Return, converting to number if forced or a qualifier was provided and val looks numeric
329 if ( extra === "" || extra ) {
330 num = parseFloat( val );
331 return extra === true || jQuery.isNumeric( num ) ? num || 0 : val;
332 }
333 return val;
334 }
335});
336
337jQuery.each([ "height", "width" ], function( i, name ) {
338 jQuery.cssHooks[ name ] = {
339 get: function( elem, computed, extra ) {
340 if ( computed ) {
341 // certain elements can have dimension info if we invisibly show them
342 // however, it must have a current display style that would benefit from this
343 return rdisplayswap.test( jQuery.css( elem, "display" ) ) && elem.offsetWidth === 0 ?
344 jQuery.swap( elem, cssShow, function() {
345 return getWidthOrHeight( elem, name, extra );
346 }) :
347 getWidthOrHeight( elem, name, extra );
348 }
349 },
350
351 set: function( elem, value, extra ) {
352 var styles = extra && getStyles( elem );
353 return setPositiveNumber( elem, value, extra ?
354 augmentWidthOrHeight(
355 elem,
356 name,
357 extra,
358 jQuery.css( elem, "boxSizing", false, styles ) === "border-box",
359 styles
360 ) : 0
361 );
362 }
363 };
364});
365
366// Support: Android 2.3
367jQuery.cssHooks.marginRight = addGetHookIf( support.reliableMarginRight,
368 function( elem, computed ) {
369 if ( computed ) {
370 // WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
371 // Work around by temporarily setting element display to inline-block
372 return jQuery.swap( elem, { "display": "inline-block" },
373 curCSS, [ elem, "marginRight" ] );
374 }
375 }
376);
377
378// These hooks are used by animate to expand properties
379jQuery.each({
380 margin: "",
381 padding: "",
382 border: "Width"
383}, function( prefix, suffix ) {
384 jQuery.cssHooks[ prefix + suffix ] = {
385 expand: function( value ) {
386 var i = 0,
387 expanded = {},
388
389 // assumes a single number if not a string
390 parts = typeof value === "string" ? value.split(" ") : [ value ];
391
392 for ( ; i < 4; i++ ) {
393 expanded[ prefix + cssExpand[ i ] + suffix ] =
394 parts[ i ] || parts[ i - 2 ] || parts[ 0 ];
395 }
396
397 return expanded;
398 }
399 };
400
401 if ( !rmargin.test( prefix ) ) {
402 jQuery.cssHooks[ prefix + suffix ].set = setPositiveNumber;
403 }
404});
405
406jQuery.fn.extend({
407 css: function( name, value ) {
408 return access( this, function( elem, name, value ) {
409 var styles, len,
410 map = {},
411 i = 0;
412
413 if ( jQuery.isArray( name ) ) {
414 styles = getStyles( elem );
415 len = name.length;
416
417 for ( ; i < len; i++ ) {
418 map[ name[ i ] ] = jQuery.css( elem, name[ i ], false, styles );
419 }
420
421 return map;
422 }
423
424 return value !== undefined ?
425 jQuery.style( elem, name, value ) :
426 jQuery.css( elem, name );
427 }, name, value, arguments.length > 1 );
428 },
429 show: function() {
430 return showHide( this, true );
431 },
432 hide: function() {
433 return showHide( this );
434 },
435 toggle: function( state ) {
436 if ( typeof state === "boolean" ) {
437 return state ? this.show() : this.hide();
438 }
439
440 return this.each(function() {
441 if ( isHidden( this ) ) {
442 jQuery( this ).show();
443 } else {
444 jQuery( this ).hide();
445 }
446 });
447 }
448});
449
450return jQuery;
451});