Prevent IE from throwing errors when setting RGBA values. Fixes #5509.
[jquery.git] / test / unit / css.js
1 module("css");
2
3 test("css(String|Hash)", function() {
4         expect(29);
5
6         equals( jQuery('#main').css("display"), 'none', '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         jQuery('#nothiddendiv').css({display: 'block'});
12         ok( jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is visible');
13
14         // handle negative numbers by ignoring #1599, #4216
15         jQuery('#nothiddendiv').css({ 'width': 1, 'height': 1 });
16
17         var width = parseFloat(jQuery('#nothiddendiv').css('width')), height = parseFloat(jQuery('#nothiddendiv').css('height'));
18         jQuery('#nothiddendiv').css({ width: -1, height: -1 });
19         equals( parseFloat(jQuery('#nothiddendiv').css('width')), width, 'Test negative width ignored')
20         equals( parseFloat(jQuery('#nothiddendiv').css('height')), height, 'Test negative height ignored')
21
22         jQuery('#floatTest').css({'float': 'right'});
23         equals( jQuery('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right');
24         jQuery('#floatTest').css({'font-size': '30px'});
25         equals( jQuery('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px');
26
27         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
28                 jQuery('#foo').css({opacity: n});
29                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
30                 jQuery('#foo').css({opacity: parseFloat(n)});
31                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
32         });
33         jQuery('#foo').css({opacity: ''});
34         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
35
36         equals( jQuery('#empty').css('opacity'), '0', "Assert opacity is accessible via filter property set in stylesheet in IE" );
37         jQuery('#empty').css({ opacity: '1' });
38         equals( jQuery('#empty').css('opacity'), '1', "Assert opacity is taken from style attribute when set vs stylesheet in IE with filters" );
39
40         var div = jQuery('#nothiddendiv'), child = jQuery('#nothiddendivchild');
41
42         equals( parseInt(div.css("fontSize")), 16, "Verify fontSize px set." );
43         equals( parseInt(div.css("font-size")), 16, "Verify fontSize px set." );
44         equals( parseInt(child.css("fontSize")), 16, "Verify fontSize px set." );
45         equals( parseInt(child.css("font-size")), 16, "Verify fontSize px set." );
46
47         child.css("height", "100%");
48         equals( child[0].style.height, "100%", "Make sure the height is being set correctly." );
49
50         child.attr("class", "em");
51         equals( parseInt(child.css("fontSize")), 32, "Verify fontSize em set." );
52
53         // Have to verify this as the result depends upon the browser's CSS
54         // support for font-size percentages
55         child.attr("class", "prct");
56         var prctval = parseInt(child.css("fontSize")), checkval = 0;
57         if ( prctval === 16 || prctval === 24 ) {
58                 checkval = prctval;
59         }
60
61         equals( prctval, checkval, "Verify fontSize % set." );
62
63         equals( typeof child.css("width"), "string", "Make sure that a string width is returned from css('width')." );
64 });
65
66 test("css(String, Object)", function() {
67         expect(22);
68
69         ok( jQuery('#nothiddendiv').is(':visible'), 'Modifying CSS display: Assert element is visible');
70         jQuery('#nothiddendiv').css("display", 'none');
71         ok( !jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is hidden');
72         jQuery('#nothiddendiv').css("display", 'block');
73         ok( jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is visible');
74
75         jQuery("#nothiddendiv").css("top", "-1em");
76         ok( jQuery("#nothiddendiv").css("top"), -16, "Check negative number in EMs." );
77
78         jQuery('#floatTest').css('float', 'left');
79         equals( jQuery('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left');
80         jQuery('#floatTest').css('font-size', '20px');
81         equals( jQuery('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px');
82
83         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
84                 jQuery('#foo').css('opacity', n);
85                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
86                 jQuery('#foo').css('opacity', parseFloat(n));
87                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
88         });
89         jQuery('#foo').css('opacity', '');
90         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
91
92         // using contents will get comments regular, text, and comment nodes
93         var j = jQuery("#nonnodes").contents();
94         j.css("padding-left", "1px");
95         equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
96
97         // opera sometimes doesn't update 'display' correctly, see #2037
98         jQuery("#t2037")[0].innerHTML = jQuery("#t2037")[0].innerHTML
99         equals( jQuery("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
100
101         var div = jQuery("#nothiddendiv"),
102                 display = div.css("display"),
103                 ret = div.css("display", undefined);
104
105         equals( ret, div, "Make sure setting undefined returns the original set." );
106         equals( div.css("display"), display, "Make sure that the display wasn't changed." );
107
108         // Test for Bug #5509
109         var success = true;
110         try {
111                 jQuery('#foo').css("backgroundColor", "rgba(0, 0, 0, 0.1)");
112         }
113         catch (e) {
114                 success = false;
115         }
116         ok( success, "Setting RGBA values does not throw Error" );
117 });
118
119 if(jQuery.browser.msie) {
120   test("css(String, Object) for MSIE", function() {
121     // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
122                 jQuery('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
123         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
124
125     var filterVal = "progid:DXImageTransform.Microsoft.Alpha(opacity=30) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
126     var filterVal2 = "progid:DXImageTransform.Microsoft.alpha(opacity=100) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
127     var filterVal3 = "progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
128     jQuery('#foo').css("filter", filterVal);
129     equals( jQuery('#foo').css("filter"), filterVal, "css('filter', val) works" );
130     jQuery('#foo').css("opacity", 1);
131     equals( jQuery('#foo').css("filter"), filterVal2, "Setting opacity in IE doesn't duplicate opacity filter" );
132     equals( jQuery('#foo').css("opacity"), 1, "Setting opacity in IE with other filters works" );
133     jQuery('#foo').css("filter", filterVal3).css("opacity", 1);
134     ok( jQuery('#foo').css("filter").indexOf(filterVal3) !== -1, "Setting opacity in IE doesn't clobber other filters" );
135   });
136 }
137
138 test("css(String, Function)", function() {
139         expect(3);
140                 
141         var sizes = ["10px", "20px", "30px"];
142         
143         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
144                                  "<div class='cssFunction'></div>" + 
145                                  "<div class='cssFunction'></div></div>")
146                 .appendTo("body");
147         
148         var index = 0;
149         
150         jQuery("#cssFunctionTest div").css("font-size", function() {
151                 var size = sizes[index];
152                 index++;
153                 return size;
154         });
155                 
156         index = 0;
157         
158         jQuery("#cssFunctionTest div").each(function() {
159                 var computedSize = jQuery(this).css("font-size")
160                 var expectedSize = sizes[index]
161                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
162                 index++;
163         });
164
165         jQuery("#cssFunctionTest").remove();
166 });
167
168 test("css(String, Function) with incoming value", function() {
169         expect(3);
170                 
171         var sizes = ["10px", "20px", "30px"];
172         
173         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
174                                  "<div class='cssFunction'></div>" + 
175                                  "<div class='cssFunction'></div></div>")
176                 .appendTo("body");
177         
178         var index = 0;
179         
180         jQuery("#cssFunctionTest div").css("font-size", function() {
181                 var size = sizes[index];
182                 index++;
183                 return size;
184         });
185                 
186         index = 0;
187         
188         jQuery("#cssFunctionTest div").css("font-size", function(i, computedSize) {
189                 var expectedSize = sizes[index]
190                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
191                 index++;
192                 return computedSize;
193         });
194
195         jQuery("#cssFunctionTest").remove();
196 });
197
198 test("css(Object) where values are Functions", function() {
199         expect(3);
200                 
201         var sizes = ["10px", "20px", "30px"];
202         
203         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
204                                  "<div class='cssFunction'></div>" + 
205                                  "<div class='cssFunction'></div></div>")
206                 .appendTo("body");
207
208         var index = 0;
209         
210         jQuery("#cssFunctionTest div").css({fontSize: function() {
211                 var size = sizes[index];
212                 index++;
213                 return size;
214         }});
215                 
216         index = 0;
217                 
218         jQuery("#cssFunctionTest div").each(function() {
219                 var computedSize = jQuery(this).css("font-size")
220                 var expectedSize = sizes[index]
221                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
222                 index++;
223         });
224                 
225         jQuery("#cssFunctionTest").remove();
226 });
227
228 test("css(Object) where values are Functions with incoming values", function() {
229         expect(3);
230                 
231         var sizes = ["10px", "20px", "30px"];
232         
233         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
234                                  "<div class='cssFunction'></div>" + 
235                                  "<div class='cssFunction'></div></div>")
236                 .appendTo("body");
237
238         var index = 0;
239         
240         jQuery("#cssFunctionTest div").css({fontSize: function() {
241                 var size = sizes[index];
242                 index++;
243                 return size;
244         }});
245                 
246         index = 0;
247                 
248         jQuery("#cssFunctionTest div").css({"font-size": function(i, computedSize) {
249                 var expectedSize = sizes[index]
250                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
251                 index++;
252                 return computedSize;
253         }});
254                 
255         jQuery("#cssFunctionTest").remove();
256 });
257
258 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
259         expect(4);
260
261         var $checkedtest = jQuery("#checkedtest");
262         // IE6 was clearing "checked" in jQuery.css(elem, "height");
263         jQuery.css($checkedtest[0], "height");
264         ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
265         ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
266         ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
267         ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
268 });