Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make left/top auto value consistent across browsers #1241

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/css.js
Expand Up @@ -506,9 +506,16 @@ jQuery(function() {
jQuery.cssHooks[ prop ] = {
get: function( elem, computed ) {
if ( computed ) {
computed = curCSS( elem, prop );
// if curCSS returns percentage, fallback to offset
return rnumnonpx.test( computed ) ?
var isAutoPosition,
elStyles = getStyles( elem ),
position = curCSS( elem, "position", elStyles );
computed = curCSS( elem, prop, elStyles );
isAutoPosition = computed === "auto";
if ( isAutoPosition && position === "relative" ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right this is needed... It's a lot of bytes, though, I wonder if there's a shorter way to achieve that.

return "0px";
}
// if curCSS returns percentage or auto, fallback to offset
return isAutoPosition && position !== "static" || rnumnonpx.test( computed ) ?
jQuery( elem ).position()[ prop ] + "px" :
computed;
}
Expand Down
7 changes: 4 additions & 3 deletions src/support.js
Expand Up @@ -62,7 +62,7 @@ jQuery.support = (function( support ) {

// Run tests that need a body at doc ready
jQuery(function() {
var container, marginDiv,
var container, marginDiv, divStyle,
// Support: Firefox, Android 2.3 (Prefixed box-sizing versions).
divReset = "padding:0;margin:0;border:0;display:block;-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box",
body = document.getElementsByTagName("body")[ 0 ];
Expand All @@ -89,8 +89,9 @@ jQuery.support = (function( support ) {

// Use window.getComputedStyle because jsdom on node.js will break without it.
if ( window.getComputedStyle ) {
support.pixelPosition = ( window.getComputedStyle( div, null ) || {} ).top !== "1%";
support.boxSizingReliable = ( window.getComputedStyle( div, null ) || { width: "4px" } ).width === "4px";
divStyle = window.getComputedStyle( div, null ) || { width: "4px" };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds 5 bytes but prevents getComputedStyle to be invoked twice which is nice. @mikesherov, what's your opinion?

support.pixelPosition = divStyle.top !== "1%" && divStyle.left !== "auto";
support.boxSizingReliable = divStyle.width === "4px";

// Support: Android 2.3
// Check if div with explicit width and no margin-right incorrectly
Expand Down
13 changes: 13 additions & 0 deletions test/unit/css.js
Expand Up @@ -874,6 +874,19 @@ test( "css opacity consistency across browsers (#12685)", function() {
equal( Math.round( el.css("opacity") * 100 ), 20, "remove opacity override" );
});

test( "css left/top auto consistency across browsers (#13767)", function() {
expect( 4 );

var fixture = jQuery("#qunit-fixture"),
el = jQuery("<div style='position: relative;padding: 20px;'>" +
"<div style='position: absolute'></div><span></span><s style='position: fixed'></s></div>").appendTo(fixture);

equal( el.css("left"), "0px" );
equal( el.find("div").css("top"), "20px" );
equal( el.find("span").css("top"), "auto" );
notEqual( el.find("s").css("top").indexOf("px"), -1 );
});

test( ":visible/:hidden selectors", function() {
expect( 13 );

Expand Down