Skip to content

Commit

Permalink
CSS: simplify hack of css getter for the computed values
Browse files Browse the repository at this point in the history
  • Loading branch information
markelog committed Dec 23, 2014
1 parent d9d8906 commit dac716c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/css/curCSS.js
Expand Up @@ -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 );
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/data/testsuite.css
Expand Up @@ -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;
}
11 changes: 11 additions & 0 deletions test/unit/css.js
Expand Up @@ -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( "<div/>" ).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");

Expand Down

0 comments on commit dac716c

Please sign in to comment.