Skip to content

Commit 9bea216

Browse files
committed
When the width/height computed unit is not pixels, return that instead. Fixes #10782.
- Reordered some of css.js in preparation for jshint undef.
1 parent 605e4bb commit 9bea216

File tree

2 files changed

+101
-89
lines changed

2 files changed

+101
-89
lines changed

src/css.js

Lines changed: 92 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -166,97 +166,16 @@ jQuery.extend({
166166
// DEPRECATED, Use jQuery.css() instead
167167
jQuery.curCSS = jQuery.css;
168168

169-
jQuery.each(["height", "width"], function( i, name ) {
170-
jQuery.cssHooks[ name ] = {
171-
get: function( elem, computed, extra ) {
172-
if ( computed ) {
173-
if ( elem.offsetWidth !== 0 ) {
174-
return getWidthOrHeight( elem, name, extra );
175-
} else {
176-
return jQuery.swap( elem, cssShow, function() {
177-
return getWidthOrHeight( elem, name, extra );
178-
});
179-
}
180-
}
181-
},
182-
183-
set: function( elem, value ) {
184-
return rnum.test( value ) ?
185-
value + "px" :
186-
value;
187-
}
188-
};
189-
});
190-
191-
if ( !jQuery.support.opacity ) {
192-
jQuery.cssHooks.opacity = {
193-
get: function( elem, computed ) {
194-
// IE uses filters for opacity
195-
return ropacity.test( (computed && elem.currentStyle ? elem.currentStyle.filter : elem.style.filter) || "" ) ?
196-
( parseFloat( RegExp.$1 ) / 100 ) + "" :
197-
computed ? "1" : "";
198-
},
199-
200-
set: function( elem, value ) {
201-
var style = elem.style,
202-
currentStyle = elem.currentStyle,
203-
opacity = jQuery.isNumeric( value ) ? "alpha(opacity=" + value * 100 + ")" : "",
204-
filter = currentStyle && currentStyle.filter || style.filter || "";
205-
206-
// IE has trouble with opacity if it does not have layout
207-
// Force it by setting the zoom level
208-
style.zoom = 1;
209-
210-
// if setting opacity to 1, and no other filters exist - attempt to remove filter attribute #6652
211-
if ( value >= 1 && jQuery.trim( filter.replace( ralpha, "" ) ) === "" ) {
212-
213-
// Setting style.filter to null, "" & " " still leave "filter:" in the cssText
214-
// if "filter:" is present at all, clearType is disabled, we want to avoid this
215-
// style.removeAttribute is IE Only, but so apparently is this code path...
216-
style.removeAttribute( "filter" );
217-
218-
// if there there is no filter style applied in a css rule, we are done
219-
if ( currentStyle && !currentStyle.filter ) {
220-
return;
221-
}
222-
}
223-
224-
// otherwise, set new filter values
225-
style.filter = ralpha.test( filter ) ?
226-
filter.replace( ralpha, opacity ) :
227-
filter + " " + opacity;
228-
}
229-
};
230-
}
231-
232-
jQuery(function() {
233-
// This hook cannot be added until DOM ready because the support test
234-
// for it is not run until after DOM ready
235-
if ( !jQuery.support.reliableMarginRight ) {
236-
jQuery.cssHooks.marginRight = {
237-
get: function( elem, computed ) {
238-
// WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
239-
// Work around by temporarily setting element display to inline-block
240-
return jQuery.swap( elem, { "display": "inline-block" }, function() {
241-
if ( computed ) {
242-
return curCSS( elem, "margin-right", "marginRight" );
243-
} else {
244-
return elem.style.marginRight;
245-
}
246-
});
247-
}
248-
};
249-
}
250-
});
251-
252169
if ( document.defaultView && document.defaultView.getComputedStyle ) {
253170
getComputedStyle = function( elem, name ) {
254-
var ret, defaultView, computedStyle, width, style = elem.style;
171+
var ret, defaultView, computedStyle, width,
172+
style = elem.style;
255173

256174
name = name.replace( rupper, "-$1" ).toLowerCase();
257175

258176
if ( (defaultView = elem.ownerDocument.defaultView) &&
259177
(computedStyle = defaultView.getComputedStyle( elem, null )) ) {
178+
260179
ret = computedStyle.getPropertyValue( name );
261180
if ( ret === "" && !jQuery.contains( elem.ownerDocument.documentElement, elem ) ) {
262181
ret = jQuery.style( elem, name );
@@ -349,6 +268,12 @@ function getWidthOrHeight( elem, name, extra ) {
349268
if ( val < 0 || val == null ) {
350269
val = elem.style[ name ];
351270
}
271+
272+
// Computed unit is not pixels. Stop here and return.
273+
if ( rnumnonpx.test(val) ) {
274+
return val;
275+
}
276+
352277
// Normalize "", auto, and prepare for extra
353278
val = parseFloat( val ) || 0;
354279

@@ -368,6 +293,89 @@ function getWidthOrHeight( elem, name, extra ) {
368293
return val + "px";
369294
}
370295

296+
jQuery.each([ "height", "width" ], function( i, name ) {
297+
jQuery.cssHooks[ name ] = {
298+
get: function( elem, computed, extra ) {
299+
if ( computed ) {
300+
if ( elem.offsetWidth !== 0 ) {
301+
return getWidthOrHeight( elem, name, extra );
302+
} else {
303+
return jQuery.swap( elem, cssShow, function() {
304+
return getWidthOrHeight( elem, name, extra );
305+
});
306+
}
307+
}
308+
},
309+
310+
set: function( elem, value ) {
311+
return rnum.test( value ) ?
312+
value + "px" :
313+
value;
314+
}
315+
};
316+
});
317+
318+
if ( !jQuery.support.opacity ) {
319+
jQuery.cssHooks.opacity = {
320+
get: function( elem, computed ) {
321+
// IE uses filters for opacity
322+
return ropacity.test( (computed && elem.currentStyle ? elem.currentStyle.filter : elem.style.filter) || "" ) ?
323+
( parseFloat( RegExp.$1 ) / 100 ) + "" :
324+
computed ? "1" : "";
325+
},
326+
327+
set: function( elem, value ) {
328+
var style = elem.style,
329+
currentStyle = elem.currentStyle,
330+
opacity = jQuery.isNumeric( value ) ? "alpha(opacity=" + value * 100 + ")" : "",
331+
filter = currentStyle && currentStyle.filter || style.filter || "";
332+
333+
// IE has trouble with opacity if it does not have layout
334+
// Force it by setting the zoom level
335+
style.zoom = 1;
336+
337+
// if setting opacity to 1, and no other filters exist - attempt to remove filter attribute #6652
338+
if ( value >= 1 && jQuery.trim( filter.replace( ralpha, "" ) ) === "" ) {
339+
340+
// Setting style.filter to null, "" & " " still leave "filter:" in the cssText
341+
// if "filter:" is present at all, clearType is disabled, we want to avoid this
342+
// style.removeAttribute is IE Only, but so apparently is this code path...
343+
style.removeAttribute( "filter" );
344+
345+
// if there there is no filter style applied in a css rule, we are done
346+
if ( currentStyle && !currentStyle.filter ) {
347+
return;
348+
}
349+
}
350+
351+
// otherwise, set new filter values
352+
style.filter = ralpha.test( filter ) ?
353+
filter.replace( ralpha, opacity ) :
354+
filter + " " + opacity;
355+
}
356+
};
357+
}
358+
359+
jQuery(function() {
360+
// This hook cannot be added until DOM ready because the support test
361+
// for it is not run until after DOM ready
362+
if ( !jQuery.support.reliableMarginRight ) {
363+
jQuery.cssHooks.marginRight = {
364+
get: function( elem, computed ) {
365+
// WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
366+
// Work around by temporarily setting element display to inline-block
367+
return jQuery.swap( elem, { "display": "inline-block" }, function() {
368+
if ( computed ) {
369+
return curCSS( elem, "margin-right", "marginRight" );
370+
} else {
371+
return elem.style.marginRight;
372+
}
373+
});
374+
}
375+
};
376+
}
377+
});
378+
371379
if ( jQuery.expr && jQuery.expr.filters ) {
372380
jQuery.expr.filters.hidden = function( elem ) {
373381
var width = elem.offsetWidth,

test/unit/css.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
module("css", { teardown: moduleTeardown });
22

33
test("css(String|Hash)", function() {
4-
expect( 44 );
4+
expect( 46 );
55

6-
equal( jQuery("#qunit-fixture").css("display"), "block", "Check for css property \"display\"");
6+
equal( jQuery("#qunit-fixture").css("display"), "block", "Check for css property \"display\"" );
7+
8+
ok( jQuery("#nothiddendiv").is(":visible"), "Modifying CSS display: Assert element is visible" );
9+
jQuery("#nothiddendiv").css({ display: "none" });
10+
ok( !jQuery("#nothiddendiv").is(":visible"), "Modified CSS display: Assert element is hidden" );
11+
var $child = jQuery("#nothiddendivchild").css({ width: "20%", height: "20%" });
12+
notEqual( $child.css("width"), "20px", "Retrieving a width percentage on the child of a hidden div returns percentage" );
13+
notEqual( $child.css("height"), "20px", "Retrieving a height percentage on the child of a hidden div returns percentage" );
714

8-
ok( jQuery("#nothiddendiv").is(":visible"), "Modifying CSS display: Assert element is visible");
9-
jQuery("#nothiddendiv").css({display: "none"});
10-
ok( !jQuery("#nothiddendiv").is(":visible"), "Modified CSS display: Assert element is hidden");
1115
jQuery("#nothiddendiv").css({display: "block"});
1216
ok( jQuery("#nothiddendiv").is(":visible"), "Modified CSS display: Assert element is visible");
1317
ok( jQuery(window).is(":visible"), "Calling is(':visible') on window does not throw an error in IE.");

0 commit comments

Comments
 (0)