comparison default/node_modules/jquery/src/css.js @ 0:1d038bc9b3d2 default tip

Up:default
author Liny <dev@neowd.com>
date Sat, 31 May 2025 09:21:51 +0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1d038bc9b3d2
1 define( [
2 "./core",
3 "./var/pnum",
4 "./core/access",
5 "./core/camelCase",
6 "./var/document",
7 "./var/rcssNum",
8 "./css/var/rnumnonpx",
9 "./css/var/cssExpand",
10 "./css/var/getStyles",
11 "./css/var/swap",
12 "./css/curCSS",
13 "./css/adjustCSS",
14 "./css/addGetHookIf",
15 "./css/support",
16
17 "./core/init",
18 "./core/ready",
19 "./selector" // contains
20 ], function( jQuery, pnum, access, camelCase, document, rcssNum, rnumnonpx, cssExpand,
21 getStyles, swap, curCSS, adjustCSS, addGetHookIf, support ) {
22
23 "use strict";
24
25 var
26
27 // Swappable if display is none or starts with table
28 // except "table", "table-cell", or "table-caption"
29 // See here for display values: https://developer.mozilla.org/en-US/docs/CSS/display
30 rdisplayswap = /^(none|table(?!-c[ea]).+)/,
31 rcustomProp = /^--/,
32 cssShow = { position: "absolute", visibility: "hidden", display: "block" },
33 cssNormalTransform = {
34 letterSpacing: "0",
35 fontWeight: "400"
36 },
37
38 cssPrefixes = [ "Webkit", "Moz", "ms" ],
39 emptyStyle = document.createElement( "div" ).style;
40
41 // Return a css property mapped to a potentially vendor prefixed property
42 function vendorPropName( name ) {
43
44 // Shortcut for names that are not vendor prefixed
45 if ( name in emptyStyle ) {
46 return name;
47 }
48
49 // Check for vendor prefixed names
50 var capName = name[ 0 ].toUpperCase() + name.slice( 1 ),
51 i = cssPrefixes.length;
52
53 while ( i-- ) {
54 name = cssPrefixes[ i ] + capName;
55 if ( name in emptyStyle ) {
56 return name;
57 }
58 }
59 }
60
61 // Return a property mapped along what jQuery.cssProps suggests or to
62 // a vendor prefixed property.
63 function finalPropName( name ) {
64 var ret = jQuery.cssProps[ name ];
65 if ( !ret ) {
66 ret = jQuery.cssProps[ name ] = vendorPropName( name ) || name;
67 }
68 return ret;
69 }
70
71 function setPositiveNumber( elem, value, subtract ) {
72
73 // Any relative (+/-) values have already been
74 // normalized at this point
75 var matches = rcssNum.exec( value );
76 return matches ?
77
78 // Guard against undefined "subtract", e.g., when used as in cssHooks
79 Math.max( 0, matches[ 2 ] - ( subtract || 0 ) ) + ( matches[ 3 ] || "px" ) :
80 value;
81 }
82
83 function boxModelAdjustment( elem, dimension, box, isBorderBox, styles, computedVal ) {
84 var i = dimension === "width" ? 1 : 0,
85 extra = 0,
86 delta = 0;
87
88 // Adjustment may not be necessary
89 if ( box === ( isBorderBox ? "border" : "content" ) ) {
90 return 0;
91 }
92
93 for ( ; i < 4; i += 2 ) {
94
95 // Both box models exclude margin
96 if ( box === "margin" ) {
97 delta += jQuery.css( elem, box + cssExpand[ i ], true, styles );
98 }
99
100 // If we get here with a content-box, we're seeking "padding" or "border" or "margin"
101 if ( !isBorderBox ) {
102
103 // Add padding
104 delta += jQuery.css( elem, "padding" + cssExpand[ i ], true, styles );
105
106 // For "border" or "margin", add border
107 if ( box !== "padding" ) {
108 delta += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
109
110 // But still keep track of it otherwise
111 } else {
112 extra += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
113 }
114
115 // If we get here with a border-box (content + padding + border), we're seeking "content" or
116 // "padding" or "margin"
117 } else {
118
119 // For "content", subtract padding
120 if ( box === "content" ) {
121 delta -= jQuery.css( elem, "padding" + cssExpand[ i ], true, styles );
122 }
123
124 // For "content" or "padding", subtract border
125 if ( box !== "margin" ) {
126 delta -= jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
127 }
128 }
129 }
130
131 // Account for positive content-box scroll gutter when requested by providing computedVal
132 if ( !isBorderBox && computedVal >= 0 ) {
133
134 // offsetWidth/offsetHeight is a rounded sum of content, padding, scroll gutter, and border
135 // Assuming integer scroll gutter, subtract the rest and round down
136 delta += Math.max( 0, Math.ceil(
137 elem[ "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 ) ] -
138 computedVal -
139 delta -
140 extra -
141 0.5
142 ) );
143 }
144
145 return delta;
146 }
147
148 function getWidthOrHeight( elem, dimension, extra ) {
149
150 // Start with computed style
151 var styles = getStyles( elem ),
152 val = curCSS( elem, dimension, styles ),
153 isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box",
154 valueIsBorderBox = isBorderBox;
155
156 // Support: Firefox <=54
157 // Return a confounding non-pixel value or feign ignorance, as appropriate.
158 if ( rnumnonpx.test( val ) ) {
159 if ( !extra ) {
160 return val;
161 }
162 val = "auto";
163 }
164
165 // Check for style in case a browser which returns unreliable values
166 // for getComputedStyle silently falls back to the reliable elem.style
167 valueIsBorderBox = valueIsBorderBox &&
168 ( support.boxSizingReliable() || val === elem.style[ dimension ] );
169
170 // Fall back to offsetWidth/offsetHeight when value is "auto"
171 // This happens for inline elements with no explicit setting (gh-3571)
172 // Support: Android <=4.1 - 4.3 only
173 // Also use offsetWidth/offsetHeight for misreported inline dimensions (gh-3602)
174 if ( val === "auto" ||
175 !parseFloat( val ) && jQuery.css( elem, "display", false, styles ) === "inline" ) {
176
177 val = elem[ "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 ) ];
178
179 // offsetWidth/offsetHeight provide border-box values
180 valueIsBorderBox = true;
181 }
182
183 // Normalize "" and auto
184 val = parseFloat( val ) || 0;
185
186 // Adjust for the element's box model
187 return ( val +
188 boxModelAdjustment(
189 elem,
190 dimension,
191 extra || ( isBorderBox ? "border" : "content" ),
192 valueIsBorderBox,
193 styles,
194
195 // Provide the current computed size to request scroll gutter calculation (gh-3589)
196 val
197 )
198 ) + "px";
199 }
200
201 jQuery.extend( {
202
203 // Add in style property hooks for overriding the default
204 // behavior of getting and setting a style property
205 cssHooks: {
206 opacity: {
207 get: function( elem, computed ) {
208 if ( computed ) {
209
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 "animationIterationCount": true,
221 "columnCount": true,
222 "fillOpacity": true,
223 "flexGrow": true,
224 "flexShrink": true,
225 "fontWeight": true,
226 "lineHeight": true,
227 "opacity": true,
228 "order": true,
229 "orphans": true,
230 "widows": true,
231 "zIndex": true,
232 "zoom": true
233 },
234
235 // Add in properties whose names you wish to fix before
236 // setting or getting the value
237 cssProps: {},
238
239 // Get and set the style property on a DOM Node
240 style: function( elem, name, value, extra ) {
241
242 // Don't set styles on text and comment nodes
243 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style ) {
244 return;
245 }
246
247 // Make sure that we're working with the right name
248 var ret, type, hooks,
249 origName = camelCase( name ),
250 isCustomProp = rcustomProp.test( name ),
251 style = elem.style;
252
253 // Make sure that we're working with the right name. We don't
254 // want to query the value if it is a CSS custom property
255 // since they are user-defined.
256 if ( !isCustomProp ) {
257 name = finalPropName( origName );
258 }
259
260 // Gets hook for the prefixed version, then unprefixed version
261 hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
262
263 // Check if we're setting a value
264 if ( value !== undefined ) {
265 type = typeof value;
266
267 // Convert "+=" or "-=" to relative numbers (#7345)
268 if ( type === "string" && ( ret = rcssNum.exec( value ) ) && ret[ 1 ] ) {
269 value = adjustCSS( elem, name, ret );
270
271 // Fixes bug #9237
272 type = "number";
273 }
274
275 // Make sure that null and NaN values aren't set (#7116)
276 if ( value == null || value !== value ) {
277 return;
278 }
279
280 // If a number was passed in, add the unit (except for certain CSS properties)
281 if ( type === "number" ) {
282 value += ret && ret[ 3 ] || ( jQuery.cssNumber[ origName ] ? "" : "px" );
283 }
284
285 // background-* props affect original clone's values
286 if ( !support.clearCloneStyle && value === "" && name.indexOf( "background" ) === 0 ) {
287 style[ name ] = "inherit";
288 }
289
290 // If a hook was provided, use that value, otherwise just set the specified value
291 if ( !hooks || !( "set" in hooks ) ||
292 ( value = hooks.set( elem, value, extra ) ) !== undefined ) {
293
294 if ( isCustomProp ) {
295 style.setProperty( name, value );
296 } else {
297 style[ name ] = value;
298 }
299 }
300
301 } else {
302
303 // If a hook was provided get the non-computed value from there
304 if ( hooks && "get" in hooks &&
305 ( ret = hooks.get( elem, false, extra ) ) !== undefined ) {
306
307 return ret;
308 }
309
310 // Otherwise just get the value from the style object
311 return style[ name ];
312 }
313 },
314
315 css: function( elem, name, extra, styles ) {
316 var val, num, hooks,
317 origName = camelCase( name ),
318 isCustomProp = rcustomProp.test( name );
319
320 // Make sure that we're working with the right name. We don't
321 // want to modify the value if it is a CSS custom property
322 // since they are user-defined.
323 if ( !isCustomProp ) {
324 name = finalPropName( origName );
325 }
326
327 // Try prefixed name followed by the unprefixed name
328 hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
329
330 // If a hook was provided get the computed value from there
331 if ( hooks && "get" in hooks ) {
332 val = hooks.get( elem, true, extra );
333 }
334
335 // Otherwise, if a way to get the computed value exists, use that
336 if ( val === undefined ) {
337 val = curCSS( elem, name, styles );
338 }
339
340 // Convert "normal" to computed value
341 if ( val === "normal" && name in cssNormalTransform ) {
342 val = cssNormalTransform[ name ];
343 }
344
345 // Make numeric if forced or a qualifier was provided and val looks numeric
346 if ( extra === "" || extra ) {
347 num = parseFloat( val );
348 return extra === true || isFinite( num ) ? num || 0 : val;
349 }
350
351 return val;
352 }
353 } );
354
355 jQuery.each( [ "height", "width" ], function( i, dimension ) {
356 jQuery.cssHooks[ dimension ] = {
357 get: function( elem, computed, extra ) {
358 if ( computed ) {
359
360 // Certain elements can have dimension info if we invisibly show them
361 // but it must have a current display style that would benefit
362 return rdisplayswap.test( jQuery.css( elem, "display" ) ) &&
363
364 // Support: Safari 8+
365 // Table columns in Safari have non-zero offsetWidth & zero
366 // getBoundingClientRect().width unless display is changed.
367 // Support: IE <=11 only
368 // Running getBoundingClientRect on a disconnected node
369 // in IE throws an error.
370 ( !elem.getClientRects().length || !elem.getBoundingClientRect().width ) ?
371 swap( elem, cssShow, function() {
372 return getWidthOrHeight( elem, dimension, extra );
373 } ) :
374 getWidthOrHeight( elem, dimension, extra );
375 }
376 },
377
378 set: function( elem, value, extra ) {
379 var matches,
380 styles = getStyles( elem ),
381 isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box",
382 subtract = extra && boxModelAdjustment(
383 elem,
384 dimension,
385 extra,
386 isBorderBox,
387 styles
388 );
389
390 // Account for unreliable border-box dimensions by comparing offset* to computed and
391 // faking a content-box to get border and padding (gh-3699)
392 if ( isBorderBox && support.scrollboxSize() === styles.position ) {
393 subtract -= Math.ceil(
394 elem[ "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 ) ] -
395 parseFloat( styles[ dimension ] ) -
396 boxModelAdjustment( elem, dimension, "border", false, styles ) -
397 0.5
398 );
399 }
400
401 // Convert to pixels if value adjustment is needed
402 if ( subtract && ( matches = rcssNum.exec( value ) ) &&
403 ( matches[ 3 ] || "px" ) !== "px" ) {
404
405 elem.style[ dimension ] = value;
406 value = jQuery.css( elem, dimension );
407 }
408
409 return setPositiveNumber( elem, value, subtract );
410 }
411 };
412 } );
413
414 jQuery.cssHooks.marginLeft = addGetHookIf( support.reliableMarginLeft,
415 function( elem, computed ) {
416 if ( computed ) {
417 return ( parseFloat( curCSS( elem, "marginLeft" ) ) ||
418 elem.getBoundingClientRect().left -
419 swap( elem, { marginLeft: 0 }, function() {
420 return elem.getBoundingClientRect().left;
421 } )
422 ) + "px";
423 }
424 }
425 );
426
427 // These hooks are used by animate to expand properties
428 jQuery.each( {
429 margin: "",
430 padding: "",
431 border: "Width"
432 }, function( prefix, suffix ) {
433 jQuery.cssHooks[ prefix + suffix ] = {
434 expand: function( value ) {
435 var i = 0,
436 expanded = {},
437
438 // Assumes a single number if not a string
439 parts = typeof value === "string" ? value.split( " " ) : [ value ];
440
441 for ( ; i < 4; i++ ) {
442 expanded[ prefix + cssExpand[ i ] + suffix ] =
443 parts[ i ] || parts[ i - 2 ] || parts[ 0 ];
444 }
445
446 return expanded;
447 }
448 };
449
450 if ( prefix !== "margin" ) {
451 jQuery.cssHooks[ prefix + suffix ].set = setPositiveNumber;
452 }
453 } );
454
455 jQuery.fn.extend( {
456 css: function( name, value ) {
457 return access( this, function( elem, name, value ) {
458 var styles, len,
459 map = {},
460 i = 0;
461
462 if ( Array.isArray( name ) ) {
463 styles = getStyles( elem );
464 len = name.length;
465
466 for ( ; i < len; i++ ) {
467 map[ name[ i ] ] = jQuery.css( elem, name[ i ], false, styles );
468 }
469
470 return map;
471 }
472
473 return value !== undefined ?
474 jQuery.style( elem, name, value ) :
475 jQuery.css( elem, name );
476 }, name, value, arguments.length > 1 );
477 }
478 } );
479
480 return jQuery;
481 } );