8802fc00a4960243410f5469a29a7416686eb37b
[jquery.git] / test / unit / css.js
1 module("css");
2
3 test("css(String|Hash)", function() {
4         expect(29);
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
66 test("css(String, Object)", function() {
67         expect(21);
68
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('float', 'left');
79         equals( jQuery('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left');
80         jQuery('#floatTest').css('font-size', '20px');
81         equals( jQuery('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px');
82
83         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
84                 jQuery('#foo').css('opacity', n);
85                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
86                 jQuery('#foo').css('opacity', parseFloat(n));
87                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
88         });
89         jQuery('#foo').css('opacity', '');
90         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
91
92         // using contents will get comments regular, text, and comment nodes
93         var j = jQuery("#nonnodes").contents();
94         j.css("padding-left", "1px");
95         equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
96
97         // opera sometimes doesn't update 'display' correctly, see #2037
98         jQuery("#t2037")[0].innerHTML = jQuery("#t2037")[0].innerHTML
99         equals( jQuery("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
100
101         var div = jQuery("#nothiddendiv"),
102                 display = div.css("display"),
103                 ret = div.css("display", undefined);
104
105         equals( ret, div, "Make sure setting undefined returns the original set." );
106         equals( div.css("display"), display, "Make sure that the display wasn't changed." );
107 });
108
109 if(jQuery.browser.msie) {
110   test("css(String, Object) for MSIE", function() {
111     // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
112                 jQuery('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
113         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
114
115     var filterVal = "progid:DXImageTransform.Microsoft.Alpha(opacity=30) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
116     var filterVal2 = "progid:DXImageTransform.Microsoft.alpha(opacity=100) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
117     jQuery('#foo').css("filter", filterVal);
118     equals( jQuery('#foo').css("filter"), filterVal, "css('filter', val) works" );
119     jQuery('#foo').css("opacity", 1)
120     equals( jQuery('#foo').css("filter"), filterVal2, "Setting opacity in IE doesn't clobber other filters" );
121     equals( jQuery('#foo').css("opacity"), 1, "Setting opacity in IE with other filters works" )
122   });
123 }
124
125 test("css(String, Function)", function() {
126         expect(3);
127                 
128         var sizes = ["10px", "20px", "30px"];
129         
130         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
131                                  "<div class='cssFunction'></div>" + 
132                                  "<div class='cssFunction'></div></div>")
133                 .appendTo("body");
134         
135         var index = 0;
136         
137         jQuery("#cssFunctionTest div").css("font-size", function() {
138                 var size = sizes[index];
139                 index++;
140                 return size;
141         });
142                 
143         index = 0;
144         
145         jQuery("#cssFunctionTest div").each(function() {
146                 var computedSize = jQuery(this).css("font-size")
147                 var expectedSize = sizes[index]
148                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
149                 index++;
150         });
151
152         jQuery("#cssFunctionTest").remove();
153 });
154
155 test("css(String, Function) with incoming value", function() {
156         expect(3);
157                 
158         var sizes = ["10px", "20px", "30px"];
159         
160         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
161                                  "<div class='cssFunction'></div>" + 
162                                  "<div class='cssFunction'></div></div>")
163                 .appendTo("body");
164         
165         var index = 0;
166         
167         jQuery("#cssFunctionTest div").css("font-size", function() {
168                 var size = sizes[index];
169                 index++;
170                 return size;
171         });
172                 
173         index = 0;
174         
175         jQuery("#cssFunctionTest div").css("font-size", function(i, computedSize) {
176                 var expectedSize = sizes[index]
177                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
178                 index++;
179                 return computedSize;
180         });
181
182         jQuery("#cssFunctionTest").remove();
183 });
184
185 test("css(Object) where values are Functions", function() {
186         expect(3);
187                 
188         var sizes = ["10px", "20px", "30px"];
189         
190         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
191                                  "<div class='cssFunction'></div>" + 
192                                  "<div class='cssFunction'></div></div>")
193                 .appendTo("body");
194
195         var index = 0;
196         
197         jQuery("#cssFunctionTest div").css({fontSize: function() {
198                 var size = sizes[index];
199                 index++;
200                 return size;
201         }});
202                 
203         index = 0;
204                 
205         jQuery("#cssFunctionTest div").each(function() {
206                 var computedSize = jQuery(this).css("font-size")
207                 var expectedSize = sizes[index]
208                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
209                 index++;
210         });
211                 
212         jQuery("#cssFunctionTest").remove();
213 });
214
215 test("css(Object) where values are Functions with incoming values", function() {
216         expect(3);
217                 
218         var sizes = ["10px", "20px", "30px"];
219         
220         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
221                                  "<div class='cssFunction'></div>" + 
222                                  "<div class='cssFunction'></div></div>")
223                 .appendTo("body");
224
225         var index = 0;
226         
227         jQuery("#cssFunctionTest div").css({fontSize: function() {
228                 var size = sizes[index];
229                 index++;
230                 return size;
231         }});
232                 
233         index = 0;
234                 
235         jQuery("#cssFunctionTest div").css({"font-size": function(i, computedSize) {
236                 var expectedSize = sizes[index]
237                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
238                 index++;
239                 return computedSize;
240         }});
241                 
242         jQuery("#cssFunctionTest").remove();
243 });
244
245 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
246         expect(4);
247
248         var $checkedtest = jQuery("#checkedtest");
249         // IE6 was clearing "checked" in jQuery.css(elem, "height");
250         jQuery.css($checkedtest[0], "height");
251         ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
252         ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
253         ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
254         ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
255 });