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