Name the try/catch arg to satisfy JSLint - thanks to @rwldrn in 873c28425fb64fdb48cfa...
[jquery.git] / test / unit / css.js
1 module("css");
2
3 test("css(String|Hash)", function() {
4         expect(33);
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         var old = child[0].style.height;
66
67         // Test NaN
68         child.css("height", parseFloat("zoo"));
69         equals( child[0].style.height, old, "Make sure height isn't changed on NaN." );
70
71         // Test null
72         child.css("height", null);
73         equals( child[0].style.height, old, "Make sure height isn't changed on null." );
74
75         old = child[0].style.fontSize;
76
77         // Test NaN
78         child.css("font-size", parseFloat("zoo"));
79         equals( child[0].style.fontSize, old, "Make sure font-size isn't changed on NaN." );
80
81         // Test null
82         child.css("font-size", null);
83         equals( child[0].style.fontSize, old, "Make sure font-size isn't changed on null." );
84 });
85
86 test("css(String, Object)", function() {
87         expect(22);
88
89         ok( jQuery('#nothiddendiv').is(':visible'), 'Modifying CSS display: Assert element is visible');
90         jQuery('#nothiddendiv').css("display", 'none');
91         ok( !jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is hidden');
92         jQuery('#nothiddendiv').css("display", 'block');
93         ok( jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is visible');
94
95         jQuery("#nothiddendiv").css("top", "-1em");
96         ok( jQuery("#nothiddendiv").css("top"), -16, "Check negative number in EMs." );
97
98         jQuery('#floatTest').css('float', 'left');
99         equals( jQuery('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left');
100         jQuery('#floatTest').css('font-size', '20px');
101         equals( jQuery('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px');
102
103         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
104                 jQuery('#foo').css('opacity', n);
105                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
106                 jQuery('#foo').css('opacity', parseFloat(n));
107                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
108         });
109         jQuery('#foo').css('opacity', '');
110         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
111
112         // using contents will get comments regular, text, and comment nodes
113         var j = jQuery("#nonnodes").contents();
114         j.css("padding-left", "1px");
115         equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
116
117         // opera sometimes doesn't update 'display' correctly, see #2037
118         jQuery("#t2037")[0].innerHTML = jQuery("#t2037")[0].innerHTML
119         equals( jQuery("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
120
121         var div = jQuery("#nothiddendiv"),
122                 display = div.css("display"),
123                 ret = div.css("display", undefined);
124
125         equals( ret, div, "Make sure setting undefined returns the original set." );
126         equals( div.css("display"), display, "Make sure that the display wasn't changed." );
127
128         // Test for Bug #5509
129         var success = true;
130         try {
131                 jQuery('#foo').css("backgroundColor", "rgba(0, 0, 0, 0.1)");
132         }
133         catch (e) {
134                 success = false;
135         }
136         ok( success, "Setting RGBA values does not throw Error" );
137 });
138
139 if(jQuery.browser.msie) {
140   test("css(String, Object) for MSIE", function() {
141     // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
142                 jQuery('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
143         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
144
145     var filterVal = "progid:DXImageTransform.Microsoft.Alpha(opacity=30) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
146     var filterVal2 = "progid:DXImageTransform.Microsoft.alpha(opacity=100) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
147     var filterVal3 = "progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
148     jQuery('#foo').css("filter", filterVal);
149     equals( jQuery('#foo').css("filter"), filterVal, "css('filter', val) works" );
150     jQuery('#foo').css("opacity", 1);
151     equals( jQuery('#foo').css("filter"), filterVal2, "Setting opacity in IE doesn't duplicate opacity filter" );
152     equals( jQuery('#foo').css("opacity"), 1, "Setting opacity in IE with other filters works" );
153     jQuery('#foo').css("filter", filterVal3).css("opacity", 1);
154     ok( jQuery('#foo').css("filter").indexOf(filterVal3) !== -1, "Setting opacity in IE doesn't clobber other filters" );
155   });
156 }
157
158 test("css(String, Function)", function() {
159         expect(3);
160                 
161         var sizes = ["10px", "20px", "30px"];
162         
163         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
164                                  "<div class='cssFunction'></div>" + 
165                                  "<div class='cssFunction'></div></div>")
166                 .appendTo("body");
167         
168         var index = 0;
169         
170         jQuery("#cssFunctionTest div").css("font-size", function() {
171                 var size = sizes[index];
172                 index++;
173                 return size;
174         });
175                 
176         index = 0;
177         
178         jQuery("#cssFunctionTest div").each(function() {
179                 var computedSize = jQuery(this).css("font-size")
180                 var expectedSize = sizes[index]
181                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
182                 index++;
183         });
184
185         jQuery("#cssFunctionTest").remove();
186 });
187
188 test("css(String, Function) with incoming value", function() {
189         expect(3);
190                 
191         var sizes = ["10px", "20px", "30px"];
192         
193         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
194                                  "<div class='cssFunction'></div>" + 
195                                  "<div class='cssFunction'></div></div>")
196                 .appendTo("body");
197         
198         var index = 0;
199         
200         jQuery("#cssFunctionTest div").css("font-size", function() {
201                 var size = sizes[index];
202                 index++;
203                 return size;
204         });
205                 
206         index = 0;
207         
208         jQuery("#cssFunctionTest div").css("font-size", function(i, computedSize) {
209                 var expectedSize = sizes[index]
210                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
211                 index++;
212                 return computedSize;
213         });
214
215         jQuery("#cssFunctionTest").remove();
216 });
217
218 test("css(Object) where values are Functions", function() {
219         expect(3);
220                 
221         var sizes = ["10px", "20px", "30px"];
222         
223         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
224                                  "<div class='cssFunction'></div>" + 
225                                  "<div class='cssFunction'></div></div>")
226                 .appendTo("body");
227
228         var index = 0;
229         
230         jQuery("#cssFunctionTest div").css({fontSize: function() {
231                 var size = sizes[index];
232                 index++;
233                 return size;
234         }});
235                 
236         index = 0;
237                 
238         jQuery("#cssFunctionTest div").each(function() {
239                 var computedSize = jQuery(this).css("font-size")
240                 var expectedSize = sizes[index]
241                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
242                 index++;
243         });
244                 
245         jQuery("#cssFunctionTest").remove();
246 });
247
248 test("css(Object) where values are Functions with incoming values", function() {
249         expect(3);
250                 
251         var sizes = ["10px", "20px", "30px"];
252         
253         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
254                                  "<div class='cssFunction'></div>" + 
255                                  "<div class='cssFunction'></div></div>")
256                 .appendTo("body");
257
258         var index = 0;
259         
260         jQuery("#cssFunctionTest div").css({fontSize: function() {
261                 var size = sizes[index];
262                 index++;
263                 return size;
264         }});
265                 
266         index = 0;
267                 
268         jQuery("#cssFunctionTest div").css({"font-size": function(i, computedSize) {
269                 var expectedSize = sizes[index]
270                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
271                 index++;
272                 return computedSize;
273         }});
274                 
275         jQuery("#cssFunctionTest").remove();
276 });
277
278 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
279         expect(4);
280
281         var $checkedtest = jQuery("#checkedtest");
282         // IE6 was clearing "checked" in jQuery.css(elem, "height");
283         jQuery.css($checkedtest[0], "height");
284         ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
285         ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
286         ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
287         ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
288 });