1 import jQuery from "./core.js";
2 import access from "./core/access.js";
3 import nodeName from "./core/nodeName.js";
4 import rcssNum from "./var/rcssNum.js";
5 import isIE from "./var/isIE.js";
6 import rnumnonpx from "./css/var/rnumnonpx.js";
7 import rcustomProp from "./css/var/rcustomProp.js";
8 import cssExpand from "./css/var/cssExpand.js";
9 import isAutoPx from "./css/isAutoPx.js";
10 import cssCamelCase from "./css/cssCamelCase.js";
11 import getStyles from "./css/var/getStyles.js";
12 import swap from "./css/var/swap.js";
13 import curCSS from "./css/curCSS.js";
14 import adjustCSS from "./css/adjustCSS.js";
15 import finalPropName from "./css/finalPropName.js";
16 import support from "./css/support.js";
18 import "./core/init.js";
19 import "./core/ready.js";
20 import "./selector.js"; // contains
24 // Swappable if display is none or starts with table
25 // except "table", "table-cell", or "table-caption"
26 // See here for display values: https://developer.mozilla.org/en-US/docs/CSS/display
27 rdisplayswap = /^(none|table(?!-c[ea]).+)/,
28 cssShow = { position: "absolute", visibility: "hidden", display: "block" },
29 cssNormalTransform = {
34 function setPositiveNumber( _elem, value, subtract ) {
36 // Any relative (+/-) values have already been
37 // normalized at this point
38 var matches = rcssNum.exec( value );
41 // Guard against undefined "subtract", e.g., when used as in cssHooks
42 Math.max( 0, matches[ 2 ] - ( subtract || 0 ) ) + ( matches[ 3 ] || "px" ) :
46 function boxModelAdjustment( elem, dimension, box, isBorderBox, styles, computedVal ) {
47 var i = dimension === "width" ? 1 : 0,
51 // Adjustment may not be necessary
52 if ( box === ( isBorderBox ? "border" : "content" ) ) {
56 for ( ; i < 4; i += 2 ) {
58 // Both box models exclude margin
59 if ( box === "margin" ) {
60 delta += jQuery.css( elem, box + cssExpand[ i ], true, styles );
63 // If we get here with a content-box, we're seeking "padding" or "border" or "margin"
67 delta += jQuery.css( elem, "padding" + cssExpand[ i ], true, styles );
69 // For "border" or "margin", add border
70 if ( box !== "padding" ) {
71 delta += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
73 // But still keep track of it otherwise
75 extra += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
78 // If we get here with a border-box (content + padding + border), we're seeking "content" or
79 // "padding" or "margin"
82 // For "content", subtract padding
83 if ( box === "content" ) {
84 delta -= jQuery.css( elem, "padding" + cssExpand[ i ], true, styles );
87 // For "content" or "padding", subtract border
88 if ( box !== "margin" ) {
89 delta -= jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles );
94 // Account for positive content-box scroll gutter when requested by providing computedVal
95 if ( !isBorderBox && computedVal >= 0 ) {
97 // offsetWidth/offsetHeight is a rounded sum of content, padding, scroll gutter, and border
98 // Assuming integer scroll gutter, subtract the rest and round down
99 delta += Math.max( 0, Math.ceil(
100 elem[ "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 ) ] -
106 // If offsetWidth/offsetHeight is unknown, then we can't determine content-box scroll gutter
107 // Use an explicit zero to avoid NaN (gh-3964)
114 function getWidthOrHeight( elem, dimension, extra ) {
116 // Start with computed style
117 var styles = getStyles( elem ),
119 // To avoid forcing a reflow, only fetch boxSizing if we need it (gh-4322).
120 // Fake content-box until we know it's needed to know the true value.
121 boxSizingNeeded = isIE || extra,
122 isBorderBox = boxSizingNeeded &&
123 jQuery.css( elem, "boxSizing", false, styles ) === "border-box",
124 valueIsBorderBox = isBorderBox,
126 val = curCSS( elem, dimension, styles ),
127 offsetProp = "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 );
129 // Return a confounding non-pixel value or feign ignorance, as appropriate.
130 if ( rnumnonpx.test( val ) ) {
140 // Fall back to offsetWidth/offsetHeight when value is "auto"
141 // This happens for inline elements with no explicit setting (gh-3571)
144 // Support: IE 9 - 11+
145 // Use offsetWidth/offsetHeight for when box sizing is unreliable.
146 // In those cases, the computed value can be trusted to be border-box.
147 ( isIE && isBorderBox ) ||
149 // Support: IE 10 - 11+
150 // IE misreports `getComputedStyle` of table rows with width/height
151 // set in CSS while `offset*` properties report correct values.
152 // Support: Firefox 70+
153 // Firefox includes border widths
154 // in computed dimensions for table rows. (gh-4529)
155 ( !support.reliableTrDimensions() && nodeName( elem, "tr" ) ) ) &&
157 // Make sure the element is visible & connected
158 elem.getClientRects().length ) {
160 isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box";
162 // Where available, offsetWidth/offsetHeight approximate border box dimensions.
163 // Where not available (e.g., SVG), assume unreliable box-sizing and interpret the
164 // retrieved value as a content box dimension.
165 valueIsBorderBox = offsetProp in elem;
166 if ( valueIsBorderBox ) {
167 val = elem[ offsetProp ];
171 // Normalize "" and auto
172 val = parseFloat( val ) || 0;
174 // Adjust for the element's box model
179 extra || ( isBorderBox ? "border" : "content" ),
183 // Provide the current computed size to request scroll gutter calculation (gh-3589)
191 // Add in style property hooks for overriding the default
192 // behavior of getting and setting a style property
195 // Get and set the style property on a DOM Node
196 style: function( elem, name, value, extra ) {
198 // Don't set styles on text and comment nodes
199 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style ) {
203 // Make sure that we're working with the right name
204 var ret, type, hooks,
205 origName = cssCamelCase( name ),
206 isCustomProp = rcustomProp.test( name ),
209 // Make sure that we're working with the right name. We don't
210 // want to query the value if it is a CSS custom property
211 // since they are user-defined.
212 if ( !isCustomProp ) {
213 name = finalPropName( origName );
216 // Gets hook for the prefixed version, then unprefixed version
217 hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
219 // Check if we're setting a value
220 if ( value !== undefined ) {
223 // Convert "+=" or "-=" to relative numbers (trac-7345)
224 if ( type === "string" && ( ret = rcssNum.exec( value ) ) && ret[ 1 ] ) {
225 value = adjustCSS( elem, name, ret );
227 // Fixes bug trac-9237
231 // Make sure that null and NaN values aren't set (trac-7116)
232 if ( value == null || value !== value ) {
236 // If the value is a number, add `px` for certain CSS properties
237 if ( type === "number" ) {
238 value += ret && ret[ 3 ] || ( isAutoPx( origName ) ? "px" : "" );
241 // Support: IE <=9 - 11+
242 // background-* props of a cloned element affect the source element (trac-8908)
243 if ( isIE && value === "" && name.indexOf( "background" ) === 0 ) {
244 style[ name ] = "inherit";
247 // If a hook was provided, use that value, otherwise just set the specified value
248 if ( !hooks || !( "set" in hooks ) ||
249 ( value = hooks.set( elem, value, extra ) ) !== undefined ) {
251 if ( isCustomProp ) {
252 style.setProperty( name, value );
254 style[ name ] = value;
260 // If a hook was provided get the non-computed value from there
261 if ( hooks && "get" in hooks &&
262 ( ret = hooks.get( elem, false, extra ) ) !== undefined ) {
267 // Otherwise just get the value from the style object
268 return style[ name ];
272 css: function( elem, name, extra, styles ) {
274 origName = cssCamelCase( name ),
275 isCustomProp = rcustomProp.test( name );
277 // Make sure that we're working with the right name. We don't
278 // want to modify the value if it is a CSS custom property
279 // since they are user-defined.
280 if ( !isCustomProp ) {
281 name = finalPropName( origName );
284 // Try prefixed name followed by the unprefixed name
285 hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ];
287 // If a hook was provided get the computed value from there
288 if ( hooks && "get" in hooks ) {
289 val = hooks.get( elem, true, extra );
292 // Otherwise, if a way to get the computed value exists, use that
293 if ( val === undefined ) {
294 val = curCSS( elem, name, styles );
297 // Convert "normal" to computed value
298 if ( val === "normal" && name in cssNormalTransform ) {
299 val = cssNormalTransform[ name ];
302 // Make numeric if forced or a qualifier was provided and val looks numeric
303 if ( extra === "" || extra ) {
304 num = parseFloat( val );
305 return extra === true || isFinite( num ) ? num || 0 : val;
312 jQuery.each( [ "height", "width" ], function( _i, dimension ) {
313 jQuery.cssHooks[ dimension ] = {
314 get: function( elem, computed, extra ) {
317 // Certain elements can have dimension info if we invisibly show them
318 // but it must have a current display style that would benefit
319 return rdisplayswap.test( jQuery.css( elem, "display" ) ) &&
321 // Support: Safari <=8 - 12+, Chrome <=73+
322 // Table columns in WebKit/Blink have non-zero offsetWidth & zero
323 // getBoundingClientRect().width unless display is changed.
325 // Running getBoundingClientRect on a disconnected node
326 // in IE throws an error.
327 ( !elem.getClientRects().length || !elem.getBoundingClientRect().width ) ?
328 swap( elem, cssShow, function() {
329 return getWidthOrHeight( elem, dimension, extra );
331 getWidthOrHeight( elem, dimension, extra );
335 set: function( elem, value, extra ) {
337 styles = getStyles( elem ),
339 // To avoid forcing a reflow, only fetch boxSizing if we need it (gh-3991)
340 isBorderBox = extra &&
341 jQuery.css( elem, "boxSizing", false, styles ) === "border-box",
352 // Convert to pixels if value adjustment is needed
353 if ( subtract && ( matches = rcssNum.exec( value ) ) &&
354 ( matches[ 3 ] || "px" ) !== "px" ) {
356 elem.style[ dimension ] = value;
357 value = jQuery.css( elem, dimension );
360 return setPositiveNumber( elem, value, subtract );
365 // These hooks are used by animate to expand properties
370 }, function( prefix, suffix ) {
371 jQuery.cssHooks[ prefix + suffix ] = {
372 expand: function( value ) {
376 // Assumes a single number if not a string
377 parts = typeof value === "string" ? value.split( " " ) : [ value ];
379 for ( ; i < 4; i++ ) {
380 expanded[ prefix + cssExpand[ i ] + suffix ] =
381 parts[ i ] || parts[ i - 2 ] || parts[ 0 ];
388 if ( prefix !== "margin" ) {
389 jQuery.cssHooks[ prefix + suffix ].set = setPositiveNumber;
394 css: function( name, value ) {
395 return access( this, function( elem, name, value ) {
400 if ( Array.isArray( name ) ) {
401 styles = getStyles( elem );
404 for ( ; i < len; i++ ) {
405 map[ name[ i ] ] = jQuery.css( elem, name[ i ], false, styles );
411 return value !== undefined ?
412 jQuery.style( elem, name, value ) :
413 jQuery.css( elem, name );
414 }, name, value, arguments.length > 1 );
418 export default jQuery;