Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -80,58 +80,77 @@ function setPositiveNumber( elem, value, subtract ) { | ||
value; | value; | ||
} | } | ||
|
|
||
function augmentWidthOrHeight( elem, name, extra, isBorderBox, styles ) { | function boxModelAdjustment( elem, dimension, box, isBorderBox, styles, computedVal ) { | ||
var i, | var i = dimension === "width" ? 1 : 0, | ||
val = 0; | extra = 0, | ||
|
delta = 0; | ||
// If we already have the right measurement, avoid augmentation |
|
||
if ( extra === ( isBorderBox ? "border" : "content" ) ) { | // Adjustment may not be necessary | ||
i = 4; | if ( box === ( isBorderBox ? "border" : "content" ) ) { | ||
|
return 0; | ||
// Otherwise initialize for horizontal or vertical properties | |||
} else { | |||
i = name === "width" ? 1 : 0; | |||
} | } | ||
|
|
||
for ( ; i < 4; i += 2 ) { | for ( ; i < 4; i += 2 ) { | ||
|
|
||
// Both box models exclude margin, so add it if we want it | // Both box models exclude margin | ||
if ( extra === "margin" ) { | if ( box === "margin" ) { | ||
val += jQuery.css( elem, extra + cssExpand[ i ], true, styles ); | delta += jQuery.css( elem, box + cssExpand[ i ], true, styles ); | ||
} | } | ||
|
|
||
if ( isBorderBox ) { | // If we get here with a content-box, we're seeking "padding" or "border" or "margin" | ||
if ( !isBorderBox ) { | |||
|
|
||
// border-box includes padding, so remove it if we want content | // Add padding | ||
if ( extra === "content" ) { | delta += jQuery.css( elem, "padding" + cssExpand[ i ], true, styles ); | ||
val -= jQuery.css( elem, "padding" + cssExpand[ i ], true, styles ); | |||
} | |||
|
|
||
// At this point, extra isn't border nor margin, so remove border | // For "border" or "margin", add border | ||
if ( extra !== "margin" ) { | if ( box !== "padding" ) { | ||
val -= jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles ); | delta += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles ); | ||
|
|||
// But still keep track of it otherwise | |||
} else { | |||
extra += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles ); | |||
} | } | ||
|
|||
// If we get here with a border-box (content + padding + border), we're seeking "content" or | |||
// "padding" or "margin" | |||
} else { | } else { | ||
|
|
||
// At this point, extra isn't content, so add padding | // For "content", subtract padding | ||
val += jQuery.css( elem, "padding" + cssExpand[ i ], true, styles ); | if ( box === "content" ) { | ||
delta -= jQuery.css( elem, "padding" + cssExpand[ i ], true, styles ); | |||
} | |||
|
|
||
// At this point, extra isn't content nor padding, so add border | // For "content" or "padding", subtract border | ||
if ( extra !== "padding" ) { | if ( box !== "margin" ) { | ||
val += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles ); | delta -= jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles ); | ||
} | } | ||
} | } | ||
} | } | ||
|
|
||
return val; | // Account for positive content-box scroll gutter when requested by providing computedVal | ||
if ( !isBorderBox && computedVal >= 0 ) { | |||
|
|||
// offsetWidth/offsetHeight is a rounded sum of content, padding, scroll gutter, and border | |||
// Assuming integer scroll gutter, subtract the rest and round down | |||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mgol
Member
|
|||
delta += Math.max( 0, Math.ceil( | |||
elem[ "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 ) ] - | |||
computedVal - | |||
delta - | |||
extra - | |||
0.5 | |||
) ); | |||
} | |||
|
|||
return delta; | |||
} | } | ||
|
|
||
function getWidthOrHeight( elem, name, extra ) { | function getWidthOrHeight( elem, dimension, extra ) { | ||
|
|
||
// Start with computed style | // Start with computed style | ||
var valueIsBorderBox, | var valueIsBorderBox, | ||
styles = getStyles( elem ), | styles = getStyles( elem ), | ||
val = curCSS( elem, name, styles ), | val = curCSS( elem, dimension, styles ), | ||
isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box"; | isBorderBox = jQuery.css( elem, "boxSizing", false, styles ) === "border-box"; | ||
|
|
||
// Computed unit is not pixels. Stop here and return. | // Computed unit is not pixels. Stop here and return. | ||
|
@@ -142,25 +161,28 @@ function getWidthOrHeight( elem, name, extra ) { | ||
// Check for style in case a browser which returns unreliable values | // Check for style in case a browser which returns unreliable values | ||
// for getComputedStyle silently falls back to the reliable elem.style | // for getComputedStyle silently falls back to the reliable elem.style | ||
valueIsBorderBox = isBorderBox && | valueIsBorderBox = isBorderBox && | ||
( support.boxSizingReliable() || val === elem.style[ name ] ); | ( support.boxSizingReliable() || val === elem.style[ dimension ] ); | ||
|
|
||
// Fall back to offsetWidth/Height when value is "auto" | // Fall back to offsetWidth/Height when value is "auto" | ||
// This happens for inline elements with no explicit setting (gh-3571) | // This happens for inline elements with no explicit setting (gh-3571) | ||
if ( val === "auto" ) { | if ( val === "auto" ) { | ||
val = elem[ "offset" + name[ 0 ].toUpperCase() + name.slice( 1 ) ]; | val = elem[ "offset" + dimension[ 0 ].toUpperCase() + dimension.slice( 1 ) ]; | ||
} | } | ||
|
|
||
// Normalize "", auto, and prepare for extra | // Normalize "" and auto | ||
val = parseFloat( val ) || 0; | val = parseFloat( val ) || 0; | ||
|
|
||
// Use the active box-sizing model to add/subtract irrelevant styles | // Adjust for the element's box model | ||
return ( val + | return ( val + | ||
augmentWidthOrHeight( | boxModelAdjustment( | ||
elem, | elem, | ||
name, | dimension, | ||
extra || ( isBorderBox ? "border" : "content" ), | extra || ( isBorderBox ? "border" : "content" ), | ||
valueIsBorderBox, | valueIsBorderBox, | ||
styles | styles, | ||
|
|||
// Provide the current computed size to request scroll gutter calculation (gh-3589) | |||
val | |||
) | ) | ||
) + "px"; | ) + "px"; | ||
} | } | ||
|
@@ -319,8 +341,8 @@ jQuery.extend( { | ||
} | } | ||
} ); | } ); | ||
|
|
||
jQuery.each( [ "height", "width" ], function( i, name ) { | jQuery.each( [ "height", "width" ], function( i, dimension ) { | ||
jQuery.cssHooks[ name ] = { | jQuery.cssHooks[ dimension ] = { | ||
get: function( elem, computed, extra ) { | get: function( elem, computed, extra ) { | ||
if ( computed ) { | if ( computed ) { | ||
|
|
||
|
@@ -336,18 +358,18 @@ jQuery.each( [ "height", "width" ], function( i, name ) { | ||
// in IE throws an error. | // in IE throws an error. | ||
( !elem.getClientRects().length || !elem.getBoundingClientRect().width ) ? | ( !elem.getClientRects().length || !elem.getBoundingClientRect().width ) ? | ||
swap( elem, cssShow, function() { | swap( elem, cssShow, function() { | ||
return getWidthOrHeight( elem, name, extra ); | return getWidthOrHeight( elem, dimension, extra ); | ||
} ) : | } ) : | ||
getWidthOrHeight( elem, name, extra ); | getWidthOrHeight( elem, dimension, extra ); | ||
} | } | ||
}, | }, | ||
|
|
||
set: function( elem, value, extra ) { | set: function( elem, value, extra ) { | ||
var matches, | var matches, | ||
styles = extra && getStyles( elem ), | styles = getStyles( elem ), | ||
subtract = extra && augmentWidthOrHeight( | subtract = extra && boxModelAdjustment( | ||
elem, | elem, | ||
name, | dimension, | ||
extra, | extra, | ||
jQuery.css( elem, "boxSizing", false, styles ) === "border-box", | jQuery.css( elem, "boxSizing", false, styles ) === "border-box", | ||
styles | styles | ||
|
@@ -357,8 +379,8 @@ jQuery.each( [ "height", "width" ], function( i, name ) { | ||
if ( subtract && ( matches = rcssNum.exec( value ) ) && | if ( subtract && ( matches = rcssNum.exec( value ) ) && | ||
( matches[ 3 ] || "px" ) !== "px" ) { | ( matches[ 3 ] || "px" ) !== "px" ) { | ||
|
|
||
elem.style[ name ] = value; | elem.style[ dimension ] = value; | ||
value = jQuery.css( elem, name ); | value = jQuery.css( elem, dimension ); | ||
} | } | ||
|
|
||
return setPositiveNumber( elem, value, subtract ); | return setPositiveNumber( elem, value, subtract ); | ||
|
here Assuming integer scroll gutter, subtract the rest and round down, but used
Math.ceil
, that round up.seems like a bug?