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