Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added css hook to work around bug in WebKit computed margin-right. Fi…

…xes #3333 - .css("marginRight") is incorrect in WebKit
  • Loading branch information...
commit c3c507e900fceb419628157504004ab2813b7d01 1 parent 0cf336d
@rdworth rdworth authored
Showing with 44 additions and 1 deletion.
  1. +19 −0 src/css.js
  2. +13 −1 src/support.js
  3. +12 −0 test/unit/css.js
View
19 src/css.js
@@ -240,6 +240,25 @@ if ( !jQuery.support.opacity ) {
};
}
+jQuery(function() {
+ // This hook cannot be added until DOM ready because the support test
+ // for it is not run until after DOM ready
+ if ( !jQuery.support.reliableMarginRight ) {
+ jQuery.cssHooks.marginRight = {
+ get: function( elem, computed ) {
+ // WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
+ // Work around by temporarily setting element display to inline-block
+ var ret = "0px",
+ display = elem.style.display;
+ elem.style.display = "inline-block";
+ ret = getComputedStyle( elem, "margin-right", "margin-right" );
+ elem.style.display = display;
@timmywil Collaborator

This functionality is already written in jQuery.swap. You could utilize that to save space. There's an old pull where I wrote a CSS hook for marginRight that's shorter. You could just swap display instead if you'd like to stick with that. See #208

@rdworth
rdworth added a note

Thanks for the help #287

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return ret;
+ }
+ }
+ }
+});
+
if ( document.defaultView && document.defaultView.getComputedStyle ) {
getComputedStyle = function( elem, newName, name ) {
var ret, defaultView, computedStyle;
View
14 src/support.js
@@ -67,7 +67,8 @@
boxModel: null,
inlineBlockNeedsLayout: false,
shrinkWrapBlocks: false,
- reliableHiddenOffsets: true
+ reliableHiddenOffsets: true,
+ reliableMarginRight: true
};
input.checked = true;
@@ -188,6 +189,17 @@
jQuery.support.reliableHiddenOffsets = jQuery.support.reliableHiddenOffsets && tds[0].offsetHeight === 0;
div.innerHTML = "";
+ // Check if div with explicit width and no margin-right incorrectly
+ // gets computed margin-right based on width of container. For more
+ // info see bug #3333
+ // Fails in WebKit before Feb 2011 nightlies
+ // WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
+ if ( document.defaultView && document.defaultView.getComputedStyle ) {
+ div.style.width = "1px";
+ div.style.marginRight = "0";
+ jQuery.support.reliableMarginRight = ( parseInt(document.defaultView.getComputedStyle(div).marginRight, 10) || 0 ) === 0;
+ }
+
body.removeChild( div ).style.display = "none";
div = tds = null;
});
View
12 test/unit/css.js
@@ -333,3 +333,15 @@ test("internal ref to elem.runtimeStyle (bug #7608)", function () {
ok( result, "elem.runtimeStyle does not throw exception" );
});
+
+test("marginRight computed style (bug #3333)", function() {
+ expect(1);
+
+ var $div = jQuery("#foo");
+ $div.css({
+ width: "1px",
+ marginRight: 0
+ });
+
+ equals($div.css("marginRight"), "0px");
+});
Please sign in to comment.
Something went wrong with that request. Please try again.