No need for the try/finally logic in the css tests, won't even work as expected.
[jquery.git] / test / unit / css.js
1 module("css");
2
3 test("css(String|Hash)", function() {
4         expect(30);
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({styleFloat: 'right'});
23         equals( jQuery('#floatTest').css('styleFloat'), 'right', 'Modified CSS float using "styleFloat": Assert float is right');
24         jQuery('#floatTest').css({cssFloat: 'left'});
25         equals( jQuery('#floatTest').css('cssFloat'), 'left', 'Modified CSS float using "cssFloat": Assert float is left');
26         jQuery('#floatTest').css({'float': 'right'});
27         equals( jQuery('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right');
28         jQuery('#floatTest').css({'font-size': '30px'});
29         equals( jQuery('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px');
30
31         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
32                 jQuery('#foo').css({opacity: n});
33                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
34                 jQuery('#foo').css({opacity: parseFloat(n)});
35                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
36         });
37         jQuery('#foo').css({opacity: ''});
38         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
39
40         equals( jQuery('#empty').css('opacity'), '0', "Assert opacity is accessible via filter property set in stylesheet in IE" );
41         jQuery('#empty').css({ opacity: '1' });
42         equals( jQuery('#empty').css('opacity'), '1', "Assert opacity is taken from style attribute when set vs stylesheet in IE with filters" );
43
44         var div = jQuery('#nothiddendiv'), child = jQuery('#nothiddendivchild');
45
46         equals( parseInt(div.css("fontSize")), 16, "Verify fontSize px set." );
47         equals( parseInt(div.css("font-size")), 16, "Verify fontSize px set." );
48         equals( parseInt(child.css("fontSize")), 16, "Verify fontSize px set." );
49         equals( parseInt(child.css("font-size")), 16, "Verify fontSize px set." );
50
51         child.attr("class", "em");
52         equals( parseInt(child.css("fontSize")), 32, "Verify fontSize em set." );
53
54         // Have to verify this as the result depends upon the browser's CSS
55         // support for font-size percentages
56         child.attr("class", "prct");
57         var prctval = parseInt(child.css("fontSize")), checkval = 0;
58         if ( prctval === 16 || prctval === 24 ) {
59                 checkval = prctval;
60         }
61
62         equals( prctval, checkval, "Verify fontSize % set." );
63
64         equals( typeof child.css("width"), "string", "Make sure that a string width is returned from css('width')." );
65 });
66
67 test("css(String, Object)", function() {
68         expect(21);
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('styleFloat', 'left');
79         equals( jQuery('#floatTest').css('styleFloat'), 'left', 'Modified CSS float using "styleFloat": Assert float is left');
80         jQuery('#floatTest').css('cssFloat', 'right');
81         equals( jQuery('#floatTest').css('cssFloat'), 'right', 'Modified CSS float using "cssFloat": Assert float is right');
82         jQuery('#floatTest').css('float', 'left');
83         equals( jQuery('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left');
84         jQuery('#floatTest').css('font-size', '20px');
85         equals( jQuery('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px');
86
87         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
88                 jQuery('#foo').css('opacity', n);
89                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
90                 jQuery('#foo').css('opacity', parseFloat(n));
91                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
92         });
93         jQuery('#foo').css('opacity', '');
94         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
95
96         // using contents will get comments regular, text, and comment nodes
97         var j = jQuery("#nonnodes").contents();
98         j.css("padding-left", "1px");
99         equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
100
101         // opera sometimes doesn't update 'display' correctly, see #2037
102         jQuery("#t2037")[0].innerHTML = jQuery("#t2037")[0].innerHTML
103         equals( jQuery("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
104 });
105
106 if(jQuery.browser.msie) {
107   test("css(String, Object) for MSIE", function() {
108     // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
109                 jQuery('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
110         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
111
112     var filterVal = "progid:DXImageTransform.Microsoft.alpha(opacity=30) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
113     var filterVal2 = "progid:DXImageTransform.Microsoft.alpha(opacity=100) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
114     jQuery('#foo').css("filter", filterVal);
115     equals( jQuery('#foo').css("filter"), filterVal, "css('filter', val) works" );
116     jQuery('#foo').css("opacity", 1)
117     equals( jQuery('#foo').css("filter"), filterVal2, "Setting opacity in IE doesn't clobber other filters" );
118     equals( jQuery('#foo').css("opacity"), 1, "Setting opacity in IE with other filters works" )
119   });
120 }
121
122 test("css(String, Function)", function() {
123         expect(3);
124                 
125         var sizes = ["10px", "20px", "30px"];
126         
127         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
128                                  "<div class='cssFunction'></div>" + 
129                                  "<div class='cssFunction'></div></div>")
130                 .appendTo("body");
131         
132         var index = 0;
133         
134         jQuery("#cssFunctionTest div").css("font-size", function() {
135                 var size = sizes[index];
136                 index++;
137                 return size;
138         });
139                 
140         index = 0;
141         
142         jQuery("#cssFunctionTest div").each(function() {
143                 var computedSize = jQuery(this).css("font-size")
144                 var expectedSize = sizes[index]
145                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
146                 index++;
147         });
148
149         jQuery("#cssFunctionTest").remove();
150 });
151
152 test("css(Object) where values are Functions", function() {
153         expect(3);
154                 
155         var sizes = ["10px", "20px", "30px"];
156         
157         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
158                                  "<div class='cssFunction'></div>" + 
159                                  "<div class='cssFunction'></div></div>")
160                 .appendTo("body");
161
162         var index = 0;
163         
164         jQuery("#cssFunctionTest div").css({fontSize: function() {
165                 var size = sizes[index];
166                 index++;
167                 return size;
168         }});
169                 
170         index = 0;
171                 
172         jQuery("#cssFunctionTest div").each(function() {
173                 var computedSize = jQuery(this).css("font-size")
174                 var expectedSize = sizes[index]
175                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
176                 index++;
177         });
178                 
179         jQuery("#cssFunctionTest").remove();
180 });
181
182 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
183         expect(4);
184
185         var $checkedtest = jQuery("#checkedtest");
186         // IE6 was clearing "checked" in jQuery.css(elem, "height");
187         jQuery.css($checkedtest[0], "height");
188         ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
189         ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
190         ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
191         ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
192 });