Skip to content

Commit 9ca2e6a

Browse files
Fix issue where getOpacity would return a string, not a number, in newer versions of IE.
Also fix documentation that incorrectly claimed `getOpacity` was designed to return a string. [close prototypejs#116]
1 parent 94e2863 commit 9ca2e6a

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/prototype/dom/dom.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,7 +2792,7 @@
27922792

27932793

27942794
/**
2795-
* Element.getStyle(@element, style) -> String | null
2795+
* Element.getStyle(@element, style) -> String | Number | null
27962796
* - style (String): The property name to be retrieved.
27972797
*
27982798
* Returns the given CSS property value of `element`. The property can be
@@ -2805,6 +2805,10 @@
28052805
* (fully transparent) and `1` (fully opaque), position properties
28062806
* (`left`, `top`, `right` and `bottom`) and when getting the dimensions
28072807
* (`width` or `height`) of hidden elements.
2808+
*
2809+
* If a value is present, it will be returned as a string — except
2810+
* for `opacity`, which returns a number between `0` and `1` just as
2811+
* [[Element.getOpacity]] does.
28082812
*
28092813
* ##### Examples
28102814
*
@@ -2915,9 +2919,12 @@
29152919
value = element.currentStyle[style];
29162920
}
29172921

2918-
if (style === 'opacity' && !STANDARD_CSS_OPACITY_SUPPORTED)
2919-
return getOpacity_IE(element);
2920-
2922+
if (style === 'opacity') {
2923+
if (!STANDARD_CSS_OPACITY_SUPPORTED)
2924+
return getOpacity_IE(element);
2925+
else return value ? parseFloat(value) : 1.0;
2926+
}
2927+
29212928
if (value === 'auto') {
29222929
// If we need a dimension, return null for hidden elements, but return
29232930
// pixel values for visible elements.
@@ -3006,7 +3013,7 @@
30063013

30073014

30083015
/**
3009-
* Element.getOpacity(@element) -> String | null
3016+
* Element.getOpacity(@element) -> Number | null
30103017
*
30113018
* Returns the opacity of the element.
30123019
**/

test/unit/dom_test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,9 @@ new Test.Unit.Runner({
10381038
},
10391039

10401040
testElementGetOpacity: function() {
1041-
this.assertEqual(0.45, $('op1').setOpacity(0.45).getOpacity());
1041+
var opacity = $('op1').setOpacity(0.45).getOpacity();
1042+
this.assertEqual(0.45, opacity);
1043+
this.assertEqual('number', typeof opacity, 'opacity should be a string, not a number');
10421044
},
10431045

10441046
testElementReadAttribute: function() {

0 commit comments

Comments
 (0)