Skip to content
Permalink
Browse files

Remove offset dependency from css. Move curCSS and getStyles to their…

… own modules. -39 bytes. Close gh-1360.
  • Loading branch information...
timmywil committed Sep 11, 2013
1 parent 641492b commit 4ded9be72a724a79d764a4fc72ea9450322235a8
Showing with 111 additions and 85 deletions.
  1. +4 −76 src/css.js
  2. +52 −0 src/css/curCSS.js
  3. +5 −0 src/css/var/getStyles.js
  4. +3 −0 src/css/var/rmargin.js
  5. +5 −0 src/css/var/rnumnonpx.js
  6. +42 −9 src/offset.js
@@ -6,18 +6,20 @@ var
jQuery = require( "./core" ),
pnum = require( "./var/pnum" ),
access = require( "./core/access" ),
rmargin = require( "./css/var/rmargin" ),
rnumnonpx = require( "./css/var/rnumnonpx" ),
cssExpand = require( "./css/var/cssExpand" ),
isHidden = require( "./css/var/isHidden" ),
getStyles = require( "./css/var/getStyles" ),
curCSS = require( "./css/curCSS" ),
support = require( "./css/support" ),
defaultDisplay = require( "./css/defaultDisplay" ),
data_priv = require( "./data/var/data_priv" ),

// swappable if display is none or starts with table except "table", "table-cell", or "table-caption"
// see here for display values: https://developer.mozilla.org/en-US/docs/CSS/display
rdisplayswap = /^(none|table(?!-c[ea]).+)/,
rmargin = /^margin/,
rnumsplit = new RegExp( "^(" + pnum + ")(.*)$", "i" ),
rnumnonpx = new RegExp( "^(" + pnum + ")(?!px)[a-z%]+$", "i" ),
rrelNum = new RegExp( "^([+-])=(" + pnum + ")", "i" ),

cssShow = { position: "absolute", visibility: "hidden", display: "block" },
@@ -33,8 +35,6 @@ require( "./core/init" );
require( "./css/swap" );
require( "./core/ready" );
require( "./selector" ); // contains
// Optional
require( "./offset" );

// return a css property mapped to a potentially vendor prefixed property
function vendorPropName( style, name ) {
@@ -59,51 +59,7 @@ function vendorPropName( style, name ) {
return origName;
}

// NOTE: we've included the "window" in window.getComputedStyle
// because jsdom on node.js will break without it.
function getStyles( elem ) {
return elem.ownerDocument.defaultView.getComputedStyle( elem, null );
}

function curCSS( elem, name, _computed ) {
var width, minWidth, maxWidth,
computed = _computed || getStyles( elem ),

// Support: IE9
// getPropertyValue is only needed for .css('filter') in IE9, see #12537
ret = computed ? computed.getPropertyValue( name ) || computed[ name ] : undefined,
style = elem.style;

if ( computed ) {

if ( ret === "" && !jQuery.contains( elem.ownerDocument, elem ) ) {
ret = jQuery.style( elem, name );
}

// Support: iOS < 6
// A tribute to the "awesome hack by Dean Edwards"
// iOS < 6 (at least) returns percentage for a larger set of values, but width seems to be reliably pixels
// this is against the CSSOM draft spec: http://dev.w3.org/csswg/cssom/#resolved-values
if ( rnumnonpx.test( ret ) && rmargin.test( name ) ) {

// Remember the original values
width = style.width;
minWidth = style.minWidth;
maxWidth = style.maxWidth;

// Put in the new values to get a computed value out
style.minWidth = style.maxWidth = style.width = ret;
ret = computed.width;

// Revert the changed values
style.width = width;
style.minWidth = minWidth;
style.maxWidth = maxWidth;
}
}

return ret;
}


function setPositiveNumber( elem, value, subtract ) {
@@ -436,34 +392,6 @@ jQuery.cssHooks.marginRight = {
}
};

// Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
// getComputedStyle returns percent when specified for top/left/bottom/right
// rather than make the css module depend on the offset module, we just check for it here
jQuery.each( [ "top", "left" ], function( i, prop ) {
jQuery.cssHooks[ prop ] = {
get: function( elem, computed ) {
if ( support.pixelPosition() || !jQuery.fn.position ) {
// Hook not needed, remove it.
// Since there are no other hooks for prop, remove the whole object.
delete jQuery.cssHooks[ prop ];
return;
}

jQuery.cssHooks[ prop ].get = function ( i, prop ) {
if ( computed ) {
computed = curCSS( elem, prop );
// if curCSS returns percentage, fallback to offset
return rnumnonpx.test( computed ) ?
jQuery( elem ).position()[ prop ] + "px" :
computed;
}
};

return jQuery.cssHooks[ prop ].get( i, prop );
}
};
});

// These hooks are used by animate to expand properties
jQuery.each({
margin: "",
@@ -0,0 +1,52 @@
define([
"../core",
"./var/rnumnonpx",
"./var/rmargin",
"./var/getStyles",
"../css", // Circular, but needs jQuery.style
"../selector" // contains
], function( jQuery, rnumnonpx, rmargin, getStyles ) {

function curCSS( elem, name, computed ) {
var width, minWidth, maxWidth, ret,
style = elem.style;

computed = computed || getStyles( elem );

// Support: IE9
// getPropertyValue is only needed for .css('filter') in IE9, see #12537
ret = computed ? computed.getPropertyValue( name ) || computed[ name ] : undefined;

if ( computed ) {

if ( ret === "" && !jQuery.contains( elem.ownerDocument, elem ) ) {
ret = jQuery.style( elem, name );
}

// Support: iOS < 6
// A tribute to the "awesome hack by Dean Edwards"
// iOS < 6 (at least) returns percentage for a larger set of values, but width seems to be reliably pixels
// this is against the CSSOM draft spec: http://dev.w3.org/csswg/cssom/#resolved-values
if ( rnumnonpx.test( ret ) && rmargin.test( name ) ) {

// Remember the original values
width = style.width;
minWidth = style.minWidth;
maxWidth = style.maxWidth;

// Put in the new values to get a computed value out
style.minWidth = style.maxWidth = style.width = ret;
ret = computed.width;

// Revert the changed values
style.width = width;
style.minWidth = minWidth;
style.maxWidth = maxWidth;
}
}

return ret;
}

return curCSS;
});
@@ -0,0 +1,5 @@
define(function() {
return function( elem ) {
return elem.ownerDocument.defaultView.getComputedStyle( elem, null );
};
});
@@ -0,0 +1,3 @@
define(function() {
return (/^margin/);
});
@@ -0,0 +1,5 @@
define([
"../../var/pnum"
], function( pnum ) {
return new RegExp( "^(" + pnum + ")(?!px)[a-z%]+$", "i" );
});
@@ -1,13 +1,17 @@
define([
"./core",
"./var/strundefined",
"./core/access",
"./core/init",
"./css",
"./selector" // contains
], function( jQuery, strundefined, access ) {
define(function( require ) {

var docElem = window.document.documentElement;
var
jQuery = require( "./core" ),
strundefined = require( "./var/strundefined" ),
access = require( "./core/access" ),
rnumnonpx = require( "./css/var/rnumnonpx" ),
curCSS = require( "./css/curCSS" ),
support = require( "./css/support" ),
docElem = window.document.documentElement;

require( "./core/init" );
require( "./css" );
require( "./selector" ); // contains

/**
* Gets a window from an element
@@ -177,5 +181,34 @@ jQuery.each( {scrollLeft: "pageXOffset", scrollTop: "pageYOffset"}, function( me
};
});

// Add the top/left cssHooks using jQuery.fn.position
// Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
// getComputedStyle returns percent when specified for top/left/bottom/right
// rather than make the css module depend on the offset module, we just check for it here
jQuery.each( [ "top", "left" ], function( i, prop ) {
jQuery.cssHooks[ prop ] = {
get: function( elem, computed ) {
if ( support.pixelPosition() ) {
// Hook not needed, remove it.
// Since there are no other hooks for prop, remove the whole object.
delete jQuery.cssHooks[ prop ];
return;
}

jQuery.cssHooks[ prop ].get = function ( i, prop ) {
if ( computed ) {
computed = curCSS( elem, prop );
// if curCSS returns percentage, fallback to offset
return rnumnonpx.test( computed ) ?
jQuery( elem ).position()[ prop ] + "px" :
computed;
}
};

return jQuery.cssHooks[ prop ].get( i, prop );
}
};
});

return jQuery;
});

1 comment on commit 4ded9be

@mgol

This comment has been minimized.

Copy link
Member

commented on 4ded9be Sep 11, 2013

@timmywil Remember to cherry-pick this commit to 1.x-master as well.

Please sign in to comment.
You can’t perform that action at this time.