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