From dac716ca65a83bab2449ffa35c43b4b52e26a0c1 Mon Sep 17 00:00:00 2001 From: Oleg Gaidarenko Date: Thu, 18 Dec 2014 18:36:58 +0300 Subject: [PATCH] CSS: simplify hack of css getter for the computed values Ref gh-1906 --- src/css/curCSS.js | 12 ++---------- test/data/testsuite.css | 5 +++++ test/unit/css.js | 11 +++++++++++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/css/curCSS.js b/src/css/curCSS.js index 0b8f25997..c3210f186 100644 --- a/src/css/curCSS.js +++ b/src/css/curCSS.js @@ -72,7 +72,7 @@ if ( window.getComputedStyle ) { }; curCSS = function( elem, name, computed ) { - var left, rs, rsLeft, ret, + var left, ret, style = elem.style; computed = computed || getStyles( elem ); @@ -84,7 +84,7 @@ if ( window.getComputedStyle ) { ret = style[ name ]; } - // From the awesome hack by Dean Edwards + // Simplified 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 @@ -97,21 +97,13 @@ if ( window.getComputedStyle ) { // Remember the original values left = style.left; - rs = elem.runtimeStyle; - rsLeft = rs && rs.left; // Put in the new values to get a computed value out - if ( rsLeft ) { - rs.left = elem.currentStyle.left; - } style.left = name === "fontSize" ? "1em" : ret; ret = style.pixelLeft + "px"; // Revert the changed values style.left = left; - if ( rsLeft ) { - rs.left = rsLeft; - } } // Support: IE<9 diff --git a/test/data/testsuite.css b/test/data/testsuite.css index de5f3fb00..9f80473d8 100644 --- a/test/data/testsuite.css +++ b/test/data/testsuite.css @@ -163,3 +163,8 @@ section { background:#f0f; display:block; } -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; } +.get-computed-value { + padding-left: 50%; + width: 20%; + font-size: 2em; +} diff --git a/test/unit/css.js b/test/unit/css.js index a5bd304b2..ffe607b15 100644 --- a/test/unit/css.js +++ b/test/unit/css.js @@ -119,6 +119,17 @@ test("css(String|Hash)", function() { "Make sure that a string z-index is returned from css('z-index') (#14432)." ); }); +test( "css(String) computed values", 3, function() { + var div = jQuery( "
" ).addClass( "get-computed-value" ), + fixture = document.getElementById( "qunit-fixture" ); + + div.appendTo( fixture ); + strictEqual( div.css( "padding-left" ), "500px", "should get computed value for padding-left property" ); + strictEqual( div.css( "width" ), "200px", "should get computed value for width property" ); + strictEqual( div.css( "font-size" ), "32px", "should get computed value for font-size property" ); +}); + + test( "css() explicit and relative values", 29, function() { var $elem = jQuery("#nothiddendiv"); -- 2.39.5