From 65b24861bc2e5f25b08373b2c5afdf15d3256e90 Mon Sep 17 00:00:00 2001 From: Brandon Aaron Date: Thu, 2 Sep 2010 21:06:40 -0500 Subject: [PATCH 01/21] First look at css hooks. These hooks provide a way to change how jQuery handles getting and setting certain css properties. This means normalizing properties like background-position can easily be done via plugins. They are similar in concept to the special event hooks. Return false from the hook to revert control back to jQuery's normal processes for getting and setting certain css properties. --- src/css.js | 86 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/src/css.js b/src/css.js index cb65c0ba..497303a8 100644 --- a/src/css.js +++ b/src/css.js @@ -25,7 +25,7 @@ jQuery.fn.css = function( name, value ) { if ( value === undefined ) { return jQuery.curCSS( elem, name ); } - + if ( typeof value === "number" && !rexclude.test(name) ) { value += "px"; } @@ -35,6 +35,42 @@ jQuery.fn.css = function( name, value ) { }; jQuery.extend({ + cssHooks: { + opacity: { + get: function( elem, force ) { + var style = elem.style; + if ( jQuery.support.opacity && !style.filter ) { + return false; // move along, nothing to see here + } + + // IE uses filters for opacity + var ret = ropacity.test(elem.currentStyle.filter || "") ? + (parseFloat(RegExp.$1) / 100) + "" : + ""; + + return ret === "" ? + "1" : + ret; + }, + + set: function( elem, value ) { + var style = elem.style; + if ( jQuery.support.opacity && !style.filter ) { + return false; // move along, nothing to see here + } + + // IE has trouble with opacity if it does not have layout + // Force it by setting the zoom level + style.zoom = 1; + + // Set the alpha filter to set the opacity + var opacity = parseInt( value, 10 ) + "" === "NaN" ? "" : "alpha(opacity=" + value * 100 + ")"; + var filter = style.filter || jQuery.curCSS( elem, "filter" ) || ""; + style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : opacity; + } + } + }, + style: function( elem, name, value ) { // don't set styles on text and comment nodes if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) { @@ -48,24 +84,6 @@ jQuery.extend({ var style = elem.style || elem, set = value !== undefined; - // IE uses filters for opacity - if ( !jQuery.support.opacity && name === "opacity" && style.filter ) { - if ( set ) { - // IE has trouble with opacity if it does not have layout - // Force it by setting the zoom level - style.zoom = 1; - - // Set the alpha filter to set the opacity - var opacity = parseInt( value, 10 ) + "" === "NaN" ? "" : "alpha(opacity=" + value * 100 + ")"; - var filter = style.filter || jQuery.curCSS( elem, "filter" ) || ""; - style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : opacity; - } - - return style.filter && style.filter.indexOf("opacity=") >= 0 ? - (parseFloat( ropacity.exec(style.filter)[1] ) / 100) + "": - ""; - } - // Make sure we're using the right name for getting the float value if ( rfloat.test( name ) ) { name = styleFloat; @@ -73,8 +91,19 @@ jQuery.extend({ name = name.replace(rdashAlpha, fcamelCase); + var hooks = jQuery.cssHooks[name] || {}; + if ( set ) { - style[ name ] = value; + if ( !('set' in hooks) || hooks.set( elem, value ) === false ) { + style[ name ] = value; + } + } + + if ( 'get' in hooks ) { + var cssHookReturn = hooks.get( elem, false ); + if ( cssHookReturn !== false ) { + return cssHookReturn; + } } return style[ name ]; @@ -98,24 +127,17 @@ jQuery.extend({ }, curCSS: function( elem, name, force ) { - var ret, style = elem.style, filter; - - // IE uses filters for opacity - if ( !jQuery.support.opacity && name === "opacity" && elem.currentStyle ) { - ret = ropacity.test(elem.currentStyle.filter || "") ? - (parseFloat(RegExp.$1) / 100) + "" : - ""; - - return ret === "" ? - "1" : - ret; - } + var ret, style = elem.style, filter, hooks = jQuery.cssHooks[name] || {}; // Make sure we're using the right name for getting the float value if ( rfloat.test( name ) ) { name = styleFloat; } + if ( 'get' in hooks && ( ret = hooks.get( elem, force ) ) !== false ) { + return ret; + } + if ( !force && style && style[ name ] ) { ret = style[ name ]; From 1970154c3b50d3cbc990b93b48d7448390bd84cf Mon Sep 17 00:00:00 2001 From: John Resig Date: Sat, 4 Sep 2010 23:28:17 -0400 Subject: [PATCH 02/21] Tweaking the formatting from the previous commit. --- src/css.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/css.js b/src/css.js index 497303a8..6f26fd0c 100644 --- a/src/css.js +++ b/src/css.js @@ -93,13 +93,11 @@ jQuery.extend({ var hooks = jQuery.cssHooks[name] || {}; - if ( set ) { - if ( !('set' in hooks) || hooks.set( elem, value ) === false ) { - style[ name ] = value; - } + if ( set && (!("set" in hooks) || hooks.set( elem, value ) === false) ) { + style[ name ] = value; } - if ( 'get' in hooks ) { + if ( "get" in hooks ) { var cssHookReturn = hooks.get( elem, false ); if ( cssHookReturn !== false ) { return cssHookReturn; @@ -134,7 +132,7 @@ jQuery.extend({ name = styleFloat; } - if ( 'get' in hooks && ( ret = hooks.get( elem, force ) ) !== false ) { + if ( "get" in hooks && ( ret = hooks.get( elem, force ) ) !== false ) { return ret; } From 426045cc741b76ce95cf91ff5818879b3d067b76 Mon Sep 17 00:00:00 2001 From: John Resig Date: Sat, 4 Sep 2010 23:34:52 -0400 Subject: [PATCH 03/21] Some more minor formatting tweaks. --- src/css.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/css.js b/src/css.js index 6f26fd0c..dab3783f 100644 --- a/src/css.js +++ b/src/css.js @@ -39,14 +39,15 @@ jQuery.extend({ opacity: { get: function( elem, force ) { var style = elem.style; + if ( jQuery.support.opacity && !style.filter ) { return false; // move along, nothing to see here } // IE uses filters for opacity var ret = ropacity.test(elem.currentStyle.filter || "") ? - (parseFloat(RegExp.$1) / 100) + "" : - ""; + (parseFloat(RegExp.$1) / 100) + "" : + ""; return ret === "" ? "1" : @@ -55,6 +56,7 @@ jQuery.extend({ set: function( elem, value ) { var style = elem.style; + if ( jQuery.support.opacity && !style.filter ) { return false; // move along, nothing to see here } @@ -64,9 +66,15 @@ jQuery.extend({ style.zoom = 1; // Set the alpha filter to set the opacity - var opacity = parseInt( value, 10 ) + "" === "NaN" ? "" : "alpha(opacity=" + value * 100 + ")"; + var opacity = parseInt( value, 10 ) + "" === "NaN" ? + "" : + "alpha(opacity=" + value * 100 + ")"; + var filter = style.filter || jQuery.curCSS( elem, "filter" ) || ""; - style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : opacity; + + style.filter = ralpha.test(filter) ? + filter.replace(ralpha, opacity) : + opacity; } } }, From 2912f8ab9520e042e8b27865617e586e34a05704 Mon Sep 17 00:00:00 2001 From: John Resig Date: Sun, 5 Sep 2010 00:04:37 -0400 Subject: [PATCH 04/21] Broke apart some of the browser-specific logic for CSS handling. --- src/css.js | 188 ++++++++++++++++++++++++++--------------------------- 1 file changed, 93 insertions(+), 95 deletions(-) diff --git a/src/css.js b/src/css.js index dab3783f..5d50d32f 100644 --- a/src/css.js +++ b/src/css.js @@ -11,6 +11,7 @@ var rexclude = /z-?index|font-?weight|opacity|zoom|line-?height/i, cssShow = { position: "absolute", visibility: "hidden", display:"block" }, cssWidth = [ "Left", "Right" ], cssHeight = [ "Top", "Bottom" ], + curCSS, // cache check for defaultView.getComputedStyle getComputedStyle = document.defaultView && document.defaultView.getComputedStyle, @@ -35,49 +36,7 @@ jQuery.fn.css = function( name, value ) { }; jQuery.extend({ - cssHooks: { - opacity: { - get: function( elem, force ) { - var style = elem.style; - - if ( jQuery.support.opacity && !style.filter ) { - return false; // move along, nothing to see here - } - - // IE uses filters for opacity - var ret = ropacity.test(elem.currentStyle.filter || "") ? - (parseFloat(RegExp.$1) / 100) + "" : - ""; - - return ret === "" ? - "1" : - ret; - }, - - set: function( elem, value ) { - var style = elem.style; - - if ( jQuery.support.opacity && !style.filter ) { - return false; // move along, nothing to see here - } - - // IE has trouble with opacity if it does not have layout - // Force it by setting the zoom level - style.zoom = 1; - - // Set the alpha filter to set the opacity - var opacity = parseInt( value, 10 ) + "" === "NaN" ? - "" : - "alpha(opacity=" + value * 100 + ")"; - - var filter = style.filter || jQuery.curCSS( elem, "filter" ) || ""; - - style.filter = ralpha.test(filter) ? - filter.replace(ralpha, opacity) : - opacity; - } - } - }, + cssHooks: {}, style: function( elem, name, value ) { // don't set styles on text and comment nodes @@ -133,69 +92,22 @@ jQuery.extend({ }, curCSS: function( elem, name, force ) { - var ret, style = elem.style, filter, hooks = jQuery.cssHooks[name] || {}; + var ret, style = elem.style || {}, hooks = jQuery.cssHooks[name] || {}; // Make sure we're using the right name for getting the float value if ( rfloat.test( name ) ) { name = styleFloat; } - if ( "get" in hooks && ( ret = hooks.get( elem, force ) ) !== false ) { + if ( "get" in hooks && (ret = hooks.get( elem, force )) !== false ) { return ret; } - if ( !force && style && style[ name ] ) { + if ( !force && name in style ) { ret = style[ name ]; - } else if ( getComputedStyle ) { - - // Only "float" is needed here - if ( rfloat.test( name ) ) { - name = "float"; - } - - name = name.replace( rupper, "-$1" ).toLowerCase(); - - var defaultView = elem.ownerDocument.defaultView; - - if ( !defaultView ) { - return null; - } - - var computedStyle = defaultView.getComputedStyle( elem, null ); - - if ( computedStyle ) { - ret = computedStyle.getPropertyValue( name ); - } - - // We should always get a number back from opacity - if ( name === "opacity" && ret === "" ) { - ret = "1"; - } - - } else if ( elem.currentStyle ) { - var camelCase = name.replace(rdashAlpha, fcamelCase); - - ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ]; - - // From the awesome hack by Dean Edwards - // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291 - - // If we're not dealing with a regular pixel number - // but a number that has a weird ending, we need to convert it to pixels - if ( !rnumpx.test( ret ) && rnum.test( ret ) ) { - // Remember the original values - var left = style.left, rsLeft = elem.runtimeStyle.left; - - // Put in the new values to get a computed value out - elem.runtimeStyle.left = elem.currentStyle.left; - style.left = camelCase === "fontSize" ? "1em" : (ret || 0); - ret = style.pixelLeft + "px"; - - // Revert the changed values - style.left = left; - elem.runtimeStyle.left = rsLeft; - } + } else if ( curCSS ) { + ret = curCSS( elem, name ); } return ret; @@ -220,6 +132,92 @@ jQuery.extend({ } }); +if ( !jQuery.support.opacity ) { + jQuery.cssHooks.opacity = { + get: function( elem, force ) { + // IE uses filters for opacity + return ropacity.test(elem.currentStyle.filter || "") ? + (parseFloat(RegExp.$1) / 100) + "" : + "1"; + }, + + set: function( elem, value ) { + var style = elem.style; + + // IE has trouble with opacity if it does not have layout + // Force it by setting the zoom level + style.zoom = 1; + + // Set the alpha filter to set the opacity + var opacity = parseInt( value, 10 ) + "" === "NaN" ? + "" : + "alpha(opacity=" + value * 100 + ")"; + + var filter = style.filter || jQuery.curCSS( elem, "filter" ) || ""; + + style.filter = ralpha.test(filter) ? + filter.replace(ralpha, opacity) : + opacity; + } + }; +} + +if ( getComputedStyle ) { + curCSS = function( elem, name ) { + var ret, defaultView, computedStyle; + + // Only "float" is needed here + if ( rfloat.test( name ) ) { + name = "float"; + } + + name = name.replace( rupper, "-$1" ).toLowerCase(); + + if ( !(defaultView = elem.ownerDocument.defaultView) ) { + return null; + } + + if ( (computedStyle = defaultView.getComputedStyle( elem, null )) ) { + ret = computedStyle.getPropertyValue( name ); + } + + // We should always get a number back from opacity + if ( name === "opacity" && ret === "" ) { + ret = "1"; + } + + return ret; + }; + +} else if ( document.documentElement.currentStyle ) { + curCSS = function( elem, name ) { + var left, rsLeft, camelCase = name.replace(rdashAlpha, fcamelCase), + ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ]; + + // From the awesome hack by Dean Edwards + // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291 + + // If we're not dealing with a regular pixel number + // but a number that has a weird ending, we need to convert it to pixels + if ( !rnumpx.test( ret ) && rnum.test( ret ) ) { + // Remember the original values + left = style.left; + rsLeft = elem.runtimeStyle.left; + + // Put in the new values to get a computed value out + elem.runtimeStyle.left = elem.currentStyle.left; + style.left = camelCase === "fontSize" ? "1em" : (ret || 0); + ret = style.pixelLeft + "px"; + + // Revert the changed values + style.left = left; + elem.runtimeStyle.left = rsLeft; + } + + return ret; + }; +} + function getWH( elem, name, extra ) { var which = name === "width" ? cssWidth : cssHeight, val = name === "width" ? elem.offsetWidth : elem.offsetHeight; From 920099b29c1157b637f918cdc293e1c89e9cd4dc Mon Sep 17 00:00:00 2001 From: John Resig Date: Sun, 5 Sep 2010 10:17:18 -0400 Subject: [PATCH 05/21] First pass at unifying the various CSS methods in jQuery (jQuery.style, jQuery.curCSS, and jQuery.css are now all under jQuery.css). --- src/attributes.js | 4 -- src/css.js | 124 +++++++++++++++++++--------------------------- src/dimensions.js | 4 +- src/effects.js | 14 +++--- src/offset.js | 24 ++++----- 5 files changed, 71 insertions(+), 99 deletions(-) diff --git a/src/attributes.js b/src/attributes.js index 719c368a..fecba661 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -339,9 +339,5 @@ jQuery.extend({ // Non-existent attributes return null, we normalize to undefined return attr === null ? undefined : attr; } - - // elem is actually elem.style ... set the style - // Using attr for specific style information is now deprecated. Use style instead. - return jQuery.style( elem, name, value ); } }); diff --git a/src/css.js b/src/css.js index 5d50d32f..cd77ec1b 100644 --- a/src/css.js +++ b/src/css.js @@ -23,94 +23,51 @@ var rexclude = /z-?index|font-?weight|opacity|zoom|line-?height/i, jQuery.fn.css = function( name, value ) { return jQuery.access( this, name, value, true, function( elem, name, value ) { - if ( value === undefined ) { - return jQuery.curCSS( elem, name ); - } - - if ( typeof value === "number" && !rexclude.test(name) ) { - value += "px"; - } - - jQuery.style( elem, name, value ); + jQuery.css( elem, name, value ); }); }; jQuery.extend({ cssHooks: {}, - style: function( elem, name, value ) { + css: function( elem, name, value, force, extra ) { // don't set styles on text and comment nodes if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) { return undefined; } - // ignore negative width and height values #1599 - if ( (name === "width" || name === "height") && parseFloat(value) < 0 ) { - value = undefined; - } - - var style = elem.style || elem, set = value !== undefined; - // Make sure we're using the right name for getting the float value if ( rfloat.test( name ) ) { name = styleFloat; } - name = name.replace(rdashAlpha, fcamelCase); + name = name.replace( rdashAlpha, fcamelCase ); - var hooks = jQuery.cssHooks[name] || {}; - - if ( set && (!("set" in hooks) || hooks.set( elem, value ) === false) ) { - style[ name ] = value; - } - - if ( "get" in hooks ) { - var cssHookReturn = hooks.get( elem, false ); - if ( cssHookReturn !== false ) { - return cssHookReturn; - } - } - - return style[ name ]; - }, - - css: function( elem, name, force, extra ) { - if ( name === "width" || name === "height" ) { - if ( elem.offsetWidth !== 0 ) { - val = getWH( elem, name, extra ); - - } else { - jQuery.swap( elem, cssShow, function() { - val = getWH( elem, name, extra ); - }); - } - - return Math.max(0, Math.round(val)); - } - - return jQuery.curCSS( elem, name, force ); - }, - - curCSS: function( elem, name, force ) { var ret, style = elem.style || {}, hooks = jQuery.cssHooks[name] || {}; - // Make sure we're using the right name for getting the float value - if ( rfloat.test( name ) ) { - name = styleFloat; - } + if ( value !== undefined ) { + if ( typeof value === "number" && !rexclude.test(name) ) { + value += "px"; + } + + if ( !("set" in hooks) || (value = hooks.set( elem, value )) === false ) { + style[ name ] = value; + } + + } else { + if ( "get" in hooks && (ret = hooks.get( elem, force, extra )) !== false ) { + return ret; + } + + if ( !force && name in style ) { + ret = style[ name ]; + + } else if ( curCSS ) { + ret = curCSS( elem, name ); + } - if ( "get" in hooks && (ret = hooks.get( elem, force )) !== false ) { return ret; } - - if ( !force && name in style ) { - ret = style[ name ]; - - } else if ( curCSS ) { - ret = curCSS( elem, name ); - } - - return ret; }, // A method for quickly swapping in/out CSS properties to get correct calculations @@ -132,6 +89,26 @@ jQuery.extend({ } }); +jQuery.each(["height", "width"], function( i, name ) { + jQuery.cssHooks[ name ] = { + get: function( elem, force, extra ) { + if ( elem.offsetWidth !== 0 ) { + val = getWH( elem, name, extra ); + + } else { + jQuery.swap( elem, cssShow, function() { + val = getWH( elem, name, extra ); + }); + } + }, + + set: function( elem, value ) { + // ignore negative width and height values #1599 + elem.style[ name ] = Math.max( parseFloat(value), 0 ); + } + }; +}); + if ( !jQuery.support.opacity ) { jQuery.cssHooks.opacity = { get: function( elem, force ) { @@ -153,7 +130,7 @@ if ( !jQuery.support.opacity ) { "" : "alpha(opacity=" + value * 100 + ")"; - var filter = style.filter || jQuery.curCSS( elem, "filter" ) || ""; + var filter = style.filter || jQuery.css( elem, "filter" ) || ""; style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : @@ -191,8 +168,7 @@ if ( getComputedStyle ) { } else if ( document.documentElement.currentStyle ) { curCSS = function( elem, name ) { - var left, rsLeft, camelCase = name.replace(rdashAlpha, fcamelCase), - ret = elem.currentStyle[ name ] || elem.currentStyle[ camelCase ]; + var left, rsLeft, ret = elem.currentStyle[ name ]; // From the awesome hack by Dean Edwards // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291 @@ -206,7 +182,7 @@ if ( getComputedStyle ) { // Put in the new values to get a computed value out elem.runtimeStyle.left = elem.currentStyle.left; - style.left = camelCase === "fontSize" ? "1em" : (ret || 0); + style.left = name === "fontSize" ? "1em" : (ret || 0); ret = style.pixelLeft + "px"; // Revert the changed values @@ -228,14 +204,14 @@ function getWH( elem, name, extra ) { jQuery.each( which, function() { if ( !extra ) { - val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0; + val -= parseFloat(jQuery.css( elem, "padding" + this, undefined, true)) || 0; } if ( extra === "margin" ) { - val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0; + val += parseFloat(jQuery.css( elem, "margin" + this, undefined, true)) || 0; } else { - val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0; + val -= parseFloat(jQuery.css( elem, "border" + this + "Width", undefined, true)) || 0; } }); @@ -251,7 +227,7 @@ if ( jQuery.expr && jQuery.expr.filters ) { true : width > 0 && height > 0 && !skip ? false : - jQuery.curCSS(elem, "display") === "none"; + jQuery.css(elem, "display") === "none"; }; jQuery.expr.filters.visible = function( elem ) { diff --git a/src/dimensions.js b/src/dimensions.js index aeaaa78b..52fd0f86 100644 --- a/src/dimensions.js +++ b/src/dimensions.js @@ -6,14 +6,14 @@ jQuery.each([ "Height", "Width" ], function( i, name ) { // innerHeight and innerWidth jQuery.fn["inner" + name] = function() { return this[0] ? - jQuery.css( this[0], type, false, "padding" ) : + jQuery.css( this[0], type, undefined, false, "padding" ) : null; }; // outerHeight and outerWidth jQuery.fn["outer" + name] = function( margin ) { return this[0] ? - jQuery.css( this[0], type, false, margin ? "margin" : "border" ) : + jQuery.css( this[0], type, undefined, false, margin ? "margin" : "border" ) : null; }; diff --git a/src/effects.js b/src/effects.js index 40326e23..d31e2e35 100644 --- a/src/effects.js +++ b/src/effects.js @@ -64,7 +64,7 @@ jQuery.fn.extend({ for ( var i = 0, l = this.length; i < l; i++ ) { var old = jQuery.data(this[i], "olddisplay"); if ( !old && old !== "none" ) { - jQuery.data(this[i], "olddisplay", jQuery.css(this[i], "display")); + jQuery.data( this[i], "olddisplay", jQuery.css(this[i], "display") ); } } @@ -314,8 +314,8 @@ jQuery.fx.prototype = { return this.elem[ this.prop ]; } - var r = parseFloat(jQuery.css(this.elem, this.prop, force)); - return r && r > -10000 ? r : parseFloat(jQuery.curCSS(this.elem, this.prop)) || 0; + var r = parseFloat(jQuery.css(this.elem, this.prop, undefined, force)); + return r && r > -10000 ? r : parseFloat(jQuery.css(this.elem, this.prop)) || 0; }, // Start an animation from one number to another @@ -342,7 +342,7 @@ jQuery.fx.prototype = { // Simple 'show' function show: function() { // Remember where we started, so that we can go back to it later - this.options.orig[this.prop] = jQuery.style( this.elem, this.prop ); + this.options.orig[this.prop] = jQuery.css( this.elem, this.prop ); this.options.show = true; // Begin the animation @@ -357,7 +357,7 @@ jQuery.fx.prototype = { // Simple 'hide' function hide: function() { // Remember where we started, so that we can go back to it later - this.options.orig[this.prop] = jQuery.style( this.elem, this.prop ); + this.options.orig[this.prop] = jQuery.css( this.elem, this.prop ); this.options.hide = true; // Begin the animation @@ -403,7 +403,7 @@ jQuery.fx.prototype = { // Reset the properties, if the item has been hidden or shown if ( this.options.hide || this.options.show ) { for ( var p in this.options.curAnim ) { - jQuery.style(this.elem, p, this.options.orig[p]); + jQuery.css( this.elem, p, this.options.orig[p] ); } } @@ -460,7 +460,7 @@ jQuery.extend( jQuery.fx, { step: { opacity: function( fx ) { - jQuery.style(fx.elem, "opacity", fx.now); + jQuery.css( fx.elem, "opacity", fx.now ); }, _default: function( fx ) { diff --git a/src/offset.js b/src/offset.js index 0ce4c197..bab253ba 100644 --- a/src/offset.js +++ b/src/offset.js @@ -103,7 +103,7 @@ if ( "getBoundingClientRect" in document.documentElement ) { jQuery.offset = { initialize: function() { - var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.curCSS(body, "marginTop", true) ) || 0, + var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.css(body, "marginTop", undefined, true) ) || 0, html = "
"; jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } ); @@ -142,25 +142,25 @@ jQuery.offset = { jQuery.offset.initialize(); if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) { - top += parseFloat( jQuery.curCSS(body, "marginTop", true) ) || 0; - left += parseFloat( jQuery.curCSS(body, "marginLeft", true) ) || 0; + top += parseFloat( jQuery.css(body, "marginTop", undefined, true) ) || 0; + left += parseFloat( jQuery.css(body, "marginLeft", undefined, true) ) || 0; } return { top: top, left: left }; }, setOffset: function( elem, options, i ) { - var position = jQuery.curCSS( elem, "position" ); + var position = jQuery.css( elem, "position" ); // set position first, in-case top/left are set even on static elem if ( position === "static" ) { elem.style.position = "relative"; } - var curElem = jQuery( elem ), - curOffset = curElem.offset(), - curCSSTop = jQuery.curCSS( elem, "top", true ), - curCSSLeft = jQuery.curCSS( elem, "left", true ), + var curElem = jQuery( elem ), + curOffset = curElem.offset(), + curCSSTop = jQuery.css( elem, "top", undefined, true ), + curCSSLeft = jQuery.css( elem, "left", undefined, true ), calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1), props = {}, curPosition = {}, curTop, curLeft; @@ -210,12 +210,12 @@ jQuery.fn.extend({ // Subtract element margins // note: when an element has margin: auto the offsetLeft and marginLeft // are the same in Safari causing offset.left to incorrectly be 0 - offset.top -= parseFloat( jQuery.curCSS(elem, "marginTop", true) ) || 0; - offset.left -= parseFloat( jQuery.curCSS(elem, "marginLeft", true) ) || 0; + offset.top -= parseFloat( jQuery.css(elem, "marginTop", undefined, true) ) || 0; + offset.left -= parseFloat( jQuery.css(elem, "marginLeft", undefined, true) ) || 0; // Add offsetParent borders - parentOffset.top += parseFloat( jQuery.curCSS(offsetParent[0], "borderTopWidth", true) ) || 0; - parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], "borderLeftWidth", true) ) || 0; + parentOffset.top += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth", undefined, true) ) || 0; + parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth", undefined, true) ) || 0; // Subtract the two offsets return { From b920f0aeb483f4565ff2f3c1d2a328d5200b23f7 Mon Sep 17 00:00:00 2001 From: John Resig Date: Sun, 5 Sep 2010 11:01:27 -0400 Subject: [PATCH 06/21] Broke more of the property-specific CSS logic out of the jQuery.css() function. --- src/css.js | 64 +++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/src/css.js b/src/css.js index cd77ec1b..2da19b9c 100644 --- a/src/css.js +++ b/src/css.js @@ -1,22 +1,18 @@ -// exclude the following css properties to add px -var rexclude = /z-?index|font-?weight|opacity|zoom|line-?height/i, - ralpha = /alpha\([^)]*\)/, +var ralpha = /alpha\([^)]*\)/, ropacity = /opacity=([^)]*)/, - rfloat = /float/i, rdashAlpha = /-([a-z])/ig, rupper = /([A-Z])/g, rnumpx = /^-?\d+(?:px)?$/i, rnum = /^-?\d/, - cssShow = { position: "absolute", visibility: "hidden", display:"block" }, + cssShow = { position: "absolute", visibility: "hidden", display: "block" }, cssWidth = [ "Left", "Right" ], cssHeight = [ "Top", "Bottom" ], curCSS, // cache check for defaultView.getComputedStyle getComputedStyle = document.defaultView && document.defaultView.getComputedStyle, - // normalize float css property - styleFloat = jQuery.support.cssFloat ? "cssFloat" : "styleFloat", + fcamelCase = function( all, letter ) { return letter.toUpperCase(); }; @@ -28,7 +24,29 @@ jQuery.fn.css = function( name, value ) { }; jQuery.extend({ - cssHooks: {}, + cssHooks: { + opacity: { + get: function( elem ) { + // We should always get a number back from opacity + var ret = curCSS( elem, "opacity", "opacity" ); + return ret === "" ? "1" : ret; + } + } + }, + + // exclude the following css properties to add px + cssNumber: { + "zIndex": true, + "fontWeight": true, + "opacity": true, + "zoom": true, + "lineHeight": true + }, + + cssProps: { + // normalize float css property + "float": jQuery.support.cssFloat ? "cssFloat" : "styleFloat" + }, css: function( elem, name, value, force, extra ) { // don't set styles on text and comment nodes @@ -36,17 +54,13 @@ jQuery.extend({ return undefined; } - // Make sure we're using the right name for getting the float value - if ( rfloat.test( name ) ) { - name = styleFloat; - } + var ret, origName = name.replace( rdashAlpha, fcamelCase ), + style = elem.style || {}, hooks = jQuery.cssHooks[ origName ] || {}; - name = name.replace( rdashAlpha, fcamelCase ); - - var ret, style = elem.style || {}, hooks = jQuery.cssHooks[name] || {}; + name = jQuery.cssProps[ origName ] || origName; if ( value !== undefined ) { - if ( typeof value === "number" && !rexclude.test(name) ) { + if ( typeof value === "number" && !jQuery.cssNumber[ origName ] ) { value += "px"; } @@ -63,7 +77,7 @@ jQuery.extend({ ret = style[ name ]; } else if ( curCSS ) { - ret = curCSS( elem, name ); + ret = curCSS( elem, name, origName ); } return ret; @@ -104,7 +118,7 @@ jQuery.each(["height", "width"], function( i, name ) { set: function( elem, value ) { // ignore negative width and height values #1599 - elem.style[ name ] = Math.max( parseFloat(value), 0 ); + elem.style[ name ] = Math.max( parseFloat(value), 0 ) + "px"; } }; }); @@ -140,29 +154,19 @@ if ( !jQuery.support.opacity ) { } if ( getComputedStyle ) { - curCSS = function( elem, name ) { + curCSS = function( elem, newName, name ) { var ret, defaultView, computedStyle; - // Only "float" is needed here - if ( rfloat.test( name ) ) { - name = "float"; - } - name = name.replace( rupper, "-$1" ).toLowerCase(); if ( !(defaultView = elem.ownerDocument.defaultView) ) { - return null; + return undefined; } if ( (computedStyle = defaultView.getComputedStyle( elem, null )) ) { ret = computedStyle.getPropertyValue( name ); } - // We should always get a number back from opacity - if ( name === "opacity" && ret === "" ) { - ret = "1"; - } - return ret; }; From a44ec402771f6d622506f39073d0be260400dd21 Mon Sep 17 00:00:00 2001 From: jeresig Date: Wed, 8 Sep 2010 12:00:29 -0400 Subject: [PATCH 07/21] Make sure that jQuery works even when the individual modules are loaded separately AND jQuery.noConflict(true) is used. Fixes #7011. --- src/ajax.js | 4 ++++ src/attributes.js | 4 ++++ src/css.js | 4 ++++ src/data.js | 4 ++++ src/dimensions.js | 4 ++++ src/effects.js | 4 ++++ src/event.js | 4 ++++ src/manipulation.js | 6 +++++- src/offset.js | 4 ++++ src/queue.js | 4 ++++ src/support.js | 6 +++++- src/traversing.js | 4 ++++ test/unit/core.js | 3 ++- 13 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/ajax.js b/src/ajax.js index 2c4f13cb..52a5a22c 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + var jsc = jQuery.now(), rscript = //gi, rselectTextarea = /select|textarea/i, @@ -686,3 +688,5 @@ jQuery.extend( jQuery.ajax, { // For backwards compatibility jQuery.extend( jQuery.ajax ); + +})( jQuery ); diff --git a/src/attributes.js b/src/attributes.js index fecba661..4fa49b91 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + var rclass = /[\n\t]/g, rspace = /\s+/, rreturn = /\r/g, @@ -341,3 +343,5 @@ jQuery.extend({ } } }); + +})( jQuery ); diff --git a/src/css.js b/src/css.js index 2da19b9c..56249598 100644 --- a/src/css.js +++ b/src/css.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + var ralpha = /alpha\([^)]*\)/, ropacity = /opacity=([^)]*)/, rdashAlpha = /-([a-z])/ig, @@ -238,3 +240,5 @@ if ( jQuery.expr && jQuery.expr.filters ) { return !jQuery.expr.filters.hidden( elem ); }; } + +})( jQuery ); diff --git a/src/data.js b/src/data.js index d38d47d3..5404c936 100644 --- a/src/data.js +++ b/src/data.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + var windowData = {}; jQuery.extend({ @@ -165,3 +167,5 @@ jQuery.fn.extend({ }); } }); + +})( jQuery ); diff --git a/src/dimensions.js b/src/dimensions.js index 52fd0f86..58c16af8 100644 --- a/src/dimensions.js +++ b/src/dimensions.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + // Create innerHeight, innerWidth, outerHeight and outerWidth methods jQuery.each([ "Height", "Width" ], function( i, name ) { @@ -55,3 +57,5 @@ jQuery.each([ "Height", "Width" ], function( i, name ) { }; }); + +})( jQuery ); diff --git a/src/effects.js b/src/effects.js index d31e2e35..130b676f 100644 --- a/src/effects.js +++ b/src/effects.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + var elemdisplay = {}, rfxtypes = /toggle|show|hide/, rfxnum = /^([+\-]=)?([\d+.\-]+)(.*)$/, @@ -480,3 +482,5 @@ if ( jQuery.expr && jQuery.expr.filters ) { }).length; }; } + +})( jQuery ); diff --git a/src/event.js b/src/event.js index 45f2202c..f0b27de4 100644 --- a/src/event.js +++ b/src/event.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + var rnamespaces = /\.(.*)$/, fcleanup = function( nm ) { return nm.replace(/[^\w\s\.\|`]/g, function( ch ) { @@ -1109,3 +1111,5 @@ if ( window.attachEvent && !window.addEventListener ) { } }); } + +})( jQuery ); diff --git a/src/manipulation.js b/src/manipulation.js index cb898cc3..3054eea1 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g, rleadingWhitespace = /^\s+/, rxhtmlTag = /(<([\w:]+)[^>]*?)\/>/g, @@ -599,4 +601,6 @@ function evalScript( i, elem ) { if ( elem.parentNode ) { elem.parentNode.removeChild( elem ); } -} \ No newline at end of file +} + +})( jQuery ); diff --git a/src/offset.js b/src/offset.js index bab253ba..5d283aac 100644 --- a/src/offset.js +++ b/src/offset.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + if ( "getBoundingClientRect" in document.documentElement ) { jQuery.fn.offset = function( options ) { var elem = this[0]; @@ -281,3 +283,5 @@ function getWindow( elem ) { elem.defaultView || elem.parentWindow : false; } + +})( jQuery ); diff --git a/src/queue.js b/src/queue.js index e52f37b8..11c658f5 100644 --- a/src/queue.js +++ b/src/queue.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + jQuery.extend({ queue: function( elem, type, data ) { if ( !elem ) { @@ -88,3 +90,5 @@ jQuery.fn.extend({ return this.queue( type || "fx", [] ); } }); + +})( jQuery ); diff --git a/src/support.js b/src/support.js index cddd3dc3..febff159 100644 --- a/src/support.js +++ b/src/support.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + (function() { jQuery.support = {}; @@ -132,7 +134,7 @@ // release memory in IE root = script = div = all = a = null; -})(); +})( jQuery ); jQuery.props = { "for": "htmlFor", @@ -146,3 +148,5 @@ jQuery.props = { usemap: "useMap", frameborder: "frameBorder" }; + +})( jQuery ); diff --git a/src/traversing.js b/src/traversing.js index fde7219b..59110b09 100644 --- a/src/traversing.js +++ b/src/traversing.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + var runtil = /Until$/, rparentsprev = /^(?:parents|prevUntil|prevAll)/, // Note: This RegExp should be improved, or likely pulled from Sizzle @@ -271,3 +273,5 @@ function winnow( elements, qualifier, keep ) { return (jQuery.inArray( elem, qualifier ) >= 0) === keep; }); } + +})( jQuery ); diff --git a/test/unit/core.js b/test/unit/core.js index 3ba16a48..811d13fb 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -183,7 +183,7 @@ test("browser", function() { } test("noConflict", function() { - expect(6); + expect(7); var $$ = jQuery; @@ -196,6 +196,7 @@ test("noConflict", function() { equals( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" ); equals( jQuery, originaljQuery, "Make sure jQuery was reverted." ); equals( $, original$, "Make sure $ was reverted." ); + ok( $$("#main").html("test"), "Make sure that jQuery still works." ); jQuery = $$; }); From ad950c8c5992937640a1e1aca8d63bb476b001f6 Mon Sep 17 00:00:00 2001 From: jeresig Date: Thu, 9 Sep 2010 15:33:06 -0400 Subject: [PATCH 08/21] Landing a bunch of bug fixes from furf's pull request at eefcbaebb31b89b5eb360cd5ec6165b89c84e75f. --- src/css.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/css.js b/src/css.js index 56249598..f263de87 100644 --- a/src/css.js +++ b/src/css.js @@ -21,7 +21,7 @@ var ralpha = /alpha\([^)]*\)/, jQuery.fn.css = function( name, value ) { return jQuery.access( this, name, value, true, function( elem, name, value ) { - jQuery.css( elem, name, value ); + return jQuery.css( elem, name, value ); }); }; @@ -66,16 +66,16 @@ jQuery.extend({ value += "px"; } - if ( !("set" in hooks) || (value = hooks.set( elem, value )) === false ) { + if ( !("set" in hooks) || (value = hooks.set( elem, value )) === undefined ) { style[ name ] = value; } } else { - if ( "get" in hooks && (ret = hooks.get( elem, force, extra )) !== false ) { + if ( "get" in hooks && (ret = hooks.get( elem, force, extra )) !== undefined ) { return ret; } - if ( !force && name in style ) { + if ( !force && style && style[ name ] ) { ret = style[ name ]; } else if ( curCSS ) { @@ -108,6 +108,8 @@ jQuery.extend({ jQuery.each(["height", "width"], function( i, name ) { jQuery.cssHooks[ name ] = { get: function( elem, force, extra ) { + var val; + if ( elem.offsetWidth !== 0 ) { val = getWH( elem, name, extra ); @@ -116,11 +118,13 @@ jQuery.each(["height", "width"], function( i, name ) { val = getWH( elem, name, extra ); }); } + + return val; }, set: function( elem, value ) { // ignore negative width and height values #1599 - elem.style[ name ] = Math.max( parseFloat(value), 0 ) + "px"; + return Math.max( parseFloat(value), 0 ) + "px"; } }; }); @@ -174,7 +178,7 @@ if ( getComputedStyle ) { } else if ( document.documentElement.currentStyle ) { curCSS = function( elem, name ) { - var left, rsLeft, ret = elem.currentStyle[ name ]; + var left, rsLeft, ret = elem.currentStyle[ name ], style = elem.style; // From the awesome hack by Dean Edwards // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291 From 7c8588530abaf4e72154fe1f59c04aae217a32b8 Mon Sep 17 00:00:00 2001 From: jeresig Date: Thu, 9 Sep 2010 15:42:38 -0400 Subject: [PATCH 09/21] Make sure that string values are returned from the height/width CSS properties. --- src/css.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/css.js b/src/css.js index f263de87..f9212581 100644 --- a/src/css.js +++ b/src/css.js @@ -119,7 +119,7 @@ jQuery.each(["height", "width"], function( i, name ) { }); } - return val; + return val + "px"; }, set: function( elem, value ) { From a166860a19a5d0de4529e4df4cb6647b3812577e Mon Sep 17 00:00:00 2001 From: jeresig Date: Thu, 9 Sep 2010 15:45:24 -0400 Subject: [PATCH 10/21] Adding in a couple private variables to effects.js that were in css.js. --- src/effects.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/effects.js b/src/effects.js index 130b676f..352ad4ae 100644 --- a/src/effects.js +++ b/src/effects.js @@ -3,6 +3,7 @@ var elemdisplay = {}, rfxtypes = /toggle|show|hide/, rfxnum = /^([+\-]=)?([\d+.\-]+)(.*)$/, + rdashAlpha = /-([a-z])/ig, timerId, fxAttrs = [ // height animations @@ -11,7 +12,11 @@ var elemdisplay = {}, [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ], // opacity animations [ "opacity" ] - ]; + ], + + fcamelCase = function( all, letter ) { + return letter.toUpperCase(); + }; jQuery.fn.extend({ show: function( speed, callback ) { From 70377a65e2a1c8478f3dc1e0e125b7b25b5324f3 Mon Sep 17 00:00:00 2001 From: jeresig Date: Thu, 9 Sep 2010 15:48:28 -0400 Subject: [PATCH 11/21] Make sure that effect tests no longer reference the old CSS methods. --- test/unit/effects.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/effects.js b/test/unit/effects.js index 919e3ea4..9ff5014c 100644 --- a/test/unit/effects.js +++ b/test/unit/effects.js @@ -480,7 +480,7 @@ jQuery.each( { if ( t_h == "hide"||t_h == "show" ) equals(this.style.height.indexOf(f_h), 0, "Height must be reset to " + f_h + ": " + this.style.height); - var cur_o = jQuery.style(this, "opacity"); + var cur_o = jQuery.css(this, "opacity", undefined, true); if ( cur_o !== "" ) cur_o = parseFloat( cur_o ); if ( t_o == "hide"||t_o == "show" ) @@ -492,7 +492,7 @@ jQuery.each( { if ( t_o.constructor == Number ) { equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o); - ok(jQuery.curCSS(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o); + ok(jQuery.css(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o); } if ( t_w.constructor == Number ) { @@ -512,7 +512,7 @@ jQuery.each( { } if ( t_h == "show" ) { - var old_h = jQuery.curCSS(this, "height"); + var old_h = jQuery.css(this, "height"); jQuery(elem).append("
Some more text
and some more..."); ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto."); } From c36596ea58a69f7772b61829f8d0609a6fb1d9a0 Mon Sep 17 00:00:00 2001 From: jeresig Date: Thu, 9 Sep 2010 16:26:30 -0400 Subject: [PATCH 12/21] Make sure that the CSS hook getter isn't called if a forced computed style is done. --- src/css.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/css.js b/src/css.js index f9212581..7a96020d 100644 --- a/src/css.js +++ b/src/css.js @@ -71,11 +71,10 @@ jQuery.extend({ } } else { - if ( "get" in hooks && (ret = hooks.get( elem, force, extra )) !== undefined ) { + if ( !force && "get" in hooks && (ret = hooks.get( elem, force, extra )) !== undefined ) { return ret; - } - if ( !force && style && style[ name ] ) { + } else if ( !force && style[ name ] ) { ret = style[ name ]; } else if ( curCSS ) { @@ -214,14 +213,14 @@ function getWH( elem, name, extra ) { jQuery.each( which, function() { if ( !extra ) { - val -= parseFloat(jQuery.css( elem, "padding" + this, undefined, true)) || 0; + val -= parseFloat(jQuery.css( elem, "padding" + this, undefined, true )) || 0; } if ( extra === "margin" ) { - val += parseFloat(jQuery.css( elem, "margin" + this, undefined, true)) || 0; + val += parseFloat(jQuery.css( elem, "margin" + this, undefined, true )) || 0; } else { - val -= parseFloat(jQuery.css( elem, "border" + this + "Width", undefined, true)) || 0; + val -= parseFloat(jQuery.css( elem, "border" + this + "Width", undefined, true )) || 0; } }); From c168c30a6afb1b3af1dee81119dbfdd3bef50d61 Mon Sep 17 00:00:00 2001 From: jeresig Date: Thu, 9 Sep 2010 16:29:26 -0400 Subject: [PATCH 13/21] Another logic bug caught by furf in ad950c8c5992937640a1e1aca8d63bb476b001f6. --- src/css.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/css.js b/src/css.js index 7a96020d..dd0ca4e2 100644 --- a/src/css.js +++ b/src/css.js @@ -66,7 +66,7 @@ jQuery.extend({ value += "px"; } - if ( !("set" in hooks) || (value = hooks.set( elem, value )) === undefined ) { + if ( !("set" in hooks) || (value = hooks.set( elem, value )) !== undefined ) { style[ name ] = value; } From cb3a9c14f1d69a60777a887a7807d7d11c5cb0a1 Mon Sep 17 00:00:00 2001 From: jeresig Date: Thu, 9 Sep 2010 16:33:05 -0400 Subject: [PATCH 14/21] Make sure that height/width methods return numbers instead of strings. --- src/dimensions.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dimensions.js b/src/dimensions.js index 58c16af8..d3561e82 100644 --- a/src/dimensions.js +++ b/src/dimensions.js @@ -8,14 +8,14 @@ jQuery.each([ "Height", "Width" ], function( i, name ) { // innerHeight and innerWidth jQuery.fn["inner" + name] = function() { return this[0] ? - jQuery.css( this[0], type, undefined, false, "padding" ) : + parseFloat( jQuery.css( this[0], type, undefined, false, "padding" ), 10 ) : null; }; // outerHeight and outerWidth jQuery.fn["outer" + name] = function( margin ) { return this[0] ? - jQuery.css( this[0], type, undefined, false, margin ? "margin" : "border" ) : + parseFloat( jQuery.css( this[0], type, undefined, false, margin ? "margin" : "border" ), 10 ) : null; }; @@ -50,7 +50,7 @@ jQuery.each([ "Height", "Width" ], function( i, name ) { // Get or set width or height on the element size === undefined ? // Get width or height on the element - jQuery.css( elem, type ) : + parseFloat( jQuery.css( elem, type ), 10 ) : // Set the width or height on the element (default to pixels if value is unitless) this.css( type, typeof size === "string" ? size : size + "px" ); From 8b7015987cbd24c79f328bcf9260a6596a785bf5 Mon Sep 17 00:00:00 2001 From: jeresig Date: Thu, 9 Sep 2010 16:34:15 -0400 Subject: [PATCH 15/21] Only set height/width if it's a non-negative number (don't set it to 0). --- src/css.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/css.js b/src/css.js index dd0ca4e2..b95eb47d 100644 --- a/src/css.js +++ b/src/css.js @@ -123,7 +123,11 @@ jQuery.each(["height", "width"], function( i, name ) { set: function( elem, value ) { // ignore negative width and height values #1599 - return Math.max( parseFloat(value), 0 ) + "px"; + value = parseFloat(value); + + if ( value >= 0 ) { + return value + "px"; + } } }; }); From 2bda99c18a0372eed02472e9d318676e180281da Mon Sep 17 00:00:00 2001 From: John Resig Date: Mon, 13 Sep 2010 18:00:28 -0400 Subject: [PATCH 16/21] Making some more adjustments to handle auto CSS properties. --- src/css.js | 2 +- src/effects.js | 4 ++-- test/unit/effects.js | 18 +++++++++--------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/css.js b/src/css.js index b95eb47d..eb7567f7 100644 --- a/src/css.js +++ b/src/css.js @@ -77,7 +77,7 @@ jQuery.extend({ } else if ( !force && style[ name ] ) { ret = style[ name ]; - } else if ( curCSS ) { + } else if ( force !== false && curCSS ) { ret = curCSS( elem, name, origName ); } diff --git a/src/effects.js b/src/effects.js index 352ad4ae..4eb725bd 100644 --- a/src/effects.js +++ b/src/effects.js @@ -349,7 +349,7 @@ jQuery.fx.prototype = { // Simple 'show' function show: function() { // Remember where we started, so that we can go back to it later - this.options.orig[this.prop] = jQuery.css( this.elem, this.prop ); + this.options.orig[this.prop] = jQuery.css( this.elem, this.prop, undefined, false ); this.options.show = true; // Begin the animation @@ -364,7 +364,7 @@ jQuery.fx.prototype = { // Simple 'hide' function hide: function() { // Remember where we started, so that we can go back to it later - this.options.orig[this.prop] = jQuery.css( this.elem, this.prop ); + this.options.orig[this.prop] = jQuery.css( this.elem, this.prop, undefined, false ); this.options.hide = true; // Begin the animation diff --git a/test/unit/effects.js b/test/unit/effects.js index 9ff5014c..cb07f6a2 100644 --- a/test/unit/effects.js +++ b/test/unit/effects.js @@ -390,16 +390,16 @@ jQuery.each( { "CSS Auto": function(elem,prop){ jQuery(elem).addClass("auto" + prop) .text("This is a long string of text."); - return ""; + return prop == "opacity" ? 1 : ""; }, "JS Auto": function(elem,prop){ jQuery(elem).css(prop,"auto") .text("This is a long string of text."); - return ""; + return prop == "opacity" ? 1 : ""; }, "CSS 100": function(elem,prop){ jQuery(elem).addClass("large" + prop); - return ""; + return prop == "opacity" ? 1 : ""; }, "JS 100": function(elem,prop){ jQuery(elem).css(prop,prop == "opacity" ? 1 : "100px"); @@ -407,7 +407,7 @@ jQuery.each( { }, "CSS 50": function(elem,prop){ jQuery(elem).addClass("med" + prop); - return ""; + return prop == "opacity" ? 0.5 : ""; }, "JS 50": function(elem,prop){ jQuery(elem).css(prop,prop == "opacity" ? 0.50 : "50px"); @@ -415,7 +415,7 @@ jQuery.each( { }, "CSS 0": function(elem,prop){ jQuery(elem).addClass("no" + prop); - return ""; + return prop == "opacity" ? 0 : ""; }, "JS 0": function(elem,prop){ jQuery(elem).css(prop,prop == "opacity" ? 0 : "0px"); @@ -480,7 +480,7 @@ jQuery.each( { if ( t_h == "hide"||t_h == "show" ) equals(this.style.height.indexOf(f_h), 0, "Height must be reset to " + f_h + ": " + this.style.height); - var cur_o = jQuery.css(this, "opacity", undefined, true); + var cur_o = jQuery.css(this, "opacity"); if ( cur_o !== "" ) cur_o = parseFloat( cur_o ); if ( t_o == "hide"||t_o == "show" ) @@ -512,9 +512,9 @@ jQuery.each( { } if ( t_h == "show" ) { - var old_h = jQuery.css(this, "height"); - jQuery(elem).append("
Some more text
and some more..."); - ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto."); + var old_h = jQuery.css(this, "height", undefined, true); + jQuery(this).append("
Some more text
and some more..."); + notEqual(jQuery.css(this, "height", undefined, true), old_h, "Make sure height is auto. " + n); } start(); From 2131e1a7ad6d9df239ec00b301303e10dbd34d49 Mon Sep 17 00:00:00 2001 From: jeresig Date: Tue, 14 Sep 2010 09:52:13 -0400 Subject: [PATCH 17/21] Removing leftover debug code. --- test/unit/effects.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/effects.js b/test/unit/effects.js index cb07f6a2..bfd6ff4f 100644 --- a/test/unit/effects.js +++ b/test/unit/effects.js @@ -514,7 +514,7 @@ jQuery.each( { if ( t_h == "show" ) { var old_h = jQuery.css(this, "height", undefined, true); jQuery(this).append("
Some more text
and some more..."); - notEqual(jQuery.css(this, "height", undefined, true), old_h, "Make sure height is auto. " + n); + notEqual(jQuery.css(this, "height", undefined, true), old_h, "Make sure height is auto."); } start(); From 37b607d2815b893d13de4ac3461090d0dd46535e Mon Sep 17 00:00:00 2001 From: jeresig Date: Thu, 16 Sep 2010 10:00:56 -0400 Subject: [PATCH 18/21] Split apart jQuery.css into jQuery.css (computed values) and jQuery.style (currently set values). --- src/css.js | 86 +++++++++++++++++++++++++++++--------------- src/dimensions.js | 4 +-- src/effects.js | 22 ++++++------ src/offset.js | 18 +++++----- test/unit/css.js | 12 ++----- test/unit/effects.js | 10 +++--- 6 files changed, 86 insertions(+), 66 deletions(-) diff --git a/src/css.js b/src/css.js index eb7567f7..87f9abd7 100644 --- a/src/css.js +++ b/src/css.js @@ -26,6 +26,8 @@ jQuery.fn.css = function( name, value ) { }; jQuery.extend({ + // Add in style property hooks for overriding the default + // behavior of getting and setting a style property cssHooks: { opacity: { get: function( elem ) { @@ -36,7 +38,7 @@ jQuery.extend({ } }, - // exclude the following css properties to add px + // Exclude the following css properties to add px cssNumber: { "zIndex": true, "fontWeight": true, @@ -45,43 +47,67 @@ jQuery.extend({ "lineHeight": true }, + // Add in properties whose names you wish to fix before + // setting or getting the value cssProps: { // normalize float css property "float": jQuery.support.cssFloat ? "cssFloat" : "styleFloat" }, - css: function( elem, name, value, force, extra ) { - // don't set styles on text and comment nodes - if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) { + // Get and set the style property on a DOM Node + style: function( elem, name, value, extra ) { + // Don't set styles on text and comment nodes + if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style ) { return undefined; } + // Make sure that we're working with the right name var ret, origName = name.replace( rdashAlpha, fcamelCase ), - style = elem.style || {}, hooks = jQuery.cssHooks[ origName ] || {}; + style = elem.style, hooks = jQuery.cssHooks[ origName ]; name = jQuery.cssProps[ origName ] || origName; + // Check if we're setting a value if ( value !== undefined ) { + // If a number was passed in, add 'px' to the (except for certain CSS properties) if ( typeof value === "number" && !jQuery.cssNumber[ origName ] ) { value += "px"; } - if ( !("set" in hooks) || (value = hooks.set( elem, value )) !== undefined ) { + // If a hook was provided, use that value, otherwise just set the specified value + if ( !hooks || !("set" in hooks) || (value = hooks.set( elem, value )) !== undefined ) { style[ name ] = value; } } else { - if ( !force && "get" in hooks && (ret = hooks.get( elem, force, extra )) !== undefined ) { + // If a hook was provided get the non-computed value from there + if ( hooks && "get" in hooks && (ret = hooks.get( elem, false, extra )) !== undefined ) { return ret; - - } else if ( !force && style[ name ] ) { - ret = style[ name ]; - - } else if ( force !== false && curCSS ) { - ret = curCSS( elem, name, origName ); } + // Otherwise just get the value from the style object + return style[ name ]; + } + }, + + css: function( elem, name, value, extra ) { + // Make sure that we're working with the right name + var ret, origName = name.replace( rdashAlpha, fcamelCase ), + hooks = jQuery.cssHooks[ origName ]; + + name = jQuery.cssProps[ origName ] || origName; + + // Check if we're setting a value, just use jQuery.style (DEPRECATED) + if ( value !== undefined ) { + jQuery.style( elem, name, value ); + + // If a hook was provided get the computed value from there + } else if ( hooks && "get" in hooks && (ret = hooks.get( elem, true, extra )) !== undefined ) { return ret; + + // Otherwise, if a way to get the computed value exists, use that + } else if ( curCSS ) { + return curCSS( elem, name, origName ); } }, @@ -106,19 +132,21 @@ jQuery.extend({ jQuery.each(["height", "width"], function( i, name ) { jQuery.cssHooks[ name ] = { - get: function( elem, force, extra ) { + get: function( elem, computed, extra ) { var val; - if ( elem.offsetWidth !== 0 ) { - val = getWH( elem, name, extra ); - - } else { - jQuery.swap( elem, cssShow, function() { + if ( computed ) { + if ( elem.offsetWidth !== 0 ) { val = getWH( elem, name, extra ); - }); - } - return val + "px"; + } else { + jQuery.swap( elem, cssShow, function() { + val = getWH( elem, name, extra ); + }); + } + + return val + "px"; + } }, set: function( elem, value ) { @@ -134,9 +162,9 @@ jQuery.each(["height", "width"], function( i, name ) { if ( !jQuery.support.opacity ) { jQuery.cssHooks.opacity = { - get: function( elem, force ) { + get: function( elem, computed ) { // IE uses filters for opacity - return ropacity.test(elem.currentStyle.filter || "") ? + return ropacity.test((computed ? elem.currentStyle.filter : elem.style.filter) || "") ? (parseFloat(RegExp.$1) / 100) + "" : "1"; }, @@ -153,7 +181,7 @@ if ( !jQuery.support.opacity ) { "" : "alpha(opacity=" + value * 100 + ")"; - var filter = style.filter || jQuery.css( elem, "filter" ) || ""; + var filter = style.filter || elem.currentStyle.filter || ""; style.filter = ralpha.test(filter) ? filter.replace(ralpha, opacity) : @@ -217,14 +245,14 @@ function getWH( elem, name, extra ) { jQuery.each( which, function() { if ( !extra ) { - val -= parseFloat(jQuery.css( elem, "padding" + this, undefined, true )) || 0; + val -= parseFloat(jQuery.css( elem, "padding" + this )) || 0; } if ( extra === "margin" ) { - val += parseFloat(jQuery.css( elem, "margin" + this, undefined, true )) || 0; + val += parseFloat(jQuery.css( elem, "margin" + this )) || 0; } else { - val -= parseFloat(jQuery.css( elem, "border" + this + "Width", undefined, true )) || 0; + val -= parseFloat(jQuery.css( elem, "border" + this + "Width" )) || 0; } }); @@ -240,7 +268,7 @@ if ( jQuery.expr && jQuery.expr.filters ) { true : width > 0 && height > 0 && !skip ? false : - jQuery.css(elem, "display") === "none"; + (elem.style.display || jQuery.css( elem, "display" )) === "none"; }; jQuery.expr.filters.visible = function( elem ) { diff --git a/src/dimensions.js b/src/dimensions.js index d3561e82..698f5f4b 100644 --- a/src/dimensions.js +++ b/src/dimensions.js @@ -8,14 +8,14 @@ jQuery.each([ "Height", "Width" ], function( i, name ) { // innerHeight and innerWidth jQuery.fn["inner" + name] = function() { return this[0] ? - parseFloat( jQuery.css( this[0], type, undefined, false, "padding" ), 10 ) : + parseFloat( jQuery.css( this[0], type, undefined, "padding" ), 10 ) : null; }; // outerHeight and outerWidth jQuery.fn["outer" + name] = function( margin ) { return this[0] ? - parseFloat( jQuery.css( this[0], type, undefined, false, margin ? "margin" : "border" ), 10 ) : + parseFloat( jQuery.css( this[0], type, undefined, margin ? "margin" : "border" ), 10 ) : null; }; diff --git a/src/effects.js b/src/effects.js index 4eb725bd..eeab9b7a 100644 --- a/src/effects.js +++ b/src/effects.js @@ -29,7 +29,7 @@ jQuery.fn.extend({ this[i].style.display = old || ""; - if ( jQuery.css(this[i], "display") === "none" ) { + if ( jQuery.css( this[i], "display" ) === "none" ) { var nodeName = this[i].nodeName, display; if ( elemdisplay[ nodeName ] ) { @@ -71,7 +71,7 @@ jQuery.fn.extend({ for ( var i = 0, l = this.length; i < l; i++ ) { var old = jQuery.data(this[i], "olddisplay"); if ( !old && old !== "none" ) { - jQuery.data( this[i], "olddisplay", jQuery.css(this[i], "display") ); + jQuery.data( this[i], "olddisplay", jQuery.css( this[i], "display" ) ); } } @@ -139,7 +139,7 @@ jQuery.fn.extend({ if ( ( p === "height" || p === "width" ) && this.style ) { // Store display property - opt.display = jQuery.css(this, "display"); + opt.display = this.style.display; // Make sure that nothing sneaks out opt.overflow = this.style.overflow; @@ -316,13 +316,13 @@ jQuery.fx.prototype = { }, // Get the current size - cur: function( force ) { + cur: function() { if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) { return this.elem[ this.prop ]; } - var r = parseFloat(jQuery.css(this.elem, this.prop, undefined, force)); - return r && r > -10000 ? r : parseFloat(jQuery.css(this.elem, this.prop)) || 0; + var r = jQuery.css( this.elem, this.prop ); + return r && r > -10000 ? r : 0; }, // Start an animation from one number to another @@ -349,7 +349,7 @@ jQuery.fx.prototype = { // Simple 'show' function show: function() { // Remember where we started, so that we can go back to it later - this.options.orig[this.prop] = jQuery.css( this.elem, this.prop, undefined, false ); + this.options.orig[this.prop] = jQuery.style( this.elem, this.prop ); this.options.show = true; // Begin the animation @@ -364,7 +364,7 @@ jQuery.fx.prototype = { // Simple 'hide' function hide: function() { // Remember where we started, so that we can go back to it later - this.options.orig[this.prop] = jQuery.css( this.elem, this.prop, undefined, false ); + this.options.orig[this.prop] = jQuery.style( this.elem, this.prop ); this.options.hide = true; // Begin the animation @@ -397,7 +397,7 @@ jQuery.fx.prototype = { var old = jQuery.data(this.elem, "olddisplay"); this.elem.style.display = old ? old : this.options.display; - if ( jQuery.css(this.elem, "display") === "none" ) { + if ( jQuery.css( this.elem, "display" ) === "none" ) { this.elem.style.display = "block"; } } @@ -410,7 +410,7 @@ jQuery.fx.prototype = { // Reset the properties, if the item has been hidden or shown if ( this.options.hide || this.options.show ) { for ( var p in this.options.curAnim ) { - jQuery.css( this.elem, p, this.options.orig[p] ); + jQuery.style( this.elem, p, this.options.orig[p] ); } } @@ -467,7 +467,7 @@ jQuery.extend( jQuery.fx, { step: { opacity: function( fx ) { - jQuery.css( fx.elem, "opacity", fx.now ); + jQuery.style( fx.elem, "opacity", fx.now ); }, _default: function( fx ) { diff --git a/src/offset.js b/src/offset.js index 5d283aac..c472f985 100644 --- a/src/offset.js +++ b/src/offset.js @@ -105,7 +105,7 @@ if ( "getBoundingClientRect" in document.documentElement ) { jQuery.offset = { initialize: function() { - var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.css(body, "marginTop", undefined, true) ) || 0, + var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.css(body, "marginTop") ) || 0, html = "
"; jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } ); @@ -144,8 +144,8 @@ jQuery.offset = { jQuery.offset.initialize(); if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) { - top += parseFloat( jQuery.css(body, "marginTop", undefined, true) ) || 0; - left += parseFloat( jQuery.css(body, "marginLeft", undefined, true) ) || 0; + top += parseFloat( jQuery.css(body, "marginTop") ) || 0; + left += parseFloat( jQuery.css(body, "marginLeft") ) || 0; } return { top: top, left: left }; @@ -161,8 +161,8 @@ jQuery.offset = { var curElem = jQuery( elem ), curOffset = curElem.offset(), - curCSSTop = jQuery.css( elem, "top", undefined, true ), - curCSSLeft = jQuery.css( elem, "left", undefined, true ), + curCSSTop = jQuery.css( elem, "top" ), + curCSSLeft = jQuery.css( elem, "left" ), calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1), props = {}, curPosition = {}, curTop, curLeft; @@ -212,12 +212,12 @@ jQuery.fn.extend({ // Subtract element margins // note: when an element has margin: auto the offsetLeft and marginLeft // are the same in Safari causing offset.left to incorrectly be 0 - offset.top -= parseFloat( jQuery.css(elem, "marginTop", undefined, true) ) || 0; - offset.left -= parseFloat( jQuery.css(elem, "marginLeft", undefined, true) ) || 0; + offset.top -= parseFloat( jQuery.css(elem, "marginTop") ) || 0; + offset.left -= parseFloat( jQuery.css(elem, "marginLeft") ) || 0; // Add offsetParent borders - parentOffset.top += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth", undefined, true) ) || 0; - parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth", undefined, true) ) || 0; + parentOffset.top += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth") ) || 0; + parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth") ) || 0; // Subtract the two offsets return { diff --git a/test/unit/css.js b/test/unit/css.js index 5f8a74ae..99bab1fe 100644 --- a/test/unit/css.js +++ b/test/unit/css.js @@ -1,7 +1,7 @@ module("css"); test("css(String|Hash)", function() { - expect(30); + expect(28); equals( jQuery('#main').css("display"), 'none', 'Check for css property "display"'); @@ -19,10 +19,6 @@ test("css(String|Hash)", function() { equals( parseFloat(jQuery('#nothiddendiv').css('width')), width, 'Test negative width ignored') equals( parseFloat(jQuery('#nothiddendiv').css('height')), height, 'Test negative height ignored') - jQuery('#floatTest').css({styleFloat: 'right'}); - equals( jQuery('#floatTest').css('styleFloat'), 'right', 'Modified CSS float using "styleFloat": Assert float is right'); - jQuery('#floatTest').css({cssFloat: 'left'}); - equals( jQuery('#floatTest').css('cssFloat'), 'left', 'Modified CSS float using "cssFloat": Assert float is left'); jQuery('#floatTest').css({'float': 'right'}); equals( jQuery('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right'); jQuery('#floatTest').css({'font-size': '30px'}); @@ -65,7 +61,7 @@ test("css(String|Hash)", function() { }); test("css(String, Object)", function() { - expect(21); + expect(19); ok( jQuery('#nothiddendiv').is(':visible'), 'Modifying CSS display: Assert element is visible'); jQuery('#nothiddendiv').css("display", 'none'); ok( !jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is hidden'); @@ -75,10 +71,6 @@ test("css(String, Object)", function() { jQuery("#nothiddendiv").css("top", "-1em"); ok( jQuery("#nothiddendiv").css("top"), -16, "Check negative number in EMs." ); - jQuery('#floatTest').css('styleFloat', 'left'); - equals( jQuery('#floatTest').css('styleFloat'), 'left', 'Modified CSS float using "styleFloat": Assert float is left'); - jQuery('#floatTest').css('cssFloat', 'right'); - equals( jQuery('#floatTest').css('cssFloat'), 'right', 'Modified CSS float using "cssFloat": Assert float is right'); jQuery('#floatTest').css('float', 'left'); equals( jQuery('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left'); jQuery('#floatTest').css('font-size', '20px'); diff --git a/test/unit/effects.js b/test/unit/effects.js index bfd6ff4f..6129d5d3 100644 --- a/test/unit/effects.js +++ b/test/unit/effects.js @@ -512,9 +512,9 @@ jQuery.each( { } if ( t_h == "show" ) { - var old_h = jQuery.css(this, "height", undefined, true); + var old_h = jQuery.css(this, "height"); jQuery(this).append("
Some more text
and some more..."); - notEqual(jQuery.css(this, "height", undefined, true), old_h, "Make sure height is auto."); + notEqual(jQuery.css(this, "height"), old_h, "Make sure height is auto."); } start(); @@ -532,7 +532,7 @@ jQuery.fn.saveState = function(){ var self = this; self.save = {}; jQuery.each(check, function(i,c){ - self.save[c] = jQuery.css(self,c); + self.save[c] = self.style[ c ] || jQuery.css(self,c); }); }); }; @@ -540,8 +540,8 @@ jQuery.fn.saveState = function(){ jQuery.checkState = function(){ var self = this; jQuery.each(this.save, function(c,v){ - var cur = jQuery.css(self,c); - equals( v, cur, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")"); + var cur = self.style[ c ] || jQuery.css(self, c); + equals( cur, v, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")"); }); start(); } From 99fcf3babbba8b794fe8b0b2e20d517156cfd4d2 Mon Sep 17 00:00:00 2001 From: John Resig Date: Fri, 17 Sep 2010 13:38:13 -0400 Subject: [PATCH 19/21] Fixed some sloppy checks in the effects test suite, makes it more apparent where issues are happening. --- test/unit/effects.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/test/unit/effects.js b/test/unit/effects.js index 6129d5d3..32ce583b 100644 --- a/test/unit/effects.js +++ b/test/unit/effects.js @@ -475,15 +475,18 @@ jQuery.each( { equals( this.style.display, "block", "Showing, display should block: " + this.style.display); if ( t_w == "hide"||t_w == "show" ) - equals(this.style.width.indexOf(f_w), 0, "Width must be reset to " + f_w + ": " + this.style.width); + ok(f_w === "" ? this.style.width === f_w : this.style.width.indexOf(f_w) === 0, "Width must be reset to " + f_w + ": " + this.style.width); if ( t_h == "hide"||t_h == "show" ) - equals(this.style.height.indexOf(f_h), 0, "Height must be reset to " + f_h + ": " + this.style.height); + ok(f_h === "" ? this.style.height === f_h : this.style.height.indexOf(f_h) === 0, "Height must be reset to " + f_h + ": " + this.style.height); - var cur_o = jQuery.css(this, "opacity"); - if ( cur_o !== "" ) cur_o = parseFloat( cur_o ); + var cur_o = jQuery.style(this, "opacity"); + + if ( cur_o !== "" ) { + cur_o = jQuery.css(this, "opacity"); + } - if ( t_o == "hide"||t_o == "show" ) + if ( t_o == "hide" || t_o == "show" ) equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o); if ( t_w == "hide" ) From d1f3dc9a04225b327b6fd6deb9d3913918054a9f Mon Sep 17 00:00:00 2001 From: John Resig Date: Fri, 17 Sep 2010 13:51:12 -0400 Subject: [PATCH 20/21] Make sure that empty height/width values are still set. --- src/css.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/css.js b/src/css.js index 87f9abd7..9113be2a 100644 --- a/src/css.js +++ b/src/css.js @@ -150,11 +150,16 @@ jQuery.each(["height", "width"], function( i, name ) { }, set: function( elem, value ) { - // ignore negative width and height values #1599 - value = parseFloat(value); + if ( value !== "" ) { + // ignore negative width and height values #1599 + value = parseFloat(value); - if ( value >= 0 ) { - return value + "px"; + if ( value >= 0 ) { + return value + "px"; + } + + } else { + return value; } } }; From 192bab8ed6e8ad2b4c5de0c4660c80b6ecddfd33 Mon Sep 17 00:00:00 2001 From: John Resig Date: Fri, 17 Sep 2010 14:30:30 -0400 Subject: [PATCH 21/21] jQuery.css() returns a string value - handle this properly in the animation code. --- src/effects.js | 2 +- test/unit/effects.js | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/effects.js b/src/effects.js index eeab9b7a..9c8abe57 100644 --- a/src/effects.js +++ b/src/effects.js @@ -321,7 +321,7 @@ jQuery.fx.prototype = { return this.elem[ this.prop ]; } - var r = jQuery.css( this.elem, this.prop ); + var r = parseFloat( jQuery.css( this.elem, this.prop ), 10 ); return r && r > -10000 ? r : 0; }, diff --git a/test/unit/effects.js b/test/unit/effects.js index 32ce583b..5ac47115 100644 --- a/test/unit/effects.js +++ b/test/unit/effects.js @@ -240,11 +240,11 @@ test("stop()", function() { $foo.animate({ width:'show' }, 1000); setTimeout(function(){ var nw = $foo.width(); - ok( nw != w, "An animation occurred " + nw + "px " + w + "px"); + notEqual( nw, w, "An animation occurred " + nw + "px " + w + "px"); $foo.stop(); nw = $foo.width(); - ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px"); + notEqual( nw, w, "Stop didn't reset the animation " + nw + "px " + w + "px"); setTimeout(function(){ equals( nw, $foo.width(), "The animation didn't continue" ); start(); @@ -266,13 +266,12 @@ test("stop() - several in queue", function() { setTimeout(function(){ equals( $foo.queue().length, 3, "All 3 still in the queue" ); var nw = $foo.width(); - ok( nw != w, "An animation occurred " + nw + "px " + w + "px"); + notEqual( nw, w, "An animation occurred " + nw + "px " + w + "px"); $foo.stop(); nw = $foo.width(); - ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px"); - // Disabled, being flaky - //equals( $foo.queue().length, 1, "The next animation continued" ); + notEqual( nw, w, "Stop didn't reset the animation " + nw + "px " + w + "px"); + $foo.stop(true); start(); }, 100); @@ -517,7 +516,7 @@ jQuery.each( { if ( t_h == "show" ) { var old_h = jQuery.css(this, "height"); jQuery(this).append("
Some more text
and some more..."); - notEqual(jQuery.css(this, "height"), old_h, "Make sure height is auto."); + notEqual(jQuery.css(this, "height") + "px", old_h, "Make sure height is auto."); } start();