Build: update grunt-jscs-checker and pass with the new rules
[jquery.git] / src / css / curCSS.js
blob66602cc80f90cc9469818efe84e5de8cd21013b1
1 define([
2         "exports",
3         "../core",
4         "./var/rnumnonpx",
5         "./var/rmargin",
6         "../selector" // contains
7 ], function( exports, jQuery, rnumnonpx, rmargin ) {
9 var getStyles, curCSS,
10         rposition = /^(top|right|bottom|left)$/;
12 if ( window.getComputedStyle ) {
13         getStyles = function( elem ) {
14                 // Support: IE<=11+, Firefox<=30+ (#15098, #14150)
15                 // IE throws on elements created in popups
16                 // FF meanwhile throws on frame elements through "defaultView.getComputedStyle"
17                 if ( elem.ownerDocument.defaultView.opener ) {
18                         return elem.ownerDocument.defaultView.getComputedStyle( elem, null );
19                 }
21                 return window.getComputedStyle( elem, null );
22         };
24         curCSS = function( elem, name, computed ) {
25                 var width, minWidth, maxWidth, ret,
26                         style = elem.style;
28                 computed = computed || getStyles( elem );
30                 // getPropertyValue is only needed for .css('filter') in IE9, see #12537
31                 ret = computed ? computed.getPropertyValue( name ) || computed[ name ] : undefined;
33                 if ( computed ) {
35                         if ( ret === "" && !jQuery.contains( elem.ownerDocument, elem ) ) {
36                                 ret = jQuery.style( elem, name );
37                         }
39                         // A tribute to the "awesome hack by Dean Edwards"
40                         // Chrome < 17 and Safari 5.0 uses "computed value"
41                         // instead of "used value" for margin-right
42                         // Safari 5.1.7 (at least) returns percentage for a larger set of values,
43                         // but width seems to be reliably pixels
44                         // this is against the CSSOM draft spec:
45                         // http://dev.w3.org/csswg/cssom/#resolved-values
46                         if ( rnumnonpx.test( ret ) && rmargin.test( name ) ) {
48                                 // Remember the original values
49                                 width = style.width;
50                                 minWidth = style.minWidth;
51                                 maxWidth = style.maxWidth;
53                                 // Put in the new values to get a computed value out
54                                 style.minWidth = style.maxWidth = style.width = ret;
55                                 ret = computed.width;
57                                 // Revert the changed values
58                                 style.width = width;
59                                 style.minWidth = minWidth;
60                                 style.maxWidth = maxWidth;
61                         }
62                 }
64                 // Support: IE
65                 // IE returns zIndex value as an integer.
66                 return ret === undefined ?
67                         ret :
68                         ret + "";
69         };
70 } else if ( document.documentElement.currentStyle ) {
71         getStyles = function( elem ) {
72                 return elem.currentStyle;
73         };
75         curCSS = function( elem, name, computed ) {
76                 var left, rs, rsLeft, ret,
77                         style = elem.style;
79                 computed = computed || getStyles( elem );
80                 ret = computed ? computed[ name ] : undefined;
82                 // Avoid setting ret to empty string here
83                 // so we don't default to auto
84                 if ( ret == null && style && style[ name ] ) {
85                         ret = style[ name ];
86                 }
88                 // From the awesome hack by Dean Edwards
89                 // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291
91                 // If we're not dealing with a regular pixel number
92                 // but a number that has a weird ending, we need to convert it to pixels
93                 // but not position css attributes, as those are
94                 // proportional to the parent element instead
95                 // and we can't measure the parent instead because it
96                 // might trigger a "stacking dolls" problem
97                 if ( rnumnonpx.test( ret ) && !rposition.test( name ) ) {
99                         // Remember the original values
100                         left = style.left;
101                         rs = elem.runtimeStyle;
102                         rsLeft = rs && rs.left;
104                         // Put in the new values to get a computed value out
105                         if ( rsLeft ) {
106                                 rs.left = elem.currentStyle.left;
107                         }
108                         style.left = name === "fontSize" ? "1em" : ret;
109                         ret = style.pixelLeft + "px";
111                         // Revert the changed values
112                         style.left = left;
113                         if ( rsLeft ) {
114                                 rs.left = rsLeft;
115                         }
116                 }
118                 // Support: IE
119                 // IE returns zIndex value as an integer.
120                 return ret === undefined ?
121                         ret :
122                         ret + "" || "auto";
123         };
126 exports.getStyles = getStyles;
127 exports.curCSS = curCSS;